You are on page 1of 53

Non-Linear Data Structure

Tree Part-3
Height of a Tree
 The height of a tree is the length of the path from the root to the deepest node (leaf node) in
the tree.
 Counting number of edges between root node and furthest leaf

Non-Linear Data Structure (Tree Part-3) 2


Depth of a Tree
 Depth of a tree is defined as number of edges / arc from the root node to the leaf node of the
tree is called as the Depth of the Tree
Depth – 0

Depth – 1 Depth – 1

Depth – 2 Depth – 2

Depth – 3

Depth – 4 Depth – 4

Non-Linear Data Structure (Tree Part-3) 3


Compare - Height of a Trees
Height – 3 A
Depth – 0 A A

Height – 1 Height – 2
B C Depth – 1 B C B C
Depth – 1

D E F D E E F G
D
Height – 0 Height – 0 Height – 0
Depth – 2 Depth – 2 Depth – 2

G F G

Height – 0
Depth – 3

Non-Linear Data Structure (Tree Part-3) 4


Problem with Heights - BST
 Searching node with value 79
13

35
21

21 48
29

Hops = 5
35
13 29 79

48

24 Hops = 2
79

Non-Linear Data Structure (Tree Part-3) 5


Balanced Tree
 Binary Search Tree gives advantage of Fast Search, but sometimes in few cases we are not able
to get this advantage. E.g. look into worst case BST
 Such trees are un-balanced trees

Worst search time cases for Binary Search Tree

50 20

40 30

30
40

20
50

Non-Linear Data Structure (Tree Part-3) 6


Balanced Tree
 Balanced trees is effective in search and related operations
 Balanced binary trees are classified into two categories
 Height Balanced Tree (AVL Tree)
 Weight Balanced Tree

 Concepts of height is an important part of understanding this

Non-Linear Data Structure (Tree Part-3) 7


Height Balanced Tree (AVL Tree)
 A tree is called AVL tree (Height Balanced Tree), if each node possessed one of the following
properties
 A node is called left heavy (L), if the longest path in its left sub tree is one longer than the longest path of its
right sub tree
 A node is called right heavy (R), if the longest path in its right subtree is one longer than the longest path of
its left sub tree
 A node is called balanced (B), if the longest path in both the right and left sub-trees are equal

 In height balanced tree, each node must be in one of these states


 If there exists a node in a tree where this is not true, then such a tree is called Unbalanced

Non-Linear Data Structure (Tree Part-3) 8


AVL Tree
B
Critical Node Critical Node
Balanced Trees
Unbalanced Node Unbalanced Node
L B

B R
B B B
R
B L
R
B

B R
B

R L L L  Sometimes tree becomes unbalanced by inserting or deleting


any node
R  Then based on position of insertion, we need to rotate the
B B B unbalanced node
B
 Rotation is the process to make tree balanced
B
Non-Linear Data Structure (Tree Part-3) 9
Right Rotation
a. Detach left child’s right sub-tree
b. Consider left child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old left child’s old right sub-tree as left sub-tree of new right child
Critical Node L

J B
K
Right
Rotation
L’ B L
K Z X B J
B B’
L’ B
X Y Y B N B Y B Z
B B’

N B’

Non-Linear Data Structure (Tree Part-3) 10


Right Rotation
a. Detach left child’s right sub-tree
b. Consider left child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old left child’s old right sub-tree as left sub-tree of new right child

Critical Node L 13 B 7
Right
Rotation
L’ B 7 B’ B 15 L 5 B 13

L’ B 5 B’ B 10 10 B 3 B 10 B 15

3 B’
Insert node 3
Non-Linear Data Structure (Tree Part-3) 11
Left Rotation
a. Detach right child’s leaf sub-tree
b. Consider right child to be new parent
c. Attach old parent onto left of new parent
d. Attach old right child’s old left sub-tree as right sub-tree of new left child

Critical Node X R Y B

Left
B’ B T1 Y B R’ Rotation B X T3 R

B’ B T2 T3 B R’ T2 B T1 T2 B n B

n B’

Non-Linear Data Structure (Tree Part-3) 12


Select Rotation based on Insertion Position
Case 1: Insertion into Left sub-tree of nodes Left child Case - 1 Case - 2
 Single Right Rotation
Case 2: Insertion into Right sub-tree of node’s Left child
 Left Right Rotation
Case 3: Insertion into Left sub-tree of node’s Right child
 Right Left Rotation
