Tree PDF
Tree PDF
Unit-4
C
B
D E G H
F L
Full Binary Tree(Strict Binary Tree) : For a full binary tree, every node has either
2 children or 0 children.
• An array can be used to store the nodes of a binary tree, which is called sequential
representation.
• In this representation, the nodes of tree are stored level-by-level, starting from the
0th level.
• The root node is always at index 0.
• Then, in successive memory locations the left child and the right child are stored.
B C
D E F G
A B C D E F G
It is a skewed binary tree. Since only the left sub-tree is present, this type of binary
tree is called left-skewed binary tree.
Madhvi Gaur (Asstt. Prof.)
Linked Representation of Binary Tree (Dynamic
Representation)
The binary tree can be represented using dynamic memory allocation in a linked list
form.
The basic component to be represented in a binary tree is a node.
The node consists of three fields:
(i) Info
Left_child Info Right_child
(ii) Left_child
(iii) Right_child
When a node has no children, the corresponding pointer fields will be NULL.
struct node
{
char info;
struct node *left;
struct node *right;
};
C
B
D E F
B C NULL
NULL G NULL
2. Inorder
(a) Traverse the Left-subtree of the root R.
(b) Process the root R.
(c) Traverse the Right-subtree of the root R.
3. Postorder
(a) Traverse the Left-subtree of the root R.
(b) Traverse the Right-subtree of the root R.
(c) Process the root. Madhvi Gaur (Asstt. Prof.)
void Preorder(struct node *root) void Postorder(struct node *root)
{ {
if(root!=NULL) if(root!=NULL)
{ {
printf(“%d”,root->info); Postorder(root->left);
Preorder(root->left); Postorder(root->right);
Preorder(root->right); printf(“%d”,root->info);
}} }}
void Inorder(struct node *root)
{
if(root!=NULL)
{
Inorder(root->left);
printf(“%d”,root->info);
Inorder(root->right);
}} Madhvi Gaur (Asstt. Prof.)
Binary Tree: Traversal
C
B
D E G H
F J L
In-Order Traversal:
D B F EAK J G C HL
Post-Order Traversal:
DFEBKJGLHCA
C
B
D E F
In-Order Traversal:
BDAECGF
Post-Order Traversal:
DBEGFCA
DHBE A FCG
DH B E F C G
D H E F G
EACK F HDBG
E A CK H D BG
E C K H B G
C B
HDBI E A FJCKGL
HD B IE FJ C KGL
H D I E F J K G L
J K L
H I
- +
a b
* e
c d
Madhvi Gaur (Asstt. Prof.)
For the following expression ((a * x + b) * x + e) * x + f
Construct the expression tree. Find the equivalent postfix notation.
* f
+ x
* e
+ x
* b
w1 w2
Create a tree T’ which gives the solution for the remaining weights : w1+w2, w3, w4,
w5 ……,wn.
The new 2-tree T is the desired solution.
LE = 3 + 3 + 2 + 3 + 4 + 4 + 2 = 21
LI = 0 + 1 + 2 + 1 + 2 + 3 = 9
So the total Path length of external node
LE = LI + 2 n = 9 + 2 * 6 = 21
Where n is the total number of internal
nodes in a tree.
P= 2 * 3 + 4 * 3 + 5 * 2 + 1 * 3 + 3* 4 + 2 * 4 + 2 * 2
P= 6 + 12 + 10 + 3 + 12 + 8 + 4
P= 55
5 2
2 4 1
3 2
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Binary Search Tree
A binary tree T is called a binary search tree if each node N of T has the following
property:
The value at N is greater than every value in the left subtree of N and is less than
every value in the right subtree of N.
Let x be a node in BST:
If y is a node in the left subtree of x, 38
then key[y] < key[x]
If y is a node in the right subtree of x, 14 56
then key[y]> key[x]
23 45 82
8
70
18
Madhvi Gaur (Asstt. Prof.)
Insertion in BST
• To insert a new value into a BST, we use the TREE_INSERTBST(root, item)
procedure TREE_INSERT. 1. if (root = =NULL)
• If the item value is less than the root node,
2. Return TREE_CREATENODE(item)
then we have to traverse the left subtree.
3. Else if(item < root->info)
• If the value is greater than the root node, then
we have to traverse the right subtree. 4. root->left = TREE_INSERTBST(root->left,
item)
• If root is empty, then make the item as a root
node. 5. Else
TREE_CREATENODE(item) 6. root->right = TREE_INSERTBST(root->right,
item)
1. new_node = (struct node *)
malloc(sizeof(struct node)) 7. Return root.
2. new_node->left=NULL 8. Exit.
3. new_node->info=item
4. new_node->right=NULL
5. Return new_node
6. Exit.
Madhvi Gaur (Asstt. Prof.)
Searching a Node in BST
A common operation performed on a BST is searching for a key stored in the tree.
The following procedure is used to search for a node with a given key in a BST.
Given a pointer to the root of the tree and a key k, TREE_SEARCH returns a
pointer to a node with key k if one exists, otherwise it returns NULL.
TREE_SEARCH(root, k, PAR)
1. If root = NULL,
Set LOC= NULL and PAR_LOC= PAR and return.
2. If root->info = k
Set LOC =root and PAR_LOC =PAR and return.
3. If root->info > k
Set PAR = root
Return TREE_SEARCH(root->left, k, PAR)
else
Set PAR = root
Madhvi Gaur (Asstt. Prof.)
Return TREE_SEARCH(root->right, k, PAR)
Deletion in BST
If we want to delete a node in BST, then we have to consider three cases:
Case 1: if a node to be deleted has no child
Then we simply delete/ remove that node.
Case 2: if a node to be deleted has only one child either left or right
If the node to be deleted has only left child, then we simply delete that node and
replace that node with its left child.
If the node to be deleted has only right child, then we simply delete that node and
replace that node with its right child.
Case 3: If the node to be deleted has both child
Then we replace that node with its inorder successor.
21
18 24
7 19 22 26
30
Madhvi Gaur (Asstt. Prof.)
Case 1: Suppose we have to delete 30.
18 24
7 19 22 26
18 24
7 19 22 30
19 24
7 22 26
30
Madhvi Gaur (Asstt. Prof.)
Deletion in BST
TREE_DELBST(root,item)
1. if(root = = NULL) // tree is empty.
Display “tree is empty” and return.
2. TREE_SEARCH(root,item,PAR)
3. if x==NULL // node to be deleted is not found
Display “ Data to be deleted is not found” and Return.
4. if(x->leftchild==NULL && x->rightchild==NULL) /*Case 1*/
5. if(Parent->rightchild==x)
Parent->rightchild=NULL
else
Parent->leftchild=NULL
free(x) and return.
6. if(x->leftchild==NULL && x->rightchild!=NULL) /*CASE 2*/
Madhvi Gaur (Asstt. Prof.)
7. if(parent->leftchild==x)
parent->leftchild = x->rightchild
else
parent->rightchild=x->rightchild
8. free(x) and return.
9. if(x->leftchild!=NULL && x->rightchild==NULL) /*CASE 2*/
10. if(parent->leftchild==x)
parent->leftchild = x->leftchild
else
parent->rightchild=x->leftchild
11. free(x) and return.
-1
0 1
A G
A1 A2
+1 0 0
A2 A3 A1
A3
A1 A2
0 0
-1
A1 A3
A2
0
A3
+2 +2
A1 A1 A3
-1 +1
A2 A3
A2 A1
0
0
A3 A2
-1
+1
A2 A3 A1 A2
0
0
A2
A3
+1
Insert 1 64
0
1
Insert 14
+2 0
+2
64 14
64
LR Rotation +1 0
-1 0
1 14
1 64
0 0
14 1
Madhvi Gaur (Asstt. Prof.)
Insert 26 Insert 110
-1 0
14 14
-1
0 +1 0
1 64
1 64
0 0
0
0 13 26 110
26
Insert 13 0 Insert 98 -1
-1 14 14
+1 -1
1 64 1 -1
64
0 +1
0 0 0
13 26 110
13 26
0
98
-2 -1
14 14
-1 -1
1 64 -2 -1
0 +2 1 64
LL
0 Rotation 0
13 26 110 0
0
13 26 98
+1
98 0
0
85 0 85 110
……… K ……….
Ai Aj
Where K is the key element, Ai and Aj are pointer to subtrees.
If Ai=Aj=NULL then delete K.
If Ai≠NULL, Aj=NULL, then choose the largest of the key elements K’ in the child node pointed by
Ai , delete the K’ and replace K by K’. (Deletion of K’ may call for subsequent replacements and
therefore deletions in similar manner.)
If Ai=NULL, Aj≠NULL, then choose the smallest of the key elements K’’ in the child node pointed
by Aj , delete the K’’ and replace K by K’’. (Deletion of K’’ may call for subsequent replacements and
therefore deletions in similar manner.)
If Ai≠NULL, Aj≠NULL, then choose either the largest of the key elements K’ in the subtree pointed
to by Ai or the smallest of the key elements K’’ from the subtree pointed to by Aj to replace K.
Insert 5: 1 5 1 5 8 11
Insert 2: 2 1 5 8 11 13
1 5 6 2 6 11
Insert 18:
Insert 8:
2 1 5 8 13 18
1 5 6 8
1 5 8 13 18 20
Insert 7: 2 6 11
1 5 7 8 13 18 20
2 6 11
Insert 9:
1 5 7 8 9 13 18 20
65 86 120 226
65 86 120 300
65 86 120 440
Delete 70:
• B-Tree of order 4
• Each node has at most 4 pointers and 3 keys, and at least 2 pointers and 1 key.
• Delete: 2, 21, 10, 3, 4
a
*9*
f g
*3*7* * 13 *
b d h c e
*1*2* *4*5* *8* * 10 * 12 * * 21 *
*9* a
f g
*3*7* * 13 *
b d h c e
*9* a
f g
*3*7* * 12 *
b d h c e
*1* *4*5* *8* * 10 * * 13 *
*3*7*9* a
b d h e
*1* *4*5* *8* * 12 * 13 *
*4*7*9* a
b d h e
*1* *5* *8* * 12 * 13 *
*7*9* a
b h e
*1*5* *8* * 12 * 13 *