Lecture 16 AVL Trees
Lecture 16 AVL Trees
Lecture outline
1
18-Apr-23
AVL Trees
“AVL trees are binary search trees (BSTs) in which the difference
between the height of the left and right subtree is either -1, 0, or
+1.”
AVL trees are also called a self-balancing binary search trees.
These trees help to maintain the logarithmic search time. They
are named after their inventors (AVL) Adelson, Velsky, and
Landis.
2
18-Apr-23
AVL Trees
AVL Trees
Here, the keys are the same,
but since they are inserted
in a different order, they
take different positions, and
the height of the tree
remains balanced. Hence
search will not take more
than O(logn) for any
element of the tree.
7
3
18-Apr-23
AVL Trees
4
18-Apr-23
10
10
11
5
18-Apr-23
12
12
13
13
6
18-Apr-23
Left Rotation
Pre-requisite: Right subtree of root can not be NULL.
Steps:
1. Parent(root)->root = pivot
2. root->right = pivot->left
3. pivot->left = root
14
14
Right Rotation
Pre-requisite: Left subtree of root can not be NULL.
Steps:
1. Parent(root)->root = pivot
2. root->left = pivot->right
3. pivot->right = root
15
15
7
18-Apr-23
16
17
8
18-Apr-23
18
18
19
19