





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of avl trees, a self-balancing binary search tree. Avl trees maintain a balance property where the height of the left and right subtrees of any node can differ by at most 1. The concept of avl trees, their advantages, and the methods to maintain balance through single and double rotations.
Typology: Study notes
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























An AVL tree 1 Not an AVL tree
(look at node 8, 12)
Insert 3.
5
3
1
4
8
x
y
A z
1
3
8
After Rotation
The rotation takes
The rotation takes O
time. Notice that the new tree is a legal
time. Notice that the new tree is a legal
search tree.
search tree.
For insert - it must be the case that subtree
For insert - it must be the case that subtree A
had been increased,
had been increased,
so after the rotation, the tree has height as before the insert.
so after the rotation, the tree has height as before the insert.
For delete, it must be the case that
For delete, it must be the case that C
had been decreased, so after
had been decreased, so after
the rotation, the tree has height shorter by 1.
the rotation, the tree has height shorter by 1.
k
2
k
1
right rotation
k
2
k
1
k
1
k
2
left rotation
k
1
k
2
Deleting X and performing a single rotation:
For the rotation, k
1
is node A, and k
2
is node B. We
make k
2
the root, and k
1
its left son.
Notice that the tree height has changed after the
rotation (since it was a delete operation).
Example (caused by insertion)
k 3 k 1 D A B k 2 Left rotation on 4 k 1
k 3 D A B k 2
Right rotation on 8
k k 3 1 B D k 2 A Note that above is a good friend of case 1
After insertion - original height (of the root) stays the same.
After insertion - original height (of the root) stays the same.
k
3
k
1
k
2
B C
k
3
k
1
k
2
B C
k
3
k
1
k
2
B
C
left rotation
on k
1
right rotation
on k
3