Unit 4 Trees
Unit 4 Trees
Tree
Tree is a derived, Non-Linear data structure base on
Directed, Acyclic Graph
Where the in-degree for each node can be 0/1
Out-degree is n
A
Root – Indegree 0 B C D
Leaf/External – outdegree 0
Internal node – outdegree is E F G
is non-zero
Basic Terminology
Root node The root node Ris the topmost node in the tree. If R = NULL, then it means the
tree is empty.
Sub-trees If the root node Ris not NULL, then the trees T1, T2,and T3 are called the sub-
trees of R.
Leaf node A node that has no children is called the leaf node or the terminal node.
Path A sequence of consecutive edges is called a path. For example, in Fig. 9.1, the path
from the root node Ato node Iis given as: A, D, and I.
Ancestor node An ancestor of a node is any predecessor node on the path from root to that
node. The root node does not have any ancestors. In the tree given in Figure, nodes A, C, and
G are the ancestors of node K.
Descendant node A descendant node is any successor node on any path from the node to a
leaf node. Leaf nodes do not have any descendants. In the tree given in Figure, nodes C, G,
J, and Kare the descendants of node A.
Level number Every node in the tree is assigned a level number in such a way that the root
node is at level 0, children of the root node are at level number 1. Thus, every node is at one
level higher than its parent. So, all child nodes have a level number given by parent’s level
number + 1.
Degree of a node is equal to the number of children that a node has. Degree The degree of a
leaf node is zero.
In-degree In-degree of a node is the number of edges arriving at that node.
Out-degree Out-degree of a node is the number of edges leaving that node.
Types of Trees
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees Forest and its corresponding tree Binary tree
Parent If N is any node in T that has left successor S1 and right successor S2, then N is called the parent
of S1and S2. Correspondingly, S1and S2 are called the left child and the right child of N.
Sibling All nodes that are at the same level and share the same parent are called siblings(brothers).
Edge It is the line connecting a node Nto any of its successors. A binary tree of n nodes has exactly n – 1edges
because every node except the root node is connected to its parent via an edge.
Path A sequence of consecutive edges. For example, in Figure, the path from the root node to the node 8 is
given as: 1, 2, 4, and 8.
Depth The depth of a node N is given as the length of the path from the root R to the node N. The depth of the
root node is zero.
Height of a tree It is the total number of nodes on the path from the root node to the deepest node in the tree. A
binary tree of height hhas at least hnodes and at most 2h – 1 nodes
Similar binary trees Two binary trees T and T’ are said to be similar if both these
trees have the same structure.
Copies Two binary trees T and T’ are said to be copies if they have similar structure
and if they have same content at the corresponding nodes
Ternary Tree
Binary Tree
Strictly B. T. Almost Complete B. T. Complete B. T.
A A A
B C B C B C
D E D E F D E F G
3 7 3 8
2 4 6 9 2 1 6 5
Linked list representation
5
L 5 R
3 7
NULL
L 3 L 7 R
2 6 9
NULL
NULL
NULL
NULL
NULL
NULL
2 6 9
Postorder
2 6 9
Left, right, root
2, 3, 6, 9, 7, 5
Preorder
Root, left, right
5, 3, 2, 7, 6, 9
Give the inorder, preorder andpostorder traversal
void inorder(int tree[],int root)
{
if(tree[root]!=0) void preorder(int tree[],int root)
{ {
inorder(tree,2*root); if(tree[root]!=0)
printf("%d ",tree[root]); {
inorder(tree,2*root +1);
}
printf("%d ",tree[root]);
} preorder(tree,2*root);
preorder(tree,2*root +1);
void postorder(int tree[],int root) }
{ }
if(tree[root]!=0)
{
postorder(tree,2*root);
postorder(tree,2*root +1);
printf("%d ",tree[root]);
}
}
Constructing a Binary Tree from Traversal
Steps to construct the Binary tree given the in-order and pre-order traversal:
• Use the pre-order sequence to determine the root node of the tree. The first element
would be the root node.
• Elements on the left side of the root node in the in-order traversal form the left sub-tree
of the root node. Similarly, elements on the right side of the root node in the in-order
traversal form the right sub-tree of the root node.
• Recursively select each element from pre-order traversal sequence and create its left
and right sub-trees from the in-order traversal sequence.
Steps to construct the Binary tree given the in-order and post-order traversal:
• In post-order traversal the root node is the last node. Rest of the steps will be the same
as mentioned above
Constructing Tree given preorder and inorder
A X
S E M R
T Q D C F
Constructing Tree given preorder and inorder
A X
S E M R
T Q D C F
Constructing Tree given postorder and inorder
void create() void inorder(tnode *root)
int main( )
{ {
{
tnode *newnode, *temp, *prev; if(root!=NULL)
create();
int k,n; {
printf("\n");
printf("no. of nodes:"); inorder(root->left);
inorder(root);
scanf("%d",&n); printf("%d ",root->info);
printf("\n");
printf("Enter the values of each node:"); inorder(root->right);
postorder(root);
for(k=1;k<=n;k++) }
printf("\n");
{ }
preorder(root);
newnode = (tnode*) malloc (sizeof(tnode));
void postorder(tnode *root) getch();
newnode->left=newnode->right=NULL;
{ }
scanf("%d", &newnode->info);
if(root==NULL) if(root!=NULL)
root=newnode; {
else postorder(root->left);
{ postorder(root->right);
temp=root; printf("%d ",root->info);
while(temp!=NULL) }
{ }
prev=temp; void preorder(tnode *root)
if(newnode->info < temp->info) {
temp = temp->left; if(root!=NULL)
else {
temp = temp->right; printf("%d ",root->info);
} preorder(root->left);
if(newnode->info < prev->info) preorder(root->right);
prev->left = newnode; }
else }
prev->right = newnode;
}
}
}
Binary Search Trees
A binary search tree, also known as an
ordered binary tree, is a variant of
binary trees in which:
• all the nodes in the left sub-tree have
a value less than that of the root node
and
• all the nodes in the right sub-tree
have a value either equal to or greater
than the root node.
• The same rule is applicable to every
sub-tree in the tree.
78
65 85
53 68 81 89
45 62 72 87 92
Inserting a node in a Binary Search Tree
Inserting 48
78
65 85
53 68 81 89
45 62 72 87 92
48
Deleting a node from a Binary Search Tree
65 85
53 68 81 89
45 62 72 87 92
Deleting a node from a Binary Search Tree
65 85
65 85
53 68 81 89
53 68 81 89
45 72 87 92
45 62 72 87 92
Deleting a node from a Binary Search Tree
65 87
65 85
53 72 81 89
53 68 81 89
45 92
45 62 72 87 92
Deleting a node from a Binary Search Tree
65 85
65 85
53 72 81 89
53 68 81 89
45 87 92
45 62 72 87 92
Deleting a node from a Binary Search Tree
Expression Tree
Binary tree representing the Arithmetic expression.
Internal nodes are operators and external nodes represent operands
Example:
a b
Expression Tree
A+B*C–D/E
–
A + T1 – D / E
A + T1 – T2 T2
/
T3
+
A + T1 – T2
T3 – T2 D E
A T1
*
T4
B C
Try this
1. P + Q * (R – S) + T
2. A + B / C * D – E
Construction of Expression Tree
For every symbol of Postfix expression do
a) Construct a node and initialize left and right to NULL
b) If the symbol is operand, then, store that in newnode and push on to
the stack
c) If the symbol is operator, then,
a) Newnode->right = pop( )
b) Newnode->left = pop( )
c) Newnode->info = symbol
d) Push (newnode)
At the end, top of the stack contains root node.
Given an expression, Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h)),
construct the corresponding binary tree.
((a + b) – (c * d)) % ((e ^f) / (g – h))
( T1 – T2 ) % ( T3 / T4)
T5 % T6
T7
Given the binary tree, write down the expression that it represents
100
10 80 130 200
Threaded Binary Tree
In a linked representation of a binary tree, the number
of null links (null pointers) are actually more than non-
null pointers.
Consider the following binary tree:
Threaded Binary Tree
In above binary tree, there are 7 null pointers & actual 5
pointers.
In all there are 12 pointers.
We can generalize it that for any binary tree with n nodes
there will be (n+1) null pointers and 2n total pointers.
The objective here to make effective use of these null
pointers.
A. J. perils & C. Thornton jointly proposed idea to make
effective use of these null pointers.
According to this idea we are going to replace all the null
pointers by the appropriate pointer values called threads.
Threaded Binary Tree
And binary tree with such pointers are called threaded
tree.
In the memory representation of a threaded binary tree,
it is necessary to distinguish between a normal pointer
and a thread.
Threaded Binary Tree
Therefore we have an alternate node representation for
a threaded binary tree which contains five fields as
show bellow:
Threaded Binary Tree
Alsoone may choose a one-way threading or a two-way
threading.
Here,our threading will correspond to the in order
traversal of T.
Threaded Binary Tree - One-Way