You are on page 1of 22

AVL Trees

Version of September 6, 2016

AVL Trees Version of September 6, 2016 1 / 22


Binary Search Trees

x
4 14

2 7 9
<x >x

1 3 12

Binary-search-tree property
For every node x
All keys in its left subtree are smaller than the key value in x
All keys in its right subtree are larger than the key value in x

AVL Trees Version of September 6, 2016 2 / 22


Height
The height of a node in a tree is the number of edges on the
longest downward path from the node to a leaf
Node height 3
= max(children height) +1 8

Leaves: height = 0 2 2
4 14
Tree height = root height
1 0 1
Empty tree: height = −1 2 7 9

Tree operations typically take 0 0 0


1 3 12
O(height) time

Question
Let n be the size of a binary search tree. How can we keep its
height O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22


Balanced Binary Search Tree: AVL Tree
An AVL [Adelson-Velskii & Landis, 1962] tree is a binary search
tree in which
for every node in the tree, heights of its left and right subtrees
differ by at most 1.
3 3
8 8
0 0
2 2 2 2
4 14 4 14
−1 −2 −1 −1
1 0 1 1 0 1 0
2 7 9 2 7 9 16
0 0 1 0 0 1 0
0 0 0 0 0 0
1 3 12 1 3 12
0 0 0 0 0 0
non-AVL Tree AVL Tree

The balance factor of a node is the height of its right subtree


minus the height of its left subtree.
A node with balance factor 1, 0 or −1 is considered balanced.
AVL Trees Version of September 6, 2016 4 / 22
AVL Trees with Minimum Number of Nodes

Let nh be the minimum number of nodes in an AVL tree of height h

n0 = 1 n1 = 2 n2 = n1 + n0 + 1 = 4 n3 = n2 + n1 + 1 = 7

AVL Trees Version of September 6, 2016 5 / 22


Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL tree


of height h
n0 = 1, n1 = 2 (base)
nh = nh−1 + nh−2 + 1
For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2i nh−2i

If h is even, let i = h/2. ⇒ n > 2h/2 n0 = 2h/2 · 1 ⇒


h = O(log n)
If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2 n1 = 2(h−1)/2 · 2
⇒ h = O(log n)
Thus, many operations (e.g., insertion, deletion, and search)
on an AVL tree will take O(logn) time
AVL Trees Version of September 6, 2016 6 / 22
AVL Trees and Fibonacci Numbers
We saw that nh = nh−1 + nh−2 + 1.
Recall Fibonacci numbers satisfy fh = fh−1 + fh−2 . Now compare

h 0 1 2 3 4 5 6 7 8
nh 1 2 4 7 12 20 33 54 88
fh 1 1 2 3 5 8 13 21 34

Lemma:
nh = fh+2 − 1

Proof: by induction

nh+1 = 1+nh +nh−1 = 1+fh+2 −1+nh+1 −1 = fh+3 −1



Since fh ∼ cφh for golden ratio φ = 1+2 5 , this also immediately
provides alternative derivation that h = O(log n).
AVL Trees Version of September 6, 2016 7 / 22
Insertion

Basically follows insertion strategy of binary search tree


But may cause violation of AVL tree property
Restore the destroyed height balance if needed

8 8 8

4 14 4 14 2 14

2 9 2 9 1 4 9

1
Insert 1 Restore height balance

AVL Trees Version of September 6, 2016 8 / 22


Insertion: Observation

After an insertion, only nodes that are on the path from the
insertion node to the root might have their balance altered
Because only those nodes have their subtrees altered

8 8
0 −1
4 14 4 14
−1 −1 −2 −1
2 9 2 9
0 0 −1 0

1
0
Insert 1

AVL Trees Version of September 6, 2016 9 / 22


Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)


— insert into the left subtree of the left child of A
Case 2 (Left-Right case)
— insert into the right subtree of the left child of A
Case 3 (Right-Left case)
— insert into the left subtree of the right child of A
Case 4 (Right-Right case)
— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,


as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22


Insertion: Left-Left Case

k+2 k+3
A A
−1 −2
B B
0 k −1 k
γ γ

k k k
α β k+1 α β

• Right rotation with B as the


pivot k+2
B
• The new subtree rooted at B has 0
height k + 2, exactly the same A
height before the insertion k+1 0
α
• The rest of the tree (if any) that k k
was originally above node A β γ
always remains balanced

AVL Trees Version of September 6, 2016 11 / 22


Insertion: Right-Right Case

