Ds 12 AVL Tree
Ds 12 AVL Tree
INTRODUCTION
Balance Binary Search Tree
Worst case height of binary search tree: N
Insertion, deletion can be O(N) in the worst case
We want a tree with small height
Height of a binary tree with N node is at least (log N)
Goal: keep the height of a binary search tree O(log N)
Balanced binary search trees
Examples: AVL tree, red-black tree
Balanced Tree?
Suggestion 1: the left and right subtrees of root have the same height
Suggestion 2: every node must have left and right subtrees of the same height
Our choice: for each node, the height of the left and right subtrees can differ at
most 1,-1,0.
AVL Tree
An AVL (Adelson-Velskii and Landis 1962) tree is a binary search tree in which
for every node in the tree, the height of the left and right subtrees differ by at most 1.
N1 = 1 N2 = 2 N3 =4 N4 = N2+N3+1=7
Height of AVL Tree
Denote Nh the minimum number of nodes in an AVL tree of height h
N1=1, N2 =2 (base)
Nh= Nh-1 + Nh-2 +1 (recursive relation)
many operations (i.e. searching) on an AVL tree will take O(log N) time
AVL Tree
INSERTION
Insertion in AVL Tree
Basically follows insertion strategy of binary search tree
But may cause violation of AVL tree property
Restore the destroyed balance condition if needed
6 8
6
Insert 6 Restore AVL property
Original AVL tree
Property violated
Some Observations
After an insertion, only nodes that are on the path from the insertion point
to the root might have their balance altered
Because only those nodes have their subtree altered
Rebalance the tree at the deepest such node guarantees that the entire tree
satisfies the AVL property
6 8
6 Rebalance node 7
Node 5,8,7 might guarantees the whole tree be AVL
have balance altered
Different Rebalancing Rotations
LL Rotation The new node is inserted in the left sub-tree of the left sub-tree of the critical
node.
Different Rebalancing Rotations
LL Rotation
Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
RR Rotation The new node is inserted in the right sub-tree of the right sub-tree of the critical
node.
Different Rebalancing Rotations
RR Rotation
Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
LR Rotation The new node is inserted in the right sub-tree of the left sub-tree of the critical
node.
Different Rebalancing Rotations
LR Rotation
Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
RL Rotation The new node is inserted in the left sub-tree of the left sub-tree of the
critical node.
Different Rebalancing Rotations
RL Rotation
Courtesy : javatpoint.com/avl-tree
AVL Tree Construction
63, 9, 19, 27, 18, 108, 99, 81
Courtesy : javatpoint.com/avl-tree
AVL Tree Construction
Courtesy : javatpoint.com/avl-tree
AVL Tree Construction
Courtesy : javatpoint.com/avl-tree
1. Create AVL for following:
43, 10, 79, 90, 12, 10,54, 11, 9, 50
Solution step1:
Final Answer:
2. CREATE AVL:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81, 68, 61,51,62
Final Answer:
3. Create a AVL tree with following data by inserting the following
elements in order of their occurrence.
200,150,350,100,70,110,250,500,400,550,450
4. Create a BST tree with following data by inserting the following
elements in order of their occurrence. [5 marks]
100,50,250,110,45,150,65,30,38,260,150,120,135
Insertion Analysis
Insert the new key as a new leaf just as in ordinary binary search tree:
O(log n)
Then trace the path from the new leaf towards the root, for each
node x encountered: O(log n)
Check height difference: O(1)
If satisfies AVL property, proceed to next node: O(1)
If not, perform a rotation: O(1)
10 35 15 35
5 15 25 40 10 18 25 40
18 30 38 45 30 38 45
10 18 25 40 15 25 38 45
30 38 45 10 18 30 50
There are three different types of rotations based on the balanced factor of
node B.