6.006 AVL Tree
6.006 AVL Tree
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Lecture 4 Balanced Binary Search Trees 6.006 Spring 2008
Lecture Overview
• The importance of being balanced
• AVL trees
– Definition
– Balance
– Insert
Readings
CLRS Chapter 13. 1 and 13. 2 (but different approach: red-black trees)
– key
– left pointer
– right pointer
– parent pointer
See Fig. 1
3 41
2 20 65 1
29 50 φ
φ 11 1
26
φ
1
Lecture 4 Balanced Binary Search Trees 6.006 Spring 2008
≤x ≥x
• height of node = length (� edges) of longest downward path to a leaf (see CLRS B.5
for details).
vs.
2
Lecture 4 Balanced Binary Search Trees 6.006 Spring 2008
AVL Trees:
Definition
AVL trees are self-balancing binary search trees. These trees are named after their two
inventors G.M. Adel’son-Vel’skii and E.M. Landis 1
An AVL tree is one that requires heights of left and right children of every node to differ
by at most ±1. This is illustrated in Fig. 4)
k-1 k
• Each node stores its height. This is inherently a DATA STRUCTURE AUGMENTATION
procedure, similar to augmenting subtree size. Alternatively, one can just store dif
ference in heights.
A good animation applet for AVL trees is available at this link. To compare Binary Search
Trees and AVL balancing of trees use code provided here .
1
Original Russian article: Adelson-Velskii, G.; E. M. Landis (1962). ”An algorithm for the organization
of information”. Proceedings of the USSR Academy of Sciences 146: 263266. (English translation by Myron
J. Ricci in Soviet Math. Doklady, 3:12591263, 1962.)
Balance:
⇒ Nh = Nh−1 + Nh−2 + 1
> 2Nh−2
⇒ Nh > 2h/2
1
=⇒ h < lg h
2
Alternatively:
AVL Insert:
2. work your way up tree, restoring AVL property (and updating heights as you go).
Each Step:
x
y
y Left-Rotate(x) k
k-1 k+1 x
A k
C
x y
k+1 x
k-1 z k+1 Left-Rotate(x)
A k
C
k B C k k-1 A k B
x y k+1
Right-Rotate(z)
z k x z k
k-1 k+1 Left-Rotate(x)
A
k-1
k y or
k-1 A B C D k-1
D k-1 k-2
k-1
or
B C
k-2
2 20 65 1 20 65 1
50 φ 50 φ
φ 11 1 29 φ 11 2 29
26 1 26
φ
φ 23
Done Insert(55)
3 41 3 41
2 20 65 1
2 20 65 1
50 φ 50 φ
φ 11 1 26 φ 11 1 26
φ 23 29 φ φ 23 29 φ
2 20 2 65 2 20 1 55
50 1 φ 50 φ 65
11 1 26 11 1 26
φ φ
55 φ
φ 23 29 φ φ 23 29 φ
Comment 1. In general, process may need several rotations before an Insert is completed.
Note 1. Skip Lists and Treaps use random numbers to make decisions fast with high
probability.
Note 2. Splay Trees and Scapegoat Trees are “amortized”: adding up costs for several
Splay Trees
Upon access (search or insert), move node to root by sequence of rotations and/or double-
rotations (just like AVL trees). Height can be linear but still O(lg n) per operation “on
average” (amortized)
Optimality
• For BSTs, cannot do better than O(lg n) per search in worst case.
A Conjecture: Splay trees are O(best BST) for every access pattern.
• With fancier tricks, can achieve O(lg lg u) performance for integers 1 · · · u [Van Ernde
Boas; see 6.854 or 6.851 (Advanced Data Structures)]
Big Picture:
Abstract Data Type(ADT): interface spec.
e.g. Priority Queue:
• Q = new-empty-queue()
• Q.insert(x)
• x = Q.deletemin()
vs.
There are many possible DSs for one ADT. One example that we will discuss much later in
the course is the “heap” priority queue.