Case 4: Insertion into Right sub-tree of node’s Right child
 Single Left Rotation Case - 3 Case - 4

Non-Linear Data Structure (Tree Part-3) 13


Insertion into Left sub-tree of nodes Left child
 Case 1: If node becomes unbalanced after insertion of new node at Left sub-tree of nodes Left
child, then we need to perform Single Right Rotation of unbalanced node to balance the node
 Right Rotation
a. Detach leaf child’s right sub-tree
b. Consider leaf child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old leaf child’s old right sub-tree as leaf sub-tree of new right child
Case - 1

Single Right Rotation


of
unbalanced node

Non-Linear Data Structure (Tree Part-3) 14


Insertion into Left sub-tree of nodes Left child
Critical Node
L Case - 1
J
B
Right K
L’ B K Z B B’ Rotation
L X B J
L’ B X Y B B’
Y Y
B N B B Z
N B’

Critical Node 13 L Insert node 3 B 7


Right
L’ B 7 B’ B 15 Rotation
L 5 B 13

L’ B 5 B’B 10 10
10 15
3 B
B B
3 B’

Non-Linear Data Structure (Tree Part-3) 15


Insertion into Right sub-tree of node’s Right child
 Case 4: If node becomes unbalanced after insertion of new node at Right sub-tree of nodes
Right child, then we need to perform Single Left Rotation of unbalance node to balance the
node
Case - 4
 Left Rotation
A. Detach right child’s leaf sub-tree
B. Consider right child to be new parent
C. Attach old parent onto left of new parent
D. Attach old right child’s old left sub-tree as right sub-tree of new left child
Single Left Rotation
of
unbalanced node

Non-Linear Data Structure (Tree Part-3) 16


Insertion into Right sub-tree of node’s Right child
Critical Node X R Y B Case - 4
Left
B’ B Y B R’ Rotation
T1
B X T3 R

B’ B T2 T3 B R’
T2 B T1 T2 B n B

n B’

R 50 Critical Node
70 B
Left
B’B 40 70 B R’ Rotation of
Node 50 B 50 80 R
B’ B 60 80 B R’
60 B 40 B 60 B 90

Insert 90 90 B’
Non-Linear Data Structure (Tree Part-3) 17
Insertion into Right sub-tree of node’s Left child
 Case 2: If node becomes unbalanced after insertion of new node at Right
sub-tree of node’s Left child, then we need to perform Left Right Rotation
for unbalanced node. Case - 2
Left Right Rotation
 Left Right Rotation Left Rotation of Left Child
 Left Rotation of Left Child followed by followed by
 Right Rotation of Parent Right Rotation of Parent
Case - 2
J Critical Node J Y B
Right Rotation
Left Rotation
B K B Z of Y Z
of
Left Child (K)
Parent (J) L K J B

B X R Y K n
n
B X B n B Z
B n X
Non-Linear Data Structure (Tree Part-3) 18
Insertion into Right sub-tree of node’s Left child
Case - 2
12 12 L 12
Left Rotation Right Rotation
of Node 4 of Node 8
Critical Node 8 16 8 16 B 6 16 L

R 4 B 10 14
6 10 14 B 4 8 R B 14

B 2 L 6
4
B 2 B 5 B 10
B 5 5
2 5

Left Right Rotation


Left Rotation of Left Child (4)
followed by
Right Rotation of Parent (8)
Non-Linear Data Structure (Tree Part-3) 19
Insertion into Left sub-tree of node’s Right child
Case - 3
5 Critical Node 5 10 B

Right Rotation Left Rotation


B 3 L 13 3 10 B 5 13 R
of Node 13 of Node 5

10 L 15 B 7 13 B 3 B 7 B 15
7

7 B 15

Right Left Rotation


Right Rotation of Right Child (13)
followed by
Left Rotation of Parent (5)
Non-Linear Data Structure (Tree Part-3) 20
Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 6, 5, 4, 3, 2, 1
Insert 6 Insert 3 Insert 2
5 L 5
B 6 L 5
Critical Node 4 6 Case 1 B 3 B 6
Insert 5
L 4 B 6
Right Rotation
of Node 4
L 6 L 3 B 2 B 4
B 3

B 5 B 2

Insert 4 Insert 1
5 Critical Node B 3
Critical Node 6
B 5 L 3 B 6 L 2 B 5
L 5 2
Case 1 B 4 B 6 L 2 B 4 Case 1 B 1 B 4 B 6
Right Rotation Right Rotation
B 4 of Node 6 B 1 4 of Node 5

