0% found this document useful (0 votes)
3 views20 pages

DS Lecture Week 11

The document provides an overview of Binary Search Trees (BST) and their operations, including construction, searching, and deletion of nodes. It also discusses balanced trees, specifically AVL trees, and the rotations required to maintain balance after insertions. Various cases of rotations (single and double) are illustrated to handle different scenarios of node insertions that lead to unbalanced trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

DS Lecture Week 11

The document provides an overview of Binary Search Trees (BST) and their operations, including construction, searching, and deletion of nodes. It also discusses balanced trees, specifically AVL trees, and the rotations required to maintain balance after insertions. Various cases of rotations (single and double) are illustrated to handle different scenarios of node insertions that lead to unbalanced trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Module-3

Non-Linear Data Structure


Tree

Dr. Ram Kishan Dewangan


Associate Professor, SCSET
Bennett University, Gr. Noida
Binary Search Tree (BST)
 A binary search tree is a binary tree in which each node possessed a key that satisfy the
following conditions
1. All key (if any) in the left sub tree of the root precedes the key in the root
2. The key in the root precedes all key (if any) in the right sub tree
3. The left and right sub trees of the root are again search trees

2
Construct Binary Search Tree (BST)
Construct binary search tree for the following data
50 , 25 , 75 , 22 , 40 , 60 , 80 , 90 , 15 , 30

50

25 75

22 40 60 80

15 30 90

Construct binary search tree for the following data


10, 3, 15, 22, 6, 45, 65, 23, 78, 34, 5
3
Search a node in Binary Search Tree
 To search for target value.
 We first compare it with the key at root of the tree.
 If it is not same, we go to either Left sub tree or Right sub tree as appropriate and repeat the
search in sub tree.
 If we have In-Order List & we want to search for specific node it requires O(n) time.
 In case of Binary tree it requires O(Log2n) time to search a node.

4
Delete node from Binary Search Tree

Delete node a

a Delete from Leaf Node

a Delete node a b

Delete from Non Terminal (Empty Left Sub Tree)

5
Delete node from BST

Delete node a

a C

b C

Delete from Non Terminal (Neither Sub Tree is Empty)

6
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
 Balanced binary trees are classified into two categories
 Height Balanced Tree (AVL Tree)
 Weight Balanced Tree Worst search time cases for Binary Search Tree

50 20

40 30

30
40

20
50

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, if the longest path in its left sub tree is one longer than the longest path of its
right sub tree (represented as 1).
 A node is called right heavy, if the longest path in its right subtree is one longer than the longest path of its
left sub tree (represented as -1).
 A node is called balanced, if the longest path in both the right and left sub-trees are equal (represented as
0).

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


 In general, one identifies Balanced Factor (BF) = Height(LST)-Height(RST)
 This Balanced Factor can be -1, 0 or +1.
 If there exists a node in a tree where this is not true, then such a tree is called Unbalanced
8
AVL Tree
0
Balanced Trees Critical Node Critical Node
Unbalanced Node Unbalanced Node
1 0

0 -1
0 0 0
-1
0 1
-1
0

0 -1
0

-1 1 1 1  Sometimes tree becomes unbalanced by inserting or deleting


any node
-1  Then based on position of insertion, we need to rotate the
0 0 0 unbalanced node
0
 Rotation is the process to make tree balanced
0
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 1

J 0
K
Right
Rotation
1’ 0
K Z 1 X 0 J
0 0’
1’ 0
X Y Y 0 N 0 Y 0 Z
0 0’
N 0’

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 1 13 0 7
Right
Rotation
1’ 0 7 0’ 0 15 1 5 0 13

1’ 0 5 0’ 0 10 10 0 3 0 10 0 15

3 0’
Insert node 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 -1 Y 0

Left
0’ 0 T1 Y 0 -1’ Rotation 0 X T3 -1

0’ 0 T2 T3 0 -1’ T2 0 T1 T2 0 n 0

n 0’

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

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

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

Critical Node 13 1 Insert node 3 0 7


Right
1’ 0 7 0’ 0 15 Rotation
1 5 0 13

1’ 0 5 0’ 0 10 10
10 15
3 0
0 0
3 0’

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

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

0’ 0 T2 T3 0 -1’
T2 0 T1 T2 0 n 0

n 0’

0 50 Critical Node
70 0
Left
0’ B 40 70 0-1’ Rotation of
Node 50 0 50 80 -1
0’ 0 60 80 0 -1’
60 0 40 0 60 0 90

Insert 90 90 0’

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 0
Right Rotation
Left Rotation
0 K 0 Z of Y Z
of
Parent (J) 1 K J 0
Left Child (K)
0 X -1 Y K n
n
0 X 0 n 0 Z
0 n X
18
Insertion into Right sub-tree of node’s Left child
Case - 2
12 12 1 12
Left Rotation Right Rotation
of Node 4 of Node 8
Critical Node 8 16 8 16 0 6 16 1

-1 4 0 10 14
6 10 14 0 4 8 -1 0 14
0 2 1 6
4
0 2 0 5 0 10
0 5 5
2 5

Left Right Rotation


Left Rotation of Left Child (4)
followed by
Right Rotation of Parent (8)
19
Insertion into Left sub-tree of node’s Right child
Case - 3
5 Critical Node 5 10 0

Right Rotation Left Rotation


0 3 1 13 3 10 0 5 13 -1
of Node 13 of Node 5

10 1 15 0 7 13 0 3 0 7 0 15
7

7 0 15

Right Left Rotation


Right Rotation of Right Child (13)
followed by
Left Rotation of Parent (5)
20

You might also like