Lecture 11
Lecture 11
1
Height of a Node
3
1 0 2
0 0 0 0
1
0 0
3
AVL Trees
• AVL tree is defined as a tree such that, for
each node
• Balanced factor 1, 0, -1
• Duplicate elements are not allowed in BST
• BST (1,2,3,4-> right skewed)
• BST (4,3,2,1-> left skewed)
• Both above cases O(n)
• Make them AVL and now O(log n)
4
AVL Trees
• LL (rotate clock wise)(Single Rotation)
• RR (rotate anti clock wise) (Single Rotation)
• LR->LL-> (Two Rotation)
• RL->RR-> (Two Rotation)
• BT contains BST contains AVL tree
• Balanced BST -> O(log n)
• Make BST balanced -> AVL Tree
5
AVL-Trees
G. M. Adelson-Velskii and E. M. Landis, 1962
1 or less
6
AVL-Trees
An AVL-tree is a BST with the property that at
every node the difference in the depth of the left
and right subtree is at most 1.
not OK
OK
7
Problem
8
But How?
9
But How?
rotate right on y
x
y
y
x
C
A
B B C
A
rotate left on x 10
So?
11
Well, not quite
12
Tree Balance Rotations
• Note that after an insertion only the nodes
in the path from root to the node have their
balance information altered
• Find the node that violates the AVL
condition and rebalance the tree.
• Assume that X is the node that has
violated the AVL condition
– I.e The left and right sub trees of X is differ by
2 in height.
13
Four Cases – Case I
Case I – The left subtree of the left child of X violates the
property. Rotate RIGHT
y
X
y X
C
B
C
A A
B
14
Four Cases – Case 2
Case 2 – The right subtree of the right child of X violates
the property. Rotate LEFT
X y
X
y
C
B
A A
C B
15
Four Cases – Case 3
Case 3 – The right subtree of the left child of X violates the
property X
X Rotate the tree LEFT
about X’s child and
Grandchild
Z
y
y A
Z A
D B
D C
C B
Ctd.. 16
Four Cases – Case 3 ctd..
y X
Z
y A
D C
B A
B
D C
17
Four Cases – Case 4
Case 4 – The left subtree of the right child of X violates the
property
Rotate the tree RIGHT X
X about Y and Z
y D Z
D
y
Z
A
C B A
C B
18
Ctd..
Four Cases – Case 4 ctd..
X y
Z
D
y
D
C B A
C
B A
19
AVL Tree Examples
• Insert 12, 8, 7, 14, 18, 10, 20 with AVL
rotations
20
AVL Summary
• Insert a new node in left or right subtree.
• Test the height information along the path
of the insertion. If not changed, we are
done
• Otherwise do a single or double rotation
based on the four cases
21
Homework/Reading Assignment
22
Depth of a Node
0
1 1 1
2 2 2
1 2 3 4
AVL Tree
• Recall height of empty tree = -1
• In AVL tree, For all nodes, height of left and right subtrees
differ by at most 1.
• AVL trees have logarithmic height
• Fibonacci numbers: F[1]=1; F[2]= 1; F[3]=2; F[4]=3;
• Induction Strikes: Thm: S[h] >= F[h+3]-1
Let S[i] = size of smallest AVL tree of height i
S[0] = 1; S[1]=2; why?
So S[1] >= F[4]-1
S[h]=S[h-1]+S[h-2]+1 >=F[h+2]-1+F[h+1]-1+1
= F[h+3]-1.
• Hence number of nodes grows exponential with height.
On Insertion, what can go
wrong?
• Tree balanced before insertion
1 2
0 1 1
1
H-1
H
Insertion
• After insertion, there are 4 ways tree can
be unbalanced. Check it out.
• Outside unbalanced: handled by single
rotations
• Inside unbalanced: handled by double
rotations. 2 2
1 1
c r
p
b
a
q
Maintaining Balance
• Rebalancing: single and double rotations
• Left rotation: after insertion
1
2
2
1
c
b a b c
a
2 Another View 1
2
a
1 c Left
b c
a b
1 Right 2
a 2 1
c
a b
b c
1 Right 2
a 2 1
c
a b
b c
2
3
1 3
1
2
3
In Steps 3
d 2 d
1
c
2 1
a
a b
b c
2
3
1
c d
b
a
Double Rotation Code (left-
right)
• Idea: rotate left child with its right child
• Then node with new left child
• static BinaryNode doubleLeft( BinaryNode n)
n.left = rotateRight(n.left);
return rotateLeft(n)
• Analogous code for other middle case
• All rotations are O(1) operations
• Out-of-balance checked after insertion and after
deletions. All O(1).
• For AVL, d is O(logN) so all operations O(logN).