Non-Linear Data Structure (Tree Part-3) 21


Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 64, 1, 44, 26, 13, 110, 98, 85
Insert 64 Insert 26 , 13 Case 3: Right Left Rotation
Right Rotation of Right Child 26
B 64 L 44 Followed By L 44
44
Left Rotation of Parent 1
Critical
Insert 1 R 1 64 1 64 B 13 B 64
Node
L 64 Right Rotation Left Rotation
B L’ 26 of Right Child 26 13 of Parent 1 B 1 B 26

B 1 B’ 13 26
Insert 44
Case 2: Left Right Rotation
Left Rotation of Left Child 1 64
Critical Node 64 B 44
Followed By
R 1 Right Rotation of Parent 64 44
Right Rotation B 1 B 64
B 44 1 of Parent 64
Left Rotation of Left Child 1

Non-Linear Data Structure (Tree Part-3) 22


Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 64, 1, 44, 26, 13, 110, 98, 85
Insert 110 , 98 Case 3: Right Left Rotation
Right Rotation of Right Child 110
B 44 Followed By 44 B 44
Left Rotation of Parent 64
B 13 R 64 Critical Node 13 64 B 13 B 98
Right Rotation Left Rotation
B 1 B 26 B L’ 110 1 26 98
of Right Child 110 of Parent 64 B 1 B 26 B 64 B 110

B’ 98 110

Insert 85
R 44

B 13 L 98

B 1 B 26 R 64 B 110

B 85
Non-Linear Data Structure (Tree Part-3) 23
Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 60 Insert 76 ,79 Insert 82

B 60 R 73 76 Critical B 76
R 73 Case 4
Left Rotation 73 79 Case 4 B 73 B 81
Insert 73 B 60 75 R Critical of Node 75 B 60 B 76 Left Rotation
60 75 R 81 of Node 79 60 75 79 82
R 60 B 76 R’ B 75 B 79
B B B B
B 73 79 B’ B 82

Insert 75 Insert 81
B 76
B 73 Critical Node
60 Critical Node
73 B 73 R 79
Case 4 B 60 76 R Case 4
R 73 Left Rotation Left Rotation
of Node 60 60 75 of Node 73
B 60 B 75 B 81
B B B 75 R 79
B 75
75
B 81
Non-Linear Data Structure (Tree Part-3) 24
Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 300 , 0 Insert 5 Case 2: Left Right Rotation,
76 Critical Left Rotation of Left Child 0, 76
76 B
Followed By
73 81 Right Rotation of Parent 60 73 81
L 73 81 R
60 75 79 82 5 75 79 82
L 60 75 79 82 R
B B 0 60
R 0 300 300
0 B B 300

B 5

Insert 73

Can not Insert 73 as duplicate key found

Non-Linear Data Structure (Tree Part-3) 25


Assignment
 Construct AVL Search tree by inserting following elements in order of their occurrence

 50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
 21 , 26 , 30 , 9 , 4 , 14 , 28 , 18 , 15 , 10 , 2 , 3 , 7

Non-Linear Data Structure (Tree Part-3) 26


More Illustrations
 https://www.cs.usfca.edu/~galles/visualization/AVLtree.html

Non-Linear Data Structure (Tree Part-3) 27


Deleting node from AVL Tree
 If element to be deleted does not have empty right sub-tree, then element is replaced with its
In-Order successor and its In-Order successor is deleted instead
 During winding up phase, we need to revisit every node on the path from the point of deletion
up to the root, rebalance the tree if require

28 B
Make sure that the given tree remains AVL after every deletion
R 23 B 32

B 22 R 26 R 30 R 34

B 27 B 31 B 36

Non-Linear Data Structure (Tree Part-3) 28


Deleting node from AVL Tree
Delete 30
28
30
31
23 32
23 32 Critical Node
34
22 26 30
22 26 31 34 R

27 31 36
27 36 B
In-Order Traversal
22, 23, 26, 27, 28, 30, 31, 32, 34, 36 Case 4:
Left Rotation of
Delete 28 Node 32
28 B
30 L 31
R 23 32 R 23 R 34 B

B 22 R 26 30
31 R R 34 32 B 36 B
B 22 R 26

27 31 36 27 B
B B B

Non-Linear Data Structure (Tree Part-3) 29


Deleting node from AVL Tree
Delete 73, 74 Delete 73
73
L 74
73
13 75
L 13 75 R
10 28 74 89
L 10 B 28 74 89 B
5 In-Order Traversal
5, 10, 13, 28, 73, 74, 75, 89 5 B

