Trees in Discrete Algorithm and Analysis
Trees in Discrete Algorithm and Analysis
B C D
E F G H I J K L M
Fig(a).Tree
Binary Tree: It is a tree with finite set of nodes which consists of a root itself or
root with left sub tree or right sub tree or consists of both.
D E F G
H I
B
D
B E E F E
E
C F F
F
B C
D E
F G
B C
D E F G
10 18
8
20
Fig. Binary Search Trees 0
Ex1: Construct the binary search tree with the following elements:
10,15,12,7,8,18,6,9,17,20
Step1: The first element 10 treated has root
root
10
5
step2:The element 15 greater than the root therefore it is inserted as right
child of root.
10
root
5
15
5
Step3:The element 12 is greater than the root and less than the 15 therefore it
is inserted as left child of 15. 10 root
5
15
5
12
5
Step4: The element 7 is less than the root therefore it is inserted as left child of
root. 10 root
5
7 15
5
12
5
step5: The element 8 is less than the root and greater than 7 therefore it is
inserted as right child of 7. root
10
5
7 15
5
8 12
5
step6: After insertion of element 18.
root
10
5
7 15
5
8 12 18
5 5
step7: After insertion of element 6.
root
10
5
7 15
5
6 8 12 18
5 5
step8: After insertion of element 9.
10 root
5
7 15
5
6 8 12 18
5 5
9
Step9: After insertion of element 17.
root
10
5
7 15
5
6 8 12 18
5 5
9 17
Step10: After insertion of element 20. 5
root
10
5
7 15
5
6 8 12 18
5 5
9 17 20
5
step2:The element F greater than the root therefore it is inserted as right child
of root.
D
root
Step3:The element B is less than the root therefore it is inserted as left child of
root. D root
B F
Step4: The element C is less than the root therefore it is inserted as right child
of B. D
B F
step5: The element E is greater than the root and less than F therefore it is
inserted as left child of F. root
D
B F
C E
B F
A C E
B F
A C E G
EX3: Construct BST with elements 25,15,40,20,30,85,10,5,50,45,8,18.
Tree Traversals:
It is nothing but process each node exactly once without leaving any node. It is
used to print node values in binary tree. But in lists we start from root node,
the root consists of first node address and we follow the links until we
encounter the NULL value. In trees we start from root after root there are two
ways i.e left or right.
Therefore 3 traversing techniques available.
1. Inorder Traversal (left, root, right)
2. Preorder Traversal (root, left, right)
3. Postorder Traversal (left, right, root)
Ex1: root Inorder Traversal: B A C
A
Preorder Traversal: A B C
B C Postorder Traversal: B C A
Fig. Binary Tree
Ex2: Consider the Binary tree then find the Inorder, Preorder and Postorder
Traversal
10
7 15
5
6 8 12 18
5 5
D E
Fig. Binary Tree
Inorder Traversal: B D C E A
Preorder Traversal: A B C D E
Postorder Traversal: D E C B A
2.The first element after the root in preorder inserted as left child in BST and
examine the inorder and preorder to identify the occurrence of each element.
3.This process is repeated until left sub tree and right sub tree constructed.
20 root
15 25
5
5 18 22 28
Fig.BST
Ex6: Consider the Inorder: D B A E C F
Preorder: A B D C E F and construct the BST?
A root
B C
D E F
Fig.BST
Binary Tree Deletions:
Consider the Binary Tree
root
50
5
40 60
35 45 55 70
70 temp
b) The leaf node is left child of a parent: : if we consider the above binary tree
then 60 parent
55 temp
parent
(a) 50 50 (b)parent (c)
parent
50 (d)
50
5 parent 5 5
5
40 temp 40 temp temp
temp 60
60
In (a)left child of temp is assigned to left child of parent then ,we get
50 parent
5
35 child
In (b)right child of temp is assigned to left child of parent then ,we get
50 parent
5
45 child
In (c)left child of temp is assigned to right child of parent then ,we get
50 parent
5
55 child
In (d)right child of temp is assigned to right child of parent then ,we get
parent
50
5
child
70
Case 3: Deletion of a node with Two Children
Here first find we find the Inorder successor for the deleted node then that
node is replaced with Inorder successor.
Consider the following binary tree
root
50
5
40 60 temp
35 45 55 70
68 Junior Parent(JP)
65 Inorder Successor(IS)
66
Fig. Binary Tree
Here we delete the node 60 its inorder successor is 65 .The IS is available at
leftmost children of right child, once we find the IS then right child of IS
attached to left child of JP. Finally temp is replaced with IS, Then we get
50
5
40 65 Inorder Successor(IS)
35 45 55 70
68 Junior Parent(JP)
66
Fig. Binary tree after deletion
//Implementation of BST
#include<stdio.h>
struct node
int data;
};
void main()
int ch,x;
nodeptr *root=NULL;
clrscr();
do
printf("\n 1.Insertion");
printf("\n 2.Inorder");
printf("\n 3.Preorder");
printf("\n 4.Postorder");
printf("\n 5.Search");
printf("\n 6.Exit");
scanf("%d",&ch);
switch(ch)
scanf("%d",&x);
root=ins(root,x);
break;
case 2:inorder(root);
break;
case 3:preorder(root);
break;
case 4:postorder(root);
break;
scanf("%d",&x);
search(root,x);
break;
case 6:exit(0);
}
}while(ch<=6);
nodeptr *p,*temp,*parent;
p=(nodeptr *)malloc(sizeof(nodeptr));
p->data=x;
p->left=p->right=NULL;
if(root==NULL)
root=p;
return(root);
else
temp=root;
parent=NULL;
while(temp!=NULL)
if(x>temp->data)
parent=temp;
temp=temp->right;
}
else if(x<temp->data)
parent=temp;
temp=temp->left;
if(x>parent->data)
parent->right=p;
return(root);
if(x<parent->data)
parent->left=p;
return(root);
nodeptr *p;
p=root;
if(p!=NULL)
inorder(p->left);
printf("%4d",p->data);
inorder(p->right);
nodeptr *p;
p=root;
if(p!=NULL)
printf("%4d",p->data);
preorder(p->left);
preorder(p->right);
nodeptr *p;
p=root;
if(p!=NULL)
postorder(p->left);
postorder(p->right);
printf("%4d",p->data);
nodeptr *temp;
int f=0;
temp=root;
while(temp!=NULL)
if(x==temp->data)
f=1;
break;
else if(x>temp->data)
temp=temp->right;
else if(x<temp->data)
temp=temp->left;
if(f==1)
else
}
Applications of Trees or Expression Trees:
The binary tree is constructed by using infix expression or postfix expression
then those tree are called as Expression trees.
(a) Construct of Expression tree by using Infix Expression:
Here the middle of the operator is treated as root for Expression tree
the two operands inserted as left child and right child of operator.
Ex: Consider the Infix Expression A + B
+
A B
+ -
A B C D
A
Step2: After the insertion of node B
A B
A B
A B
A B
Step4: After the insertion of node C
+ C
A B
+ C D
A B
+ -
A B C D
+ -
A B C D
20
15 25
5
5 18 22 28
1000 20 2000
500
1000 2000
5 18 22 28
500
1000 2000
5 18 22 28
B C
D E F G
H I J K
0 G -1 2
1
H -1 -1
2 K -1 -1
0
3 A 5 7
root
4 D 1 -1
5 B 4 8
6 F -1 -1
7 C 6 0
8
E 9 10
9 I -1 -1
0
10 J -1 -1
(ii)Array or Sequential Representation: Here we use only one array. The root
node stored at 0th location and its left child is stored at 1st location and its right
child is available at 2 nd location and so on….
In general the root node is stored at k th location and its
left child is available at (2k+1) and its
right child is available at (2k+2)
Array or sequential Representation for above binary tree
A B C D E F G H I J K
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14