AVL
Trees / Slide 4
AVL Tree
An AVL
tree is a binary search tree in which
for every node
in the tree, the height right subtrees diff er by at most 1.
Height
of subtree:
Height
of an
Height AVL tree
Max #
of edges to
empty subtree: -1
of one
of the
node: 0 AVL property violated here
lef t and
a leaf
AVL
Trees / Slide 5
AVL Tree with Minimum Number of Nodes
N0 = 1
N1 = 2
height of left=? Height right=?
N2 =4
N3 = N1+N2+1=7
AVL
Trees / Slide 6
Smallest
AVL tree of height 7
Smallest
Smallest
AVL tree of height 8
AVL tree of height 9
AVL
Trees / Slide 7
Height
of AVL Tree
Denote Nh the minimum tree of height h
number of nodes in an AVL
N1 =2 (base) Nh= Nh-1 + Nh-2 +1 (recursive relation)
N0=0,
N
> Nh= Nh-1 + Nh-2 +1 >2 Nh-2 >4 Nh-4 >«>2i Nh-2i
If h is even, let i=h/2±1. The equation becomes N>2h/2-1N2
N>2h/2-1x4 h=O(logN) If h is odd, let i=(h-1)/2. The equation becomes N>2(h-1)/2N1 N>2(h-1)/2x2 h=O(logN)
Thus, many operations (i.e. searching) on an AVL tree will take O(log N) time
AVL
Trees / Slide 8
Insertion
Basically follows insertion strategy search tree
in AVL Tree
But may cause vi olation
of AVL
of binary
tree pr operty
Restore the destr oyed balance c ondition if needed
7 6
8
6
Original AVL tree
Insert 6 Property violated
Restore AVL property
AVL
Trees / Slide 9
Some Observations
Af ter
an insertion, only nodes that are on the path f ro m the insertion point to the r oot might have their balance altered
Because only those nodes have their subtrees altered
Rebalance the tree at the deepest such node guarantees that the entire tree satis f ies the AVL pr operty
7 6
8
6
Node 5,8,7 might have balance altered
Rebalance node 7 guarantees the whole tree be AVL
AVL
Trees / Slide 10
Different
Denote
Cases for Rebalance
the node that must be rebalanced
Case 1: an insertion into the lef t subtree child of
of the
Case 2: an insertion into the right subtree child of
Case 3: an insertion into the lef t subtree child of
Case 4: an insertion into the right subtree right child of
lef t
of the
of the
right
of the
Cases 1&4 are mirr or image symmetries with respect to , as are cases 2&3
lef t
AVL
Trees / Slide 11
Rotations
Rebalance of AVL tree are d one with simple modif ication to tree, known as r otation
Insertion occurs on the ³outside´ (i.e., lef t-lef t or right-right) is f ixed by single r otation of the tree
Insertion occurs on the ³inside´ (i.e., lef t-right or right-lef t) is f ixed by double r otation of the tree
AVL
Trees / Slide 12
Insertion
Algorithm
First, insert the new key as a new lea f just as in ordinary binary search tree
Then trace the path f r om the new lea f towards the r oot. For each node x encountered, check i f heights of lef t(x) and right(x) di ff er by at m ost 1
If yes, pr oceed to parent(x)
If not, restructure by doing either a single r otation or a double r otation
Note: once we per form a r otation at a node x, we won¶t need to per form any r otation at any ancestor of x.
AVL
Trees / Slide 13
Single Rotation to Fix Case 1(left-left) k2 violates
An
insertion in subtree X,
AVL pr operty
violated at node k2
AVL-pr operty 1. 2.
Solution: single r otation
quiz: Can Y have the same height as the new X? Can Y have the same height as Z?
AVL
Trees / Slide 14
Single Rotation Case 1 Example
k2
k1
X
k1
X
k2
AVL
Trees / Slide 15
Single Rotation to Fix Case 4 (right-right) k1 violates
An
insertion in subtree Z
Case 4 is a symmetric case t o case 1
Insertion takes O(Height of AVL Tree) time, Single r otation takes O(1) time
AVL
Trees / Slide 16
Single Rotation Example
Sequentially insert 3, 2, 1, 4, 5, 6 t o an AVL Tree 3 2
3 2
1
2 1
Insert 3, 2
2 3
3
1
Single r otation
Insert 4
Insert 1 violation at node 3 2
2 3
1
Insert 5, 4 violation at node 3
4
2 4
1 3
Single r otation
4
1 5
5
4 5
2 3
5
Insert 6, violation at node 2
1 6
3
Single r otation
6
AVL
Trees / Slide 17
If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 4
4 5
2 1
1
6
3
6
2
Insert 7, violation at node 5
7
3
Single r otation 4
4
3
5
Insert 16, f ine Insert 15 violation at node 7
6
2
6
2 1
7
5
1
7
3
Single r otation 16 15
But«. Violation remains
5
What is the result?
AVL
Trees / Slide 18
Single Rotation Fails to fix Case 2&3
Case 2: violation in k2 because of insertion in subtree Y
Single r otation result
Single r otation f ails to f ix case 2&3
Take case 2 as an example (case 3 is a symmetry to it )
The pr oblem is subtree Y is t oo deep
Single r otation doesn¶t make it any less deep
AVL
Trees / Slide 19
Single Rotation Fails
What
We
shall we do?
need to r otate twice
Double
Rotation