Delete 74

74 Critical
75 B
13
Case 1: Right Rotation
L 13 89 B
75 of Node 75 L 10 75 B
L 10 28 B 89
28 89 B
B 5 B 28
B 5

Non-Linear Data Structure (Tree Part-3) 30


Exercises
 Create a tree with following nodes:
 180, 200, 100, 125, 70, 150, 50, 80, 90, 110, 130, 120
 When node 50 will be deleted, what will be resultant AVL tree?

 Consider the following tree:


30

12 45

18

Insert: 10,15,40,20,22 Delete: 22


What will be the post order traversal of resultant tree after insertion and deletion.
Non-Linear Data Structure (Tree Part-3) 31
Weight Balanced Tree
 In a weight balanced tree, the nodes are arranged on the basis of the knowledge available on
the probability for searching each node
 The node with highest probability is placed at the root of the tree
 The nodes in the left sub-tree are less in ranking as well as less in probability then the root
node
 The nodes in the right sub-tree are higher in ranking but less in probability then the root node
 Each node of such a Tree has an information field contains the value of the node and count
number of times node has been visited

 Commonly, weight balanced trees, the nodes may have weight equal to number of children

Non-Linear Data Structure (Tree Part-3) 32


Weight Balanced Tree
Probability

P 23

20 21
E T

K 15 S V
14 13
11 G 10 M

Non-Linear Data Structure (Tree Part-3) 33


Weight Balanced Tree

M 2
P
G 4 T 3 D G T
M L Z
D 4 L 5 P 6 Z 1
Ordered Tree
P 6

L 5 T 3

G 4 M 2 Z 1

D 4

Non-Linear Data Structure (Tree Part-3) 34


Applications
 Maintaining Ordered Lists or Dictionaries
 Operating System – Processes and Memory management
 Databases - Indexing

 Managing Priority Queues

Non-Linear Data Structure (Tree Part-3) 35


Multiway Search Tree (B - Tree)
 The nodes in a binary tree like AVL tree contains only one record
 AVL tree is commonly stored in primary memory
 In database applications where huge volume of data is handled, the search tree can not be
accommodated in primary memory
 B-Trees are primarily meant for secondary storage
 B-Tree is a M-way tree which can have maximum of M Children

10, 15, 20

2, 3 11, 14 16 21

4 – way Tree

Non-Linear Data Structure (Tree Part-3) 36


Multiway Search Tree (B - Tree)
 An M- way tree contains multiple keys in a node
 This leads to reduction in overall height of the tree
 If a node of M-way tree holds K keys then it will have k+1 children

No of Keys = 3

K1, K2, K3
No of Ways or children = 4

Non-Linear Data Structure (Tree Part-3) 37


Multiway Search Tree (B - Tree)
 A tree of order M is a M-way search tree with the following properties
1. The Root can have 1 to M-1 keys
2. All nodes (except Root) have (M-1)/2 to (M-1) keys
3. All leaves are at the same level
4. If a node has ‘t’ number of children, then it must have ‘t-1’ keys
5. ​Keys of the nodes are stored in ascending order

K0, K1, K2, ……… ,Kn-1

P0 P1 P2 Pn-1 Pn

Non-Linear Data Structure (Tree Part-3) 38


Multiway Search Tree (B - Tree)

K0, K1, K2, ……… ,Kn-1

P0 P1 P2 Pn-1 Pn

 K0, K1, K2, ……… ,Kn-1 are keys stored in the node
 Sub-Trees are pointed by P0, P1, P2, ……… ,Pn then
 K0 >= all keys of sub-tree P0

 K1 >= all keys of sub-tree P1

 ………..

 ………..

 Kn-1 >= all keys of sub-tree Pn-1

 Kn-1 < all keys of sub-tree Pn Non-Linear Data Structure (Tree Part-3) 39
Multiway Search Tree (B - Tree)
 This node has capacity of 7 keys (K1 – K7) and 8 children (C1 – C8)

C1 K1 C2 K2 C3 K3 C4 K4 C5 K5 C6 K6 C7 K7 C8

 Relation amongst the keys:


 K1 < K2 < K3 < K4 < K5 < K6 < K7
 The key K1 is greater than all the keys in subtree pointed to by C1 and less than all
keys in subtree pointed to by pointer C2
 Keys(C1) < K1 < Keys (C2) < K2 < Keys (C3) < C3 …..

Non-Linear Data Structure (Tree Part-3) 40


