Tree
Tree
B C D
Level
1
E F G I
v H
v v
Level
J K
v L
v M
v 2
Level
Fig: A Tree 3
4
Terminologies
Node: Each data item in a tree is called a node. It is the basic
structure in a tree. It specifies the data and links (branches) to
other data items. There are 13 nodes in the above tree.
5
Terminologies…
Degree of a tree: It is the maximum degree of nodes in a given
tree. In the above tree, the degree of node A is 3 and another
node I also have degree 4. In the whole tree, this value is the
maximum. So, the degree of the above tree is 4.
6
Terminologies…
Siblings: The children nodes of a given parent node are called
siblings. They are also called brothers. In the above tree,
E and F are siblings of parent node B
K, L and M are siblings of parent node I
8
Binary Tree
A binary tree is the most commonly used non-
linear data structure.
A binary tree is a finite set of data items which
is either empty or is partitioned into three
disjoint subsets. The first subset consists of a
single data item called the root of the binary
tree. The other two subsets are themselves
binary trees called the left subtree and right
subtree of the original binary tree.
In a binary tree, the maximum children of any
node is at most two.
9
A ROOT Level
0
B C
Level
1
D E G
F
v v
Level
H
v
I Jv 2
Note:
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is called father of B and B is called left or right son of A.
A node that has no sons (such as H, I, F or J) is called a leaf.
Node n1 is an ancestor of node n2 (and n2 is a descendant of n1) if n1 is
either the father of n2 or the father of some ancestor of n2. For example,
in the above binary tree A is an ancestor of I, and J is a descendant of C,
but E is neither an ancestor nor a descendant of C.
A node n2 is a left descendant of node n1 if n2 is either the left son of n1
or a descendant of the left son of n1. Similarly right descendant can be
defined.
11
Types of Binary Trees
Strictly Binary Tree
Complete Binary Tree
12
Strictly Binary Tree
If every non-terminal node in a binary tree
consists of non-empty left and right subtrees,
then such a tree is called strictly binary
tree. In this binary tree,
A all the non-
terminal nodes
such as C and D
B C have non-empty
left and right
subtrees.
D
v
E
v
Note: A strictly
binary tree with n
Fv G
v
leaves always
contains (2n-1)
Fig: A Strictly Binary Tree nodes. 13
Complete Binary Tree
A complete binary tree of depth d is the strictly binary
tree all of whose leaves are at level d.
In a complete binary tree, there is exactly one node at
level 0, two nodes at level 1, four nodes at level 2 and so on.
Note: A complete
A binary tree of
depth d contains a
B C total of (2d+1-1)
nodes with 2d
leaves at level d
D
v
E
v
F
v G
v
and 2d-1 non-leaf
nodes.
2 B 3
C
4 D
v 5 E
v 6 F
v 7 G
v
8 H 9 I
v Jv 10
B C
17
Is the following an almost complete binary tree?
H Iv Jv K
v
18
Is the following an almost complete binary tree?
B C
D
v
E
v F
v G
v
3 11
v 5 9 14
v
1 v v
2 10 12
v 15
v
v
6
v
7
v 13
22
C Implementation of BST
For the implementation of a binary search tree, a binarysearchtree
template is defined using a self-referential structure in linked list
format and after this, nodes of the binary search tree are inserted
and removed using dynamic memory allocation functions like
malloc() and free(). We consider rootnode as an external pointer
that keeps the address of the root node of the binary search tree. The
following code defines the structure for binary search tree and
creates a rootnode.
struct binarysearchtree
{
int info;
struct binarysearchtree *leftlink;
struct binarysearchtree *rightlink;
};
struct binarysearchtree *rootnode;
rootnode=NULL;
Whenever rootnode=NULL, the binary search tree is empty.
23
Insertion in BST
I. Inserting a node into an empty tree: In this
case, the node inserted into the tree is considered
as the root node.
II. Inserting a node into a non-empty tree: In this
case, we compare the new node to the root node of
the tree.
a. If the value of the new node is less than the value of the
root node, then if the left subtree is empty, the new node is
appended as the left leaf of the root node else we search
continuous down the left subtree.
b. If the value of the new node is greater than the value of
the root node, then if the right subtree is empty, the new
node is appended as the right leaf of the root node else we
search continuous down the right subtree.
c. If the value of the new node is equal to the value of the
root node, then print “DUPLICATE ENTRY” and return.
24
Recursive Function for Insertion in BST
Function Call: insertion(&rootnode,item); insertion(temp->leftlink, x);
Function Definition: }
void insertion(struct binarysearchtree **root, int x)
{ else if(temp->info<x)
struct binarysearchtree *temp,*node; {
temp=*root; if(temp->rightlink==NULL)
if(temp==NULL) //empty tree {
{ node=(struct binarysearchtree
*)malloc(sizeof(struct binarysearchtree));
node=(struct binarysearchtree *)malloc(sizeof(struct
binarysearchtree)); node->info=x;
node->info=x; node->leftlink=NULL;
node->leftlink=NULL; node->rightlink=NULL;
node->rightlink=NULL; temp->rightlink=node;
*root=node; }
} else
else if(temp->info>x) insertion(temp->rightlink, x);
{ }
if(temp->leftlink==NULL)
{ else
node=(struct binarysearchtree {
*)malloc(sizeof(struct binarysearchtree)); printf("\n DUPLICATE ENTRY!!!");
node->info=x; return;
node->leftlink=NULL; }
node->rightlink=NULL; }
temp->leftlink=node;
}
else
25
Deletion in BST
The node to be deleted may be a leaf node or a node with one
child or a node with two children.
a) Deleting a leaf node: In this case, the node is simply deleted from
the tree and NULL is set to its parent pointer.
Ex: Deleting 25 *rootnode
1011
1022 20 1033
101
1 1044
NULL 12 NULL NULL 30 1055
102 103
2
╳ 3
NULL 25 NULL NULL 35 NULL
104 105
4 5 26
Deletion in BST…
b) Deleting a node with one child: In this case,
the child of the node is appended to its parent
node.
Ex: Deleting 12
20 20
1 30 1 30
2 5
1
v 24 35
v
35
v
24
v
v
5
32
v
32
v
27
Deletion in BST…
c) Deleting a node with two children: In this
case, the node being deleted is replaced by:
leftmost node of its right child subtree
OR rightmost node of its left child subtree
Ex: Deleting 30
20
20
1 32
1 30 2
2 35
35 24
v
v
24
v
v
32
v
28
Algorithm for Deletion in BST
1) If leaf node
– set NULL to its parent pointer.
2) If node with one child
– redirect its child pointer to its parent
pointer.
3) If node with two child
– replace node being deleted by
I. Leftmost node of its right subtree
II. OR rightmost node of its left subtree
29
Tree Traversal
Tree traversal is one of the most common operations
performed on tree data structures.
Traversing a binary tree is a way in which each node
in the tree is visited exactly once in a systematic
manner.
We know that the order of traversal of nodes in a linear
list is from first to last, however there is no such
“natural” linear order for the nodes of a tree.
We have the following three orderings or methods for
traversing a non-empty binary tree:
Preorder (Depth-first order) Traversal
Inorder (Symmetric order) Traversal
Postorder Traversal
All theses traversal techniques are defined recursively.
30
Preorder Traversal
In this technique, first of all we process the root R of the binary
tree T. Then, we traverse the left subtree T1 of R in preorder
(which means that we traverse root of subtree T1 first and then
its left subtree). After visiting left subtree of R, then we take
over right subtree T2 of R and process all the nodes in preorder.
25
1 35
7
v
50
4 2
v 30
v 5
0
2
v 26 45
v 65
v
Preorder Traversal:
25 17 4 2 20 35 30 26 50 55
31
45 65 55
Traverse the given binary tree in preorder
A
B C
D
v
E
v
F
v
G
v
Iv Jv
Answer: A B D E I C F G J
32
Algorithm for preorder traversal
1. If root=NULL, return.
2. Visit the root.
3. Traverse the left subtree in preorder.
4. Traverse the right subtree in preorder.
A
B C
C
v
Postorder Traversal: D E
v F
5
G D BH I E F C A
Preorder Traversal:
G
v H Iv
A B D G C E H I F
34
Traverse the given binary tree in postorder
A
B
C
v
D
E
v Fv G
v H
v
I
v J
v K
v Lv
Answer: I E J F C G K L H D B Preorder: A B C E I F J D G H
A KL
35
Algorithm for postorder traversal
1. If root=NULL, return.
2. Traverse the left subtree in postorder.
3. Traverse the right subtree in postorder.
4. Visit the root.
v
D E
v F
5
G
v H Iv
Inorder Traversal:
D G B A H E I C F
37
Perform inorder traversal on the given tree
A
B
C
v
D
E
v Fv G
v H
v
Iv J
v K
v Lv
Answer: E I C F J B G D K H L A 38
Algorithm for inorder traversal
1. If root=NULL, return.
2. Traverse the left subtree in inorder.
3. Visit the root.
4. Traverse the right subtree in inorder.
40
Task
1) Suppose the following eight numbers are
inserted in order into an empty binary search
tree T:
50, 33, 44, 77, 35, 60, 40, 100
Draw the tree T.
42
Answer: The binary tree T is constructed from its root downwards
as follows:
a) The root of binary tree T is obtained by choosing the first node
in its preorder (since preorder is root-left-right). Thus the
node S is the root of tree T.
S root
b) The left child of the root node S is obtained as follows: First we
use the inorder of T to find the nodes in the left subtree T1 of
S (since inorder is left-root-right). Thus the left subtree T1
consists of the nodes V, P, N, A, Q and R. Then the left child
of S is obtained by choosing the first node in the preorder of
T1 (which clearly appears in the preorder of T). Thus P is the
left child of S.
S
P
43
c) Similarly, the right subtree T2 of S consists of the
nodes O, K, B, T and M. Thus the right child of S is
obtained by choosing the first node in the preorder of
T2. Thus the node T is the right child of S.
S
P T
d) Now, we need to find the left child of P. By seeing the
inorder of tree T, we find that the left subtree T1 of P
consists of only one node i.e. V. Thus the left child of P is V.
S
P T
V 44
e) The right subtree T2 of P consists of the nodes N, A, Q and R.
Thus the right child of P is obtained by choosing the first node in
the preorder of T2. Thus the node Q is the right child of S.
S
P T
V Q
f) The left subtree T1 of node T consists of the nodes O, K and B. By
seeing the preorder of T1, we find that the left child of T is O.
S
P T
V Q O 45
g) By seeing the inorder traversal, we find that the right
subtree T2 of node T consists of only one node M. Thus the
right child of node T is S
M.
P T
V Q O M
h) Now node V does not have any child because it is the leftmost node
according to the inorder traversal. Therefore it’s a leaf.
i) To find the left child of node Q, the left subtree T1 of node Q
consists of the nodes N and A (since V and P are already
occupied). By seeing the preorder, it is clear that the left child of
node Q is N.
S
P T
V Q O M
N 46
j) The right subtree T2 of node Q consists of only one node R.
Thus the right child of node Q is R.
S
P T
V Q O M
N R
k) Now node O does not have a left child. By seeing the inorder,
the right subtree of node O consists of nodes K and B. By
seeing the preorder, node K is the right child of node O.
S
P T
V Q O M
N R K
47
l) Node M is also a leaf node since it is the rightmost node in
the inorder traversal.
m) Node N does not have a left child since its left subtree is
empty.
n) The right subtree of node N consists of the only one node i.e.
A. S
P T
V Q O M
N R K
A
o) Node K does not have a left child as its left subtree is empty
which is obvious from its inorder.
p) The right subtree of node K contains the only remaining
node i.e. B.
Hence the final binary tree T is: 48
S
P T
V Q O M
N R K
A B
49
Classwork
A binary tree T has 9 nodes. The inorder and
preorder traversals of T yield the following
sequence of nodes:
In-order: E A C K F H D B G
Pre-order: F A E K C D H G B
Draw the tree T.
50
Answer:
F
A D
E K H G
C B
52
Answer:
A
B C
D E F
G H I
54
Application of Binary Tree
A binary tree (or more appropriately strictly
binary tree) can be used to represent an expression
containing operands and binary operators.
A binary tree used to represent a binary expression
is called a binary expression tree (BET).
The root of BET contains the operator and its two
children contains operands.
For example, X + Y can be represented as:
+
X Y
55
Application of Binary Tree…
If we have an expression with more arithmetic operators
like (X+Y)/(X-Y), then we have to construct two subtrees
and then these two subtrees are added to the root node as:
+ - /
X Y X Y + -
X Y X Y
When a binary expression tree is traversed in preorder (root-left-
right), we get prefix form of the expression and when traversed in
postorder (left-right-root), we get postfix form of the expression.
Thus postfix form of above arithmetic expression is: XY+XY-/
56
Classwork
Represent the following arithmetic
expressions by their corresponding binary
trees and find out their postfix equivalent:
a) A+B*C
b) (A+B)*C
c) A+(B-C)*D$(E*F)
d) (A+B*C)$((A+B)*C)
57
+
A
*
B C *
Postfix: ABC*+ A B
A
*
- $
B C D *
E F
+ *
A * + C
B C A B
61
Huffman Coding…
To encode letters using different length bit-strings, the
letters that occur more frequently are encoded using
shorter bit-strings and letters occurring rarely are
encoded using longer bit-strings.
When letters are encoded using varying numbers of bits,
some method must be used to determine where the bits
for each character start and end.
For example, if e were encoded with 0, a with 1, and t
with 01, then the bit string 0101 could represent eat,
tea, eaea, or tt.
One way to ensure that no bit-string corresponds to
more than one sequence of letters is to encode letters so
that the bit string for a letter never occurs as the first
part of the bit string for another letter. Codes with this
property are called prefix codes.
62
Huffman Coding…
An example of prefix code: If e is encoded with 0, a with 10, and t
with 11, then the bit string 10110 is the encoding of ate.
A prefix code can be represented using a binary tree, where the
characters are the labels of the leaves in the tree. The edges of the
tree are labeled so that an edge leading to a left child is assigned a 0
and an edge leading to a right child is assigned a 1. The bit string used
to encode a character is the sequence of labels of the edges in the
unique path from the root to the leaf that has this character as its
label.
o 1
e o 1
a t
63
Huffman Coding…
The Huffman coding or Huffman algorithm takes as
input the frequencies (which are the probabilities of
occurrences or repetitions known in advance) of symbols in
a string of text and produces as output a prefix code that
encodes the string using the fewest possible bits, among all
possible binary prefix codes for these symbols.
Given symbols and their frequencies, the goal is to
construct a rooted binary tree where the symbols are the
labels of the leaves.
The algorithm begins with a forest of trees each consisting of one
vertex, where each vertex has a symbol as its label and where the
weight of this vertex equals the frequency of the symbol that is its
label. At each step, we combine two trees having the least total weight
into a single tree by introducing a new root and placing the tree with
larger weight as its left subtree and the tree with smaller weight as
its right subtree. Furthermore, we assign the sum of the weights of
the two subtrees of this tree as the total weight of the tree. The
algorithm is finished when it has constructed a tree, that is, when the
forest is reduced to a single tree. 64
Question: Use Huffman coding to encode the following
symbols with the frequencies listed: A:0.08, B:0.10,
C:0.12, D:0.15, E:0.20, F:0.35. What is the average
number of bits used to encode a character?
Answer:
0.08 0.10 0.12 0.15 0.20 0.35
Initial
A B C D E F Forest
0 1 Step1
C D E F
B A
65
0.18 0.20 0.27 0.35
0 1 0 1 Step2
E F
B A D C
0 1
0 1 0 1
1 E 0 1 Step5
F 0
D C B A
Fig: Final Huffman Tree
or Huffman Coding of symbols)
Thus the Huffman encoding produced encode A by 111, B by
110, C by 011, D by 010, E by 10, and F by 00.
Thus the average number of bits used to encode a symbol
using Huffman encoding = 3*0.08 + 3*0.10 + 3*0.12 + 3*0.15
+ 2*0.20 + 2*0.35 = 2.45. 67
The Huffman Algorithm
Assumptions:
1. Let |C| be the number of symbols ai with frequencies wi, i=1, 2, …, n.
2. The tree is constructed in bottom-up manner starting from the |C| leaves and |C|-1
merging operations.
3. We use priority queue Q to keep nodes ordered by their frequencies.
Algorithm:
HuffmanAlgorithm(C)
{
n=|C|;
Q=C;
for(i=1;i<=n-1;i++)
{
z=Allocate_Node();
x=Extract_Min(Q);
y=Extract_Min(Q);
right(z)=x;
label_edge(z, x)=1;
left(z)=y;
label_edge(z, y)=0;
w(z)=w(x)+w(y);
Insert(Q, z);
}
} // The Huffman coding for the symbol ai is the concatenation of the
68