Unit-5 Trees
Unit-5 Trees
Trees
Tree is one of the most important non-linear data structures. Generally a tree structure
shows a hierarchical relationship among the nodes of the tree. One very common place
where such a situation arises is ‘parent-child’ relationship. Figure-5.1 shows parent-child
relationship among person in a family.
Mohan
Sweta Sunil
Each node of figure 5.1 represents a person name and each line connecting two nodes
denotes a relation namely ‘parent-child’ relation between the connected nodes. If a node ‘A’
is connected to a node ‘B’ and ‘A’ is above node ‘B’ in the tree then ‘A’ is a parent of ‘B’. This
figure shows that the node labeled ‘Mohan’ is a parent of nodes labeled ‘Suresh’, ‘Sanjay’
and ‘Shashi’. Similarly the node labeled ‘Karan’ and ‘Arjun’ are children of ‘Shashi’. In figure-
5.1, the tree originates from a node ‘Mohan’. A node having no parent, is called the ‘root’
node. In figure-5.1, ‘Mohan’ is the root node of the tree.
Before the detail discussion of trees, let us define a tree and some terminology that are often
used during the discussion of any type of tree. Consider the figure-5.2.
A tree may be defined as a finite set ‘T’ of one or more nodes such that
(i) there is a specially designated node, called the root, ‘R’, of the tree
(ii) the remaining nodes (excluding the root node) are partitioned into n 0 disjoint sets
T1, T2, …., Tn and each of these sets is a tree in turn, known as subtrees of the root.
Tree – a tree is a finite non-empty set of elements in which one element is called root
or more subtrees.
Node - Each element of a tree is called a node of the tree. A node may contain a value, a
condition, or represent a separate data structure (which could be a tree of its own). A tree,
shown in figure 5.2, has 12 nodes and each item of data represents a single letter for
convenience.
2
Degree of a node - The number of nodes connected to a particular node is called degree
of a particular node. The degree of a leaf node is always 1. The degree of ‘A’ is 3, of ‘C’ is 1,
of ‘D’ is 2 and of ‘I’ is 1.
B C D
E F G H
I J K L M
Terminal (Leaf) Nodes - Nodes that do not have any children are called leaf nodes. They
are also referred to as terminal nodes. Terminal nodes are also called as leaf nodes. ‘F’, ‘I’,
‘J’, ‘K’, ‘L’ and ‘M’ are leaf nodes.
Non-Terminal Nodes - Nodes that have children are called non-terminal nodes. Except
leaf nodes, all other nodes are referred to as non-terminal nodes. ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘G’ and
‘H’ are non-terminal nodes.
Parent/Child - The subtrees of a node are called its children. If ‘A’ has three children ‘B’,
‘C‘ and ‘D’ then ‘A’ is said to be a parent of ‘B’, ‘C‘ and ‘D’.
Sibling - Children of the same parent are said to be sibling. In figure-5.2, ‘B’, ‘C’ and ‘D’ are
siblings. Similarly ‘J’ and ‘K’ are siblings.
Edge - A link between a parent and its child is known as an edge or a branch. If ‘A’ is a
father of ‘B’ then we can define an edge as a tuple (A, B).
Path - A collection of edges connected one node to another is often termed as a path. For
example, the collection of edges {(A, B), (B, E), (E, I)} denotes a path from the node ‘A’ to
‘node ‘I’ in the tree of figure-5.2.
Ancestors of a node - Any node say ‘A’ is an ancestor of node ‘B’ if ‘A’ is either father of
‘B’ or the father of some ancestor of node ‘B’. same way ‘B’ is called the descendant of ‘A’.
The ancestors of a node ‘L’ are ‘A’, ‘D’ and ‘H’ and node ‘J’ is descendant of ‘C’.
Level of a node - The level of the root node is defined as 0 and level of any other node in
the tree is one more than the level of its father. If a node is at level ‘n’ then its children are at
level ‘n+1’. For example in the tree of figure-5.3, the node ‘E’ is at level 2 and ‘K’ is at level 3.
3
A
Level 0
B C
Level 1
Level 2
D E F G H
Level 3 I J K L
Figure 5.3
Depth of a Tree - The maximum level of any leaf in the tree is called as depth of a tree.
The depth of the tree shown in figure-5.3 is 3.
The children of a node of a binary tree are called left child and right child.
Binary Tree – a finite set of nodes which is either empty or consists of a root and
two disjoint binary trees called the left subtree and the right subtree
B C
D E F G
H I J K
In figure-5.4, nodes ‘A’, ‘B’, and ‘C’ have two children each. On the other hand nodes ‘D’ and
‘F’ have only one child each. Node ‘D’ has only right subtree and node ‘F’ has only left
subtree. The nodes ‘E’, ‘G’, ‘H’, ‘I’, ‘J’ and ‘K’ have no subtrees and they are leaf nodes in the
tree.
A left or right subtree can be empty. Figure-5.5 shows some typical binary trees.
A A A A
B B B C
D E
Figure 5.5
Look at figure-5.5(b), its root has an empty right subtree and in figure-5.5(c), the root has an
empty left subtree.
Strictly (Full) Binary Tree - If every non-terminal node in a binary tree has non-empty left
and right subtrees, the tree is termed as a strictly binary tree. Figure-5.5(d) shows a strictly
binary tree.
Complete Binary Tree - A binary tree is called a completely binary tree if it’s all nodes
have both children except last node. Figure-5.6 shows a complete binary tree of depth 2.
This type of binary tree will be discussed later on.
B C
D E F G
5
Complete binary tree – a special type of binary tree in which all nodes have both
children except last node
In a binary tree we can easily find the maximum number of nodes on any level. We know
that the root node is the only node on level 1. So the maximum number of nodes on level 1
is 1. Similarly on level 2 we can have maximum two nodes and on level 3 we can have
maximum four nodes. Thus on any level, say ‘i’, we can maximum 2 i-1 number of nodes for
i1.
An extended binary tree is a transformation of any binary tree into a complete binary tree.
This transformation consists of replacing every null subtree of the original tree with
“special nodes.” The nodes from the original tree are then internal nodes, while the “special
nodes” are external nodes.
Extended binary tree – a special type of complete binary tree in which all nodes
which every null subtree is replaced by “special node” node
For instance, consider the binary tree shown in figure 5.7(a). The tree shown in figure 5.7(b)
is its extended binary tree. The circles represent internal nodes, and filled rectangles
represent external nodes. Every internal node in the extended tree has exactly two children,
and every external node is a leaf. The result is a complete binary tree.
1 1
2 3 2 3
4 5 6 4 5 6
6 6
For example, if Data[I] denotes the data field (information) of node, say ‘I’, then the
LChild[I] would contain the index value of the left child of node ‘I’ on array data. Similarly
the rchild[i] would contain the index value of the right child of node ‘I’ on array data.
B C
D E F G
(a)
1 2 3 4 5 6 7
Data A B C D E F G
LChild 2 4 6 -1 -1 -1 -1
RChild 3 5 7 -1 -1 -1 -1
(b)
For example, if Data[2] is ‘B’, then LChild[2] =4 and RChild[1] = 5. This signifies that the 2nd
node contains ‘B’ as its data; its left child is the node indexed by ‘4’, that is ‘D’, and right child
is the node indexed by ‘5’, that is ‘E’. For the sake of simplicity, the root node is always
obtained at an index 1 in this array.
B C
D E F
G H
Figure 5.8
A binary tree can also be represented using a large single array. In this scheme, the nodes
are numbered sequentially level by level. Nodes in the same level are numbered from left to
right. Figure-5.9 shows the level-wise numbering of the nodes of the tree of figure-5.8.
Node-1
A
Node-2 Node-3
B C
Node-6
Node-4 -1
D E F
Node-5
Node-7
(empty
-1 -1 G -1 -1 H node)
Node-13
Node-8 Node-9 Node-10
(empty (empty
node) node) Node-11 Node-12
(empty (empty
node) node)
Figure 5.9
This figure clearly shows that if the number appearing against one node ‘A’ is ‘n’ then the
number appearing against the left and right children of ‘A’ are ‘2n +1’ and ‘2n+2’
respectively. Now if we store these nodes in an array tree[] then the number appearing
against the nodes may act as indices of the nodes in this array. Here is the array
representation for tree shown in figure-5.9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
In above array, unutilized space (empty nodes) is marked by dash entries. If it is a complete
binary tree then the array representation is ideal; otherwise there will be a lot of unutilized
space. Thus array representation will not always be efficient in terms of space. Another
limitation of array representation of binary tree is that insertion and deletion of nodes from
the middle of a tree require a lot of movement of many nodes to reflect the change in level of
these nodes. These problems are overcome by linked representation of binary trees.
data
lchild data rchild
lchild rchild
Figure 5.10
In algorithm notations, Data(Root) specifies the data field of Root node, LChild(Root)
specifies the left child of Root node and RChild(Root) of the Root node.
struct tnode
{
struct tnode *lchild;
int data;
struct tnode *rchild;
};
Let ‘Root’ is the root node of the tree and it is declared as:
tnode *Root;
If ‘Root’ is Null then the tree is empty. The linked list representation of the binary tree shown
in figure-5.8 is given in figure-5.11.
Root
C null
B
Figure 5.11
9
If we name these three tasks V, L and R for visiting the node, moving left and moving right
respectively, then there are three popular methods of traversing a binary tree in which it is
assumed that the left subtree must always visit before the right subtree. And these three
traversal techniques are called remain, LVR, LRV and VLR, which are known as inorder
(LVR), postorder (LRV) and preorder (VLR) respectively.
To traverse a non-empty binary tree in inorder traversal, the following operations are
performed:
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
To traverse a non-empty binary tree in preorder traversal, the following operations are
performed:
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
To traverse a non-empty binary tree in postorder traversal, the following operations are
performed:
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
Inorder traversing – the node is visited between the left subtree and right subtree
Postorder traversing – the node is visited after the left subtree and right subtree
Preorder traversing – the node is visited before the left subtree and right subtree
Here the names inorder, posrorder and preorder are related closely to producing the infix,
postfix, and prefix forms of an expression. A tress representing an expression is known as
an expression tree. An expression tree is constituted from the simple operands as the leaf
nodes of a binary tree, and the operators as the non-terminal nodes. Figure-5.12 shows a
simple expression tree.
A /
* D
B C
Figure 5.12 Expression Tree
10
In inorder traversal, a binary tree is moved down towards left until it can go no farther. Then
the node is visited, moved one node to the right and continues the same process for the left
subtree of right node. However if there is no move to the right, go back one more node. Here
is the algorithm which traverses a binary tree in an inorder fashion.
Algorithm : Inorder
Inorder(Root)
Here ‘Root’ contains the address of the root node.
Implementation : Inorder
Inorder(tnode *Root)
{
if (Root != NULL)
{
Inorder(Root->lchild);
printf(“\n%c”, Root->data);
Inorder(Root->rchild);
}
}
2 +
1 6
A /
4 7
* D
B C
3 5
Here numbers represent the sequence of visited node. The nodes are visited in inorder as:
A+B*C/D
11
Since this traversal gives the infix form of an expression, that’s why it is called as inorder
traversal.
In postorder traversal, the left subtree of ‘T’ is traversed first, then the right subtree is
traversed and finally the root node of ‘T’ is visited. Here is the algorithm which traverses a
binary tree in postorder way.
Algorithm : Postorder
Postorder(Root)
Here ‘Root’ contains the address of the root node.
Implementation : Postorder
Postorder(tnode *Root)
{
if (Root != NULL)
{
Postorder(Root->lchild);
Postorder(Root->rchild);
printf(“\n%c”, Root->data);
}
}
7
+
1 6
A /
4 * 5 D
B C
2 3
Here numbers represent the sequence of visited node. The nodes are visited in postorder
as:
ABC*D/+
12
Since this traversal gives the postfix form of an expression, that’s why it is called as
postorder traversal.
In preorder traversal, the node is visited first, then its left subtree of ‘T’ is traversed and then
finally its right subtree is traversed. Here is the algorithm which traverses a binary tree in
preorder way.
Algorithm : Preorder
Preorder(Root)
Here ‘Root’ contains the address of the root node.
Implementation : Preorder
Preorder(tnode *Root)
{
if (Root != NULL)
{
printf(“\n%c”, Root->data);
Preorder(Root->lchild);
Preorder(Root->rchild);
}
}
1
+
2 3
A /
4 * 7 D
B C
5 6
Here numbers represent the sequence of visited node. The nodes are visited in preorder as:
+A/*BCD
13
Since this traversal gives the prefix form of an expression, that’s why it is called as preorder
traversal.
Let us take another binary tree figure 5.13 (a) and apply each traversal technique as shown
in figure 5.13(b), 5.13 (c) and 5.13 (d).
2
A A
B C 1 B 4 C
D E 3 D 6 E
F G 5 F 7 G
1 7
A A
2 3 1 6
B C B C
4 D 5 E 2 D 5 E
6 F 7 G 3 F 4 G
Note that numbers represent the order in which the nodes are visited.
14
5.6 Searching, Inserting and Deleting Binary Search Trees
A binary search tree (BST) is a special binary tree and is defined as:
(i) every node has a key and no two nodes have the same key value
(ii) all left subtree’s keys (if any) are smaller than the root’s key
(iii) all right subtree’s keys (if any) are larger than the root’s key
(iv) the left and right subtrees of a binary search tree are itself binary search trees
Binary Search Tree – a binary tree such that all the elements in the left subtree of a
node ‘n’ are less than the content of ‘n’ and all the elements in the right subtree of ‘n’ are
greater than or equal to the contents of ‘n’
52 80
41 69 60 93
27 48 80 90 99
(a) (b)
In this we will how an element with key ‘item’ is searched in a binary search tree. Initially the
‘item’ is compared with the root’s key. If both match, then the search terminates successfully.
If the ‘item’ is less than the root’s key then the search is continued only in the left subtree of
the current root; otherwise the search is continued only in the right subtree of the current
root. This process continues either the item is found successfully or the current root node
contains a NULL value.
Here is the algorithm which searches a given key in a binary search tree.
Algorithm: Search
Search(Root, Item)
Here Root contains the address of root node and item be the element
to be searched.
Implementation : Search
On successful, this function returns 1; otherwise returns 0. This recursive function can also
be written in non-recursive way. Here is the non-recursive (iterative) version of above
Search() function.
When a new element is inserted into a binary search tree then there are two possibilities:
(i) If tree is empty then we make the new node as the root node of the tree
(ii) If the tree is not empty then this new item is inserted at proper position.
(a) Compare the key – ‘item’ with the root’s key value.
(b) If it is less then the new node must be inserted into the left subtree.
(c) If it is larger one then the new node must be inserted into the right subtree.
(d) If both keys are equal then it must display an error message (because in a binary
search tree, no two nodes have the same key).
For example, let we want to insert an element with key 44 into the tree of figure-5.15(a). For
this we perform the following actions:
(a) Here we first compare ‘44’ with the root’s key 68.
(b) Since 44 is less than 68 so we move towards left subtree of the tree rooted at 68.
16
(c) Now we compare 44 with 40. Since 44 is greater than 40 so we move towards right
subtree of the tree rooted at 40.
(d) Since there is no further nodes to be checked, so we insert this new element as the
right child of this node.
68 68
40 91 40 91
29 77 97 29 44 77 97
(a) (b)
Here is the algorithm which inserts a new node into a binary search tree.
Algorithm : TreeInsert
TreeInsert(Root, item)
Here Root contains the address of root node.
Implementation : TreeInsert
When we delete a node, it is first searched and then deleted. There are four possibilities:
Case 1 : If the node to be deleted is a leaf node the respective pointer of its parent is set to
NULL, as shown in figure-5.16(a). Figure-5.16(a) shows the deletion of node 48.
67 67
55 89 55 89
48 77 97 77 97
Case 2 : If the node to be deleted has either right child or left child. In such case we need to
adjust the respective link of its parent node as shown in figure-5.17(a) and figure-5.17(b).
67 67
55 89 48 89
48 77 97 77 97
67 67
55 89 55 97
48 60 97 48 60
Case 3 : If the node to be deleted has both children, as shown in figure-5.18. In such case,
the situation is bit complex. When the node to be deleted has both children, we first replace
the node to be deleted either the largest element in the left subtree or the smallest element
in the right subtree. After this we delete this replacing node from the subtree from which it
was taken.
For example, let we want to delete the element 71 in the tree shown in figure-5.18(a). Here
we will replace it by the smallest element ‘80’ in its right subtree as shown in figure-5.18(b).
For this we will need to find the inorder successor of a node 71. Inorder successor of a node
is found out by first going to its right child and then continually going to left as long as an
empty subtree is encountered. The inorder successor of element 71 is 80. Once the element
is replaced, we must delete the inorder successor.
50 50
32 71 32 80
21 61 89 21 61 89
80 98 71 98
Note that the inorder successor of any node has at most one child. In this case since 71 has
no child, the pointer from its parent 89 is set to NULL. Figure-5.18 (b) shows the resultant
tree after deleting element 71.
19
Here is the algorithm, which deletes the specific node from a binary search tree.
Algorithm : TreeDelete
TreeDelete(Root, item)
Here Root contains the address of root node.
Implementation : TreeDelete
A B
B C D A
D E E C
(a) (b)
21
Figure 5.19
It means that a binary tree can not be constructed if we have only its inorder or preorder or
postorder. But we can definitely construct a unique binary tree if we have inorder and
preorder sequence of nodes.
Let us see how to create a unique binary tree for the following inorder and postorder
traversal:
The above preorder sequence suggests that ‘A’ is the root node of the binary tree and the
inorder traversal suggests that there are two nodes (D B) in the left subtree and 4 nodes (Q
P R M) in the right subtree of node ‘A’.
Inorder : D B Inorder : Q P R M
Preorder : B D Preorder : M P Q R
Inorder Sequence: DB
Preorder Sequence : BD
The preorder clearly shows the next visited node which is none other than node ‘B’. And
since there is only one node ‘D’ remain in left subtree. The inorder traversal suggests that it
must be its left child, as shown in following figure:
B Inorder : Q P R M
Preorder : M P Q R
D
22
Now consider the inorder and preorder traversal of right subtrees.
The preorder clearly shows the next visited node which is none other than node ‘M’. and the
inorder traversal suggests that there are three nodes (Q P R) in the left subtree and no node
in its right subtree, as shown in following figure:
B M
D Inorder : Q P R
Preorder : P Q R
The preorder of this left subtree of node ‘M’ clearly shows that the next node to be visited is
‘P’ and the inorder shows that the node ‘Q’ is on the left side of node ‘P’ and the node ‘R’ is
on the right side of node ‘P’.
The tree for the above said preorder and inorder traversal will be as shown below:
B M
D P
Q R
Construction of binary tree from postorder traversal and inorder traversal is same as creation
of tree from inorder and preorder. The only difference is that here we will start looking the
nodes from the right side mezsn that the last node in the postorder traversal will be the first
and the first node will be inserted in the last.