2. First-invented self-balancing binary search
tree
Named after itstwoinventors,
1. G.M. Adelson-Velsky and
2. E.M. Landis,
› published it in their 1962 paper "An
algorithm for the organization of
information."
3. • In an AVL tree, the heights of the
two child subtrees of any node differ by at most
one; if at any time they differ by more than one,
rebalancing is done to restore thisproperty.
• An AVL tree is a BST in which each node has
a balance factor (Bf) of +1,0 or-1.
4. • The balance factor of a node is the difference of the
height of the left sub tree ( hL )and height of the
right
subtree(hR)
Bf = hL-hR)
• if a node has a balance factor of +1 then its subtree
is said to be left heavy since the height of left
subtree is greater than then rightsubtree.
• if a node has a balance factor of -1 then its subtree
is said to be right heavy since the height of right
subtree is greater than then leftsubtree.
• If a node has balance factor of 0 then its subtree is
said to bebalanced.
5. First, its a binary
search tree...
L <=P and P <=R
RL
P
6. Is this a binary tree search
tree?
5 17 32 46 53
3 9 15 21 28 34 43 51 52 54 58 68
60 71 79
7712 35 56
29 70
50
7. An AVL tree isa balanced binary tree
Tounderstand balance we need to
understand the notion of TreeHeight
32 71
64 86 Height 0
Height 1
55 Height 2
8. Bydefault, nodes with no children ha
vea height of Height of 0.
71
64 86 Height 0
Height 1
55 Height 2
Height
0
Height 0 32
9. But, we must also understand
theconcept ofSub-trees
71Height 0 32 Height 1
55 Height 2
Height 0 64 86 Height 0
sub-tree Lhas a
height of 0 sub-tree Rhas a
height of 1
Height =max(L.height, R.height) +1
10. Also empty sub-trees have a Height
of -1
Height =max(L.height, R.height) +1
44 Height =2 =max(0, 1) +1
58 Height =1 =max(-1, 0) +1
91 Height =0 =max(-1,-1) +1
11. Anyway, the AVL Balance Property is as
follows...
For ALL nodes, the Height of the Left and
Right Sub-trees can only differ by 1.
P A Node
L R
R.height 1L.height
12. 1. After everyinsertion
2. Check to see if an imbalance was
created.
• All you have to do backtrack up the tree
3. Ifyou find an imbalance, correct it.
4. As long as the original tree is an AVL
tree, there are only 4 types of
imbalances that can occur.
46. LLROTATION
• LL rotation is performed when the node N is
inserted in the left subtree of the left child of the
pivot nodeP.
• In order to rebalance the BSTusing LLrotation ,we
perform following steps:
• 1.the right subtree (XR) of the left child (X) of the
pivot node(P) becomes the left subtree ofP
• 2.Pbecomes right child of nodeX.
• 3.left sub tree (XL) of the node X remains
unchanged.
47. RR ROTATIONS
•RRrotation is performed when a node N is inserted in the
right subtree of the right child of the pivot node P. In order
to rebalance this BST using RR rotation, we perform the
following steps:
•1.Theleft subtree (YL) of the right child (Y) of the pivot
node (P) becomes the right subtree ofP.
• 2.P becomes the left child of nodeY.
•3.Right subtree (YR ) of node Yremainsunchanged.
•After RR rotation ,theAVLtree has the same height s it
had before the insertion ofnode.
48. LRROTATION
• LRRotation is performed when node Nis inserted
in the right subtree of the left child of the pivot
node P.
• In order to explain LRrotation,let usassumenodeX
be the left child of node P,andnode Zbe the right
child of node X.Thefollowing steps are performed
in the LRrotation.
49. ROTATION1:
Theleft subtree of node Z(ZL)becomes the right subtree of
node X.
2.Theleft child (node X)of the pivot node Pbecomesthe
left child of nodeZ
ROTATION2:
3.Theright subtree of node Z(ZR)becomesthe left subtree
of nodeP.
4. Pbecomes the right child of nodeZ.
50. RL
ROTATION
• RL( Right-Left) rotation is just mirror image of LR
rotation. It is performed when a node N is inserted in the
left subtree of the right child of the pivot node P. Like
LR rotation , it is also a double rotation process.
• Let us assume node Y be the right child of node P and
node Z be the left child of node Y. The following steps
are performed in RL rotation:
51. ) becomes the left
Rotation 1
1. The right subtree of node Z
(ZRsubtree of node Y.
2. The right child (node Y) of the pivot node P becomes
the right child of node Z.
Rotation 2
) becomes the right3.The left subtree of node Z(
ZLsubtree of node P.
4. P becomes the left child of node Z.
52. AVL
tree?
YES
Each left sub-tree has
height 1 greater than each
right sub-tree
NO
Left sub-tree has height 3,
but right sub-tree has height
1