DS Notes TREE
DS Notes TREE
DS Notes TREE
TREE
Introduction to Trees
A tree is a collection of elements called “nodes”,one
of which is distinguished as a root say r ,
By Nitin Sir
2|VAISHNAVI ACADEMY
Degree of a node:
The degree of a node is a number of children of a
node
A node of degree 2 and B node of degree 3.
Binary Tree
A tree is binary if each node of the tree can have
maximum of two children.
Children of a node of binary tree are ordered. One
child is called the “left” child and the other is
called “right” child.
By Nitin Sir
3|VAISHNAVI ACADEMY
By Nitin Sir
4|VAISHNAVI ACADEMY
By Nitin Sir
5|VAISHNAVI ACADEMY
By Nitin Sir
6|VAISHNAVI ACADEMY
if (T!=NULL)
inorder (T→left);
printf(‘\ n% , T→data);
By Nitin Sir
7|VAISHNAVI ACADEMY
if(T!= NULL)
postorder(T→left);
postorder (T→right);
Preorder sequence : A B C D E F G H I J
By Nitin Sir
8|VAISHNAVI ACADEMY
Inorder sequence : D C E B G F A I H J
Postorder sequence : D E C G F B I J H A
Postorder sequence : A B D G K H L M C E
Inorder sequence : K G D L H M A E C
Preorder sequence : K G L M H D B E C A
By Nitin Sir
9|VAISHNAVI ACADEMY
Preorder A B D G C E H I F
Inorder D G B A H E I C F
Step 1 : A
DGB HEICF
Step 2 :
A
B C
DG F
HEI
Step 3 :
C
B
D F
E
F H I
By Nitin Sir
10 | V A I S H N A V I A C A D E M Y
Find Operation
Whenever an element is to be searched, start
searching from the root node, then if the data is
less than the key value, search for the element in
the left subtree. Otherwise, search for the element
in the right subtree.
By Nitin Sir
11 | V A I S H N A V I A C A D E M Y
Recursive
BSTnode * Find(BSTnode *root, int x)
{
if (root == NULL )
{
return(null);
}
if(root→data == x)
return root;
if (root->key < key)
return search(root→right, x);
else
return search(root→left, x);
}
Non recursive
BSTnode * Find(BSTnode *root, int x)
{
while(root != null)
{
If(x==root→data)
return (root);
if(x>root→data)
root=root->right;
else
root=root→left;
}
return(NULL);
}
Insert Operation
By Nitin Sir
12 | V A I S H N A V I A C A D E M Y
T->data = x;
return temp;
}
/* Otherwise, recur down the tree */
if (X <T->data)
T->left = insert(T->left, X);
else if (X> T->key)
T->right = insert(T->right, key);
return (T);
}
Delete Operation
1. A Leaf node
2. A node with one child.
3. A node with two child.
1. A Leaf node
By Nitin Sir
13 | V A I S H N A V I A C A D E M Y
By Nitin Sir
14 | V A I S H N A V I A C A D E M Y
//One Child
if(root->left ==NULL || root->right ==NULL)
By Nitin Sir
15 | V A I S H N A V I A C A D E M Y
{
BSTnode *temp;
if(root->left ==NULL)
temp = root->right;
else
temp = root->left;
free(root);
return temp;
}
//Two Children
else
{
By Nitin Sir
16 | V A I S H N A V I A C A D E M Y
30,100,90,15,2,25,36,72,78,10
By Nitin Sir
17 | V A I S H N A V I A C A D E M Y
By Nitin Sir
18 | V A I S H N A V I A C A D E M Y
Huffman Coding
Huffman’s algorithm
By Nitin Sir
19 | V A I S H N A V I A C A D E M Y
Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
Solution-
Step-01:
By Nitin Sir
20 | V A I S H N A V I A C A D E M Y
Step-02:
Step-03:
Step-04:
Step-05:
By Nitin Sir
21 | V A I S H N A V I A C A D E M Y
By Nitin Sir
22 | V A I S H N A V I A C A D E M Y
Step-06:
Step 7
By Nitin Sir
23 | V A I S H N A V I A C A D E M Y
By Nitin Sir
24 | V A I S H N A V I A C A D E M Y
After we have constructed the Huffman tree, we will assign weights to all the edges. Let us assign
weight ‘0’ to the left edges and weight ‘1’ to the right edges
• a = 111
• e = 10
• i = 00
• o = 11001
• u = 1101
• s = 01
• t = 11000
By Nitin Sir
25 | V A I S H N A V I A C A D E M Y
= 2.52
= 58 x 2.52
= 146.16
AVL TREE
Adelson, Velski & Landis, AVL trees are height
balancing binary search tree. AVL tree checks the
height of the left and the right sub-trees and
assures that the difference is not more than 1. This
difference is called the Balance Factor.
BalanceFactor=height(leftsutree)−height(right sutree)
By Nitin Sir
26 | V A I S H N A V I A C A D E M Y
AVL Rotations
• Left rotation
• Right rotation
• Left-Right rotation
• Right-Left rotation
Left Rotation
Right Rotation
By Nitin Sir
27 | V A I S H N A V I A C A D E M Y
As depicted, the unbalanced node becomes the right child of its left child by performing a
right rotation.
Left-Right Rotation
State Action
A node has been inserted into the right subtree of the left subtree.
This makes C an unbalanced node. These scenarios cause AVL tree to
perform left-right rotation.
By Nitin Sir
28 | V A I S H N A V I A C A D E M Y
We shall now right-rotate the tree, making B the new root node of
this subtree. C now becomes the right subtree of its own left
subtree.
Right-Left Rotation
State Action
A node has been inserted into the left subtree of the right subtree.
This makes A, an unbalanced node with balance factor 2.
First, we perform the right rotation along C node, making C the right
subtree of its own left subtree B. Now, B becomes the right subtree
of A.
By Nitin Sir
29 | V A I S H N A V I A C A D E M Y
Expression tree
When expression is represented through a tree, it is
known as an expression tree.
By Nitin Sir
30 | V A I S H N A V I A C A D E M Y
TREE A + B * C + D * E
By Nitin Sir
31 | V A I S H N A V I A C A D E M Y
B-Trees
B-Tree is a self-balanced search tree in which every
node contains multiple keys and has more than two
children
Properties of B-Tree
By Nitin Sir
32 | V A I S H N A V I A C A D E M Y
By Nitin Sir