2. AVL TREES
(Adelson-Velskii and Landis 1962)
AVL tree is a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees cannot be
more than one for all nodes.
Key idea: if insertion or deletion get the tree out of balance then fix
it immediately
All operations insert, delete,… can be done on an AVL tree with N
nodes in O(log N) time (average and worst case!)
3. Example:
An example of an AVL tree where the
heights are shown next to the nodes:
88
44
17 78
32 50
48 62
1
3
0
0
1
2
0
0
4. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
5. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
44
17 78
32 50 88
48 62
6. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
44
17 78
32 50 88
48 62
7. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
44
17 78
32 50 88
48 62
54
Insert node 54
44
17 78
32 50 88
48 62
8. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
9. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
10. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1 0
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
11. Insertion in an AVL Tree
Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1 0
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
Unbalanced!!
12. How does the AVL tree work?
First, insert the new key as a new leaf just as in
ordinary binary search tree
Then trace the path from the new leaf towards the
root. For each node x encountered, check if heights
of left(x) and right(x) differ by at most 1.
If yes, proceed to parent(x). If not, restructure by
doing either a single rotation or a double rotation
[next slide].
For insertion, once we perform a rotation at a node x,
we won’t need to perform any rotation at any
ancestor of x.
13. Rotations
Two types of rotations
Single rotations
two nodes are “rotated”
Double rotations
three nodes are “rotated”
21. Single Rotation
Sometimes a single rotation fails to solve the problem
k2
k1
X
Y
Z
k1
X
Y
Z
k2
h+2
h
h
h+2
In such cases, we need to use a double-rotation
27. A B1 B2 C
x
y
z
Tree has this form
After Double Rotation
28.
29. Insertion
The time complexity to perform a rotation is O(1)
The time complexity to find a node that violates the
AVL property is dependent on the height of the tree,
which is log(N)
30. Deletion
Perform normal BST deletion
Perform exactly the same checking as for insertion to
restore the tree property
31. Summary AVL Trees
Maintains a Balanced Tree
Modifies the insertion and deletion routine
Performs single or double rotations to restore structure
Guarantees that the height of the tree is O(logn)
The guarantee directly implies that functions find(), min(), and
max() will be performed in O(logn)
32. Summary AVL Trees
Requires a little more work for insertion and
deletion
But, since trees are mostly used for searching
More work for insert and delete is worth the performance
gain for searching
34. Self-adjusting Structures
Consider the following AVL Tree
44
17 78
32 50 88
48 62
Suppose we want to search for the following sequence of
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
35. Self-adjusting Structures
Consider the following AVL Tree
44
17 78
32 50 88
48 62
Suppose we want to search for the following sequence of
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
In this case,
is this a good structure?
36. Self-adjusting Structures
Splay Trees (Tarjan and Sleator 1985)
Binary search tree.
Every accessed node is brought to the root
Adapt to the access probability distribution