Multiway Search Tree (B - Tree)

20

10, 15 25, 40, 50

2, 8 11 16, 18 22 30, 35 42, 44 55, 60

B-Tree of Order 4 (4 way Tree)

Non-Linear Data Structure (Tree Part-3) 41


Example: B – Tree of Order 5

30 70

8, 25 40, 50 76, 88

1, 3, 5, 7 11, 19 27, 29 71, 73, 75 77, 85 89, 97

32, 37 43, 49 56, 67

Non-Linear Data Structure (Tree Part-3) 42


Generalizations
 From the said conditions it is implied that
 M-way Search Trees are generalized form of Binary Search Trees
 A Binary Search Tree is considered as an M-way Search Tree of order 2

Non-Linear Data Structure (Tree Part-3) 43


Insertion of Key in B-Tree
1. If Root is NULL, construct a node and insert key
2. If Root is NOT NULL
I. Find the correct leaf node to which key should be added
II. If leaf node has space to accommodate key, it is inserted and sorted
III. If leaf node does not have space to accommodate key, we split node into two parts

Non-Linear Data Structure (Tree Part-3) 44


Split Node (5 way Tree, max 4 Keys)

P
5, 10, 15, 20 1, 5, 9, 11

Insert - 3 Insert - 3 Overflow


3, 5, 10, 15, 20 1, 3, 5, 9, 11
Overflow
5
10
1, 3 9, 11
3, 5 15, 20

Non-Linear Data Structure (Tree Part-3) 45


Split Node (5 way Tree, max 4 Keys)

10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35 42, 45, 48

Insert - 38

10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35 ,38 42, 45, 48

Non-Linear Data Structure (Tree Part-3) 46


Split Node (5 way Tree, max 4 Keys)
10, 20, 30, 40

3, 5, 6, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

Insert 7

10, 20, 30, 40

3, 5, 6, 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

Overflow

Non-Linear Data Structure (Tree Part-3) 47


Split Node (5 way Tree, max 4 Keys)

6, 10, 20, 30, 40 Overflow

3, 5 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

20

6, 10 30, 40

3, 5 7, 8 12, 14 21, 25, 27 31, 35, 38 42, 45, 48

Non-Linear Data Structure (Tree Part-3) 48


Construct M-Way Tree
Construct 5 Order (5 Way) Tree from following data
1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
We are asked to create 5 Order Tree (5 Way Tree) maximum 4 records can be accommodated in a node

Insert 1 Insert 7 Insert 6 Insert 2 Insert 11


1, 2, 6, 7, 11 6
1 1, 7 1, 6, 7 1, 2, 6, 7
Overflow 1, 2 7, 11
Insert 5 Insert 10 Insert 13
6 6 6

1,2,5 7,11 1,2,5 7,10,11 1,2,5 7,10,11,13

Non-Linear Data Structure (Tree Part-3) 49


Construct M-Way Tree
Construct 5 Order (5 Way) Tree from following data
1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
Insert 12 Insert 20, 16, 24

6 6, 11 6, 11
Overflow Overflow
1,2,5 7,10,11,12,13 1,2,5 7,10 12,13 1,2,5 7,10 12,13,16,20,24

Insert 3,4
3, 6, 11, 16
6, 11, 16 6, 11, 16
Overflow
1,2 4,5 7,10 12,13 20,24
1,2,5 7,10 12,13 20,24 1,2, 3,4,5 7,10 12,13 20,24

Non-Linear Data Structure (Tree Part-3) 50


Construct M-Way Tree
Construct 5 Order (5 Way) Tree from following data
1, 7, 6, 2, 11, 5, 10, 13, 12, 20, 16, 24, 3, 4, 18, 19, 14, 25
Insert 18,19,14
3, 6, 11, 16

1,2 4,5 7,10 12, 13, 14 18, 19, 20, 24

Insert 25
3, 6, 11, 16 11
Overflow
1,2 4,5 7,10 12, 13, 14 18, 19, 20, 24, 25 3, 6 16, 20

3, 6, 11, 16, 20 Overflow 1,2 4,5 7,10 12, 13, 14 18, 19 24, 25

Non-Linear Data Structure (Tree Part-3) 51


Construct M-Way Tree
Construct a B-Tree of order 5 from following data
10, 40, 30, 35, 20, 15, 50, 28, 25, 5, 60, 19, 12, 38, 27, 90, 45, 48

Non-Linear Data Structure (Tree Part-3) 52


Thank
You

You might also like