Module 4 Trees
Module 4 Trees
TREES
DATA STRUCTURE HIMANI DESHPANDE ( TSEC ) 1
Introduction
Tree Terminologies
oThere is one and only one path between every pair of vertices in a
tree.
oA tree with n vertices has exactly (n-1) edges.
oA graph is a tree if and only if it is minimally connected.
oAny connected graph with n vertices and (n-1) edges is a tree.
Degree of
node A = 2, node F = 0,
node B = 3, node G = 1,
node C = 2, node H = 0,
node D = 0, node I = 0,
node E = 2. node J = 0,
node K = 0 HIMANI DESHPANDE ( TSEC ) 13
INTERNAL NODE
oThe node which has at least one child
is called as an internal node.
oInternal nodes are also called as
non-terminal nodes.
oEvery non-leaf node is an internal
node.
oA forest is a set of
disjoint trees.
struct node
{
struct node *left ;
int data ;
struct node *right ;
}
Example:
To construct a binary tree of height =
4, we need at least 4 + 1 = 5 nodes.
Example:
Maximum number of nodes in a binary tree of
height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Example:
Maximum number of nodes at level-2
in a binary tree
= 22
=4
Here,
oThe index 1 is holding the root, it has two children 5 and 16, they are placed at
location 2 and 3.
left
Right
HIMANI DESHPANDE ( TSEC ) 36
REPRESENTATION OF BINARY TREE
O/P:
A→B→D→E→C→F→G
Code snippet
PREORDER
O/P:
ABDCEGFHI
O/P:
D→B→E→A→F→C→G
INORDER TRAVERSAL
O/P:
BDAGECHFI
O/P:
{5 , 15 , 18 , 20 , 25 , 30 , 35 , 40 , 45 , 50 , 60}
O/P:
D→E→B→F→G→C→A
Code snippet
void postorder(struct node *root)
{
if(root==null)
return;
postorder(root -> left);
postorder(root -> right);
printf(“%c”,root -> data); O/P:
{5 , 18 , 15 , 25 , 20 , 35 , 45 , 60 , 50 , 40 , 30}
}
O/P:
DBGEHIFCA
Inorder Traversal-
10 , 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal-
10 , 30 , 20 , 150 , 300 , 200 , 100
Inorder Traversal-
24, 32, 40, 52, 58, 62, 69
Postorder Traversal-
32, 24, 40, 58, 69, 62, 52
Preorder Traversal-
Inorder Traversal-
Postorder Traversal-
In-order Traversal
10 , 20 , 30 , 100 , 150 , 200 , 300
15
There are three cases of deleting a node from binary search tree:
In Order : 2,10,12,15,16,17,19,20,30
HIMANI DESHPANDE ( TSEC ) 95
CASE 3: A NODE HAS 2 CHILDREN
Method-2:
oVisit to the left subtree of the
deleting node.
oPluck the greatest value element
called as inorder predecessor.
oReplace the deleting element with its
inorder predecessor.
oDelete the duplicate.
Example:
a + (b * c) + d * (e + f)
(a+(b*c))+(d*(e + f))
++a*bc*d+ef
abc*+def+*+
HIMANI DESHPANDE ( TSEC ) 114
EXPRESSIONS FROM GIVEN TREE
HUFFMAN ENCODING
•Consider an example, there is a message with 100 characters and there are only 4 distinct
characters with the following frequency.
a(50) b(30) c(15) d(5)
• In encoding, a character is represented with binary stream of 1's and 0's. Binary string which
represents a single character is codeword.
• In fixed length, codeword representing different characters have same length, whereas in
variable length, code word will have varying length.
0 1
0 1 1
0
0
1
Create Tree
o Print '0' for the left branch and print '1' for right branch.
o The accumulated 0's and 1's at each leaf constitutes the huffman
encoding corresponding the character.
AVL tree ensures that Insertion, Deletion and Search operations take
O(log n) in both average and worst cases.
HIMANI DESHPANDE ( TSEC ) 149
AVL TREE
• An AVL tree is a binary tree in which heights of LST and RST of the root
differ utmost by 1, in which LST and RST of the root are again AVL trees.
• An empty BT is an AVL tree.
•A non-empty binary tree 'T' is an AVL tree iff given,
|HL - HR| <= 1
Where HL - HR is known as balance factor and for an AVL tree balance
factor of a node can be either 0, 1, -1.
• An AVL tree reduces skewness from tree.
Balance of H is effected
B closest is child of H
11 17
L
7 53
L
4
LL
7 17
4 11 53
13
BALANCED
7 17
4 11 53
R
13
L
12
RL
7 17
4 11 53
R
12
R
13
RR
7 17
4 12 53
11 13
BALANCED
14
7 17
R
4 12 53
L
11 13
8
RL
7 17
R
4 11 53
R
8 12
13
RR
11 17
7 12 53
4 8 13
Balanced
11 17
7 12 53
4 8 13
14
L
11 17
L
7 12
4 8 13
LL
11
7 14
4 8 12 17
13
Balanced
Use Inorder predecessor
7 14
4 12 17
13
7 14
4 12 17
13
7
R
4 14
L
12 17
13
RL
7
R
4 12
R
14
13 17
RR
12
7 14
4 13 17
Balanced
15
15 24
20
10
24
13
20 20
13 24 15 24
10 15 13
10
HIMANI DESHPANDE ( TSEC ) 194
15, 20, 24, 10, 13,
15, 20, 24, 7, 30,
10, 13, 36,
7, 30, 36, 25
25
20
13
13 24 10 20
10 15 7 15 24
7 30
13 36
10 20
7 15 30
24 36
10 20 10 20
7 15 30 7 15 24
24 36 30
25 13 25 36
10 24
7 20 30
15 25 36
13 13
10 24 10 20
7 20 30 7 15 30
15 25 36 25 36
13 13
10 30 10 15
7 15 36 7 30
25 25 36
HIMANI DESHPANDE ( TSEC ) 197
AVL INSERTION EXAMPLE
Construct an AVL Tree for the following set of element to be inserted one by one.
13, 15, 20, 7, 10, 1, 18, 25, 16
AVL DELETION X B
Two phases
Phase 1 : Deletion is same as BST
Phase 2 : Rotations need to be applied in case imbalance.
If the node which is to be deleted is present in the left sub-tree of the critical node, then L rotation
needs to be applied
If the node which is to be deleted is present in the right sub-tree of the critical node, the R rotation
will be applied.
Based on balance factor of node B, rotations are further classified as
•R0 , R1 , R-1
•L0 , L1 , L-1
HIMANI DESHPANDE ( TSEC ) 205
✓ Which side child is deleted
AVL DELETION ✓ Check balance factor of sibling
R o → LL
R 1 → LL
R -1 → LR
Rotation
Delete 30
HIMANI DESHPANDE ( TSEC ) 206
R0 rotation (Node B has balance factor 0 )
R o → LL
R o → LL L o → RR
R 1 → LL L 1 → RL
R -1 → LR L -1 → RR
HIMANI DESHPANDE ( TSEC ) 211
R1 Rotation (Node B has balance factor 1)
AVL DELETION ✓ Which side child is deleted
✓ Check balance factor of sibling
AVL DELETION
R 1 → LL
AVL DELETION
R -1 → LR
L o → RR
L 1 → RL
L -1 → RR
HIMANI DESHPANDE ( TSEC ) 217
L0
Note: Unlike insertion in case of deletion we not only need to re-balance the subtree
where the first imbalance has occurred but also its unbalanced ancestors too.224
HIMANI DESHPANDE ( TSEC )
B-Trees
➢the number of keys in each non-leaf node is one less than the number of its
children and these keys partition the keys in the children in the fashion of a
search tree
➢all leaves are on the same level
➢all non-leaf nodes except the root have at least m / 2 children
➢the root is either a leaf node, or it has from two to m children
➢a leaf node contains no more than m – 1 keys
The number ‘m’ should always be odd
HIMANI DESHPANDE ( TSEC
226)
A B-tree of order 5
AN EXAMPLE B-TREE containing 26 items
26
6 12
42 51 62
1 2 4 7 8 13 15 18 25
27 29 45 46 48 53 55 60 64 70 90
Note that all the leaves are at the same level HIMANI DESHPANDE ( TSEC )
CONSTRUCTING A B-TREE
Suppose we start with an empty B-tree and keys arrive in the following order:
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
We want to construct a B-tree of degree 5
1 2 8 12 1 2 8 12
1 2 8 12
1 2 12 25
1 2 6 12 14 25 28
1 2 6 12 14 25 28
1 2 6 7 12 14 16 25 28 48 52
1 2 6 7 12 14 16 25 26 28 29 52 53 55 68
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
1 - If the key is already in a leaf node, and removing it doesn’t cause that leaf
node to have too few keys, then simply remove the key to be deleted.
2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that
its predecessor or successor will be in a leaf -- in this case we can delete the key
and promote the predecessor or successor key to the non-leaf deleted key’s
position.
22 7 9 15 22 31 43 56 69 72
2 7 9 15 22 31 43 56 69 72
2 7 9 15 22 31 43 56 69 72
7 9 15 22 31 43 56 69 72
7 9 15 22 31 43 69 72
Too few keys!
Delete 72
7 9 15 22 31 43 56 69
7 9 15 22 31 43 56 69
Delete 22
7 9 15 29 43 56 69
Advantages of B+ trees:
•Because B+ trees don't have data associated with interior nodes, more keys can fit on a page of memory.
Therefore, it will require fewer cache misses in order to access data that is on a leaf node.
•The leaf nodes of B+ trees are linked, so doing a full scan of all objects in a tree requires just one linear pass
through all the leaf nodes. A B tree, on the other hand, would require a traversal of every level in the tree.
This full-tree traversal will likely involve more cache misses than the linear traversal of B+ leaves.
HIMANI DESHPANDE ( TSEC ) 260
B vs B+ Tree
Advantages of B trees:
•Because B trees contain data with each key, frequently accessed nodes can lie closer to the root, and
therefore can be accessed more quickly.
Step 2: If the leaf doesn't have required space, split the node and copy the
middle node to the next index node.
Step 3: If the index node doesn't have required space, split the node and
copy the middle element to the next index page.
Step 2: if the leaf node contains less than minimum number of elements, merge
down the node with its sibling and delete the key in between them.
Step 3: if the index node contains less than minimum number of elements,
merge the node with the sibling and move down the key in between them.