Avl 1
Avl 1
• Debjit Mitra
• Jeet Saha
• Soham Das
• Sounava Ghosh
• Suraj Kumar Chouhan
AVL Tree
3
Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- Z > Y > X
BF=> 2-0 =2
Z
Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- Z > Y >
X
Z
Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- Z > Y >
X
BF=> 1-1 = 0
Y
X Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- X < Y < Z
Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- X < Y < Z
BF => 0-2 = -2
X
Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- X < Y < Z
Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}
• For Example – Z, Y,
• X
Assuming- X < Y < Z
BF => 1-1 = 0
Y
X Z
LEFT-RIGHT ROTATION
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.
We first perform the left rotation on the left subtree of C. This makes A, the left subtree of B.
Node C is still unbalanced, however now, it is because of the left-subtree of the left-subtree.
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.
SOLUTION-
50
Tree is Balanced
SOLUTION-
• As 20<50, so insert
50
20 in 50’s left sub
tree.
20
Tree is Balanced
SOLUTION-
• As 60>50, so insert
60 in 50’s right sub 50
tree.
20 60
Tree is Balanced
SOLUTION-
10
Tree is Imbalanced
SOLUTION-
50 50
RR Rotation
1
20 60 60
0
1
0 8 20
8 Tree is balanced
Tree is Imbalanced
SOLUTION-
Step- 6: Insert 15
15
Tree is Imbalanced
SOLUTION-
• Find the first imbalanced node on
the path from the newly inserted
node (node 15) to the root node.
• The first imbalanced node is node
50.
• Now, count three nodes from node
50 in the direction of leaf node.
• Then, use AVL tree rotation to
balance the tree.
50
20
1
LR Rotation
60 1
0 50
0
8 20
1
8 60
5
1
5
Tree is balanced
Tree is Imbalanced
SOLUTION-
Step- 7: Insert 32
10 50
8 15 32 60
Tree is balanced
SOLUTION-
Step- 8: Insert 46
46
Tree is balanced
SOLUTION-
Step- 9: Insert 11
11 46
Tree is balanced
SOLUTION-
Step- 10: Insert 48
11 46
48
Tree is imbalanced
SOLUTION-
• Find the first imbalanced node on
the path from the newly inserted
node (node 15) to the root node.
• The first imbalanced node is node
50.
• Now, count three nodes from node
50 in the direction of leaf node.
• Then, use AVL tree rotation to
balance the tree.
20
20
10 50 LR Rotation
10 50
8 15 32 60
8 15 46 60
11 46
11
32 48
48
11 32 48
Tree is balanced
DELETION OF A NODE IN AVL TREE
BALANCED TREE
1
8 46 60
5
11 32 48
SOLUTION-
1
50
0
1
8 46 60
5
32 48
SOLUTION-
BALANCED TREE
• Normally when we
delete a node , we
need to rotate the 20
tree for balancing but
here after deletion of
node 11, the tree is 1
50
not imbalanced. So 0
this is the final tree
after deleting node
1
11. 8 46 60
5
32 48
Tree is balanced
Pros And Cons Of AVL Trees
Arguments for AVL trees: Arguments against using AVL
trees:
• Search is O(log N) since • Difficult to program & debug;
AVL trees are always more space for balance factor.
balanced. • Asymptotically faster but
• Insertion and deletions are rebalancing costs time.
also O(log n) • Most large searches are done in
• The height balancing adds no database systems on disk and use
more than a constant factor other structures (e.g. 8-trees).
to the speed of insertion. • May be OK to have O(N) for a
single operation if total run time
for many consecutive operations
is fast (e.g. Splay trees).
Thank you