k+2 k+3
A A
1 2
B B
k 0 k 1
α α

k k k k+1
β γ β γ

• Left rotation with B as the pivot k+2


B
• The new subtree rooted at B has 0
height k + 2, exactly the same A
height before the insertion 0
k+1 γ
• The rest of the tree (if any) that k k
was originally above node A α β
always remains balanced

AVL Trees Version of September 6, 2016 12 / 22


Insertion: Left-Right Case

k+2 k+3
A A
−1 −2
B B
0 k 1 k
γ γ

k k k k+1
α β α β

k+3
B
• Single rotation fails to fix it 2
A
• Subtree β is too tall k -1
α
k+1 k
β γ

AVL Trees Version of September 6, 2016 13 / 22


Left-Right Case: Special Case
When subtree α, β and γ are empty, k = −1. Insert C :
2
1 A
A −2
−1
B
B
1
0
C
0

A 2
−2 1
C
C
0
−1
B A
B 0 0
0

Left rotation and then right rotation with C as the pivot.


Done!
AVL Trees Version of September 6, 2016 14 / 22
Left-Right Case: General Case 1
k+2 k+3
A A
−1 −2
B B
0 k 1 k

X X
0 δ −1 δ
k k

k−1
α α
β γ k−1 γ k−1

β k

A k+3
−2 k+2
X
X 0
−2 k B A
B 0 1
0 k−1
δ k−1
γ k
k γ
k
α β k δ

α β k

AVL Trees Version of September 6, 2016 15 / 22


Left-Right Case: General Case 2
k+2 k+3
A A
−1 −2
B B
0 k 1 k

X X
0 δ 1 δ
k k

k−1
α α k−1
β γ k−1 β
k
γ
A k+3
−2 k+2
X
X 0
−1 k B A
B −1 0
−1 δ
k k−1
k−1 β
k k
k β γ
α γ k δ
α

AVL Trees Version of September 6, 2016 16 / 22


Right-Left Case: Special Case
When subtree α, β and γ are empty, k = −1. Insert C :

2
1 A
A
2
1
B
B
−1
0
C
2 0
A
2
C 1
C
1
0
B A B
0 0 0

Right rotation and then left rotation with C as the pivot.


Done!
AVL Trees Version of September 6, 2016 17 / 22
Insertion: Summary

Left-Left case: single rotation


— Right rotation

Left-Right case: double rotation


— Left rotation and then right rotation

Right-Left case: double rotation


— Right rotation and then left rotation

Right-Right case: single rotation


— Left rotation
For each insertion, at most two rotations are needed to restore the
height balance of the entire tree.
Note that in all cases, height of rebalanced subtree is unchanged!
This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22


Deletion

Delete a node as in ordinary binary search tree


If the node is a leaf, remove it
If not, replace it with either the largest in its left subtree or
the smallest in its right subtree, and remove that node
Note: The replacement node has at most one subtree
Trace the path from the parent of removed node to root
For each node along path, restore the height balance if needed
by doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22


Deletion (continued)

For each node along the path, restore the height balance if needed
by doing single or double rotations
Very similar to insertion with one major caveat
In insertion a (single/double) rotation restored balance and
kept height of rebalanced subtree unchanged.
⇒ Only one rotation needed.
In deletion, rotation restores balance but final height of rotated
subtree might decrease by one. If this occurs, need to continue
walking up path towards root, continuing to restoring balance
by rotations when necessary.
Since path has length O(h) this might require O(h) =
O(log n) rotations.
⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22


Deletion Example

Diagram below illustrates example in which subtree rooted at A


has height k + 3. An item is deleted from subtree γ, reducing its
height from k + 1 to k, leading to an imbalance.
After a single rotation, the subtree is now rooted at B with no
imbalance. But, B has height k + 2. This might cause an
imbalance further up the tree, so the algorithm might need to
continue walking upwards, correcting that imbalance.

k+3 B k+2
A
−1 0
B A 0
-1 k+1
γ
k+1
α
k+1 k k k
α β β γ

AVL Trees Version of September 6, 2016 21 / 22


Going Further

AVL trees are one particular type of Balanced Search trees, yielding
O(log n) behavior for dictionary operations.
There are many other types of Balanced Search Trees, e.g.

red-black trees
B-trees
(a, b) trees
(2, 3) and (2, 3, 4) trees are special cases
treaps (randomized BSTs)
splay Trees (only O(log n) in amortized sense)

AVL Trees Version of September 6, 2016 22 / 22

You might also like