Professional Documents
Culture Documents
10 - IT623 Algorithms & Data Structures-TreeAVL
10 - IT623 Algorithms & Data Structures-TreeAVL
Structures
AVL Trees
Notes referred from Prof Anish Maturia’s lectures
Balanced Binary Search Trees
• What is the maximum height of a n-node
binary search tree?
• Balanced binary search trees add additional
balance condition
• Maximum height is logarithmic in the number
of nodes
Balance
• Balance t
• height(left subtree) - height(right subtree)
• zero everywhere perfectly balanced
• small everywhere balanced enough
1
2 2
1 -2
1 -2 0
0 -1
0 1
0
0
AVL trees: find, insert
• AVL tree find is the same as BST find
• AVL insert: same as BST insert, except that we
might have to “fix” the AVL tree after an insert
-2
Insert(small) S
Insert(middle)
Insert(tall) -1
M
0
T
AVL Tree Insert
• Let x be the deepest node where an imbalance occurs
• Four cases to consider. The insertion is in the
• left subtree of the left child of x
• right subtree of the left child of x
• left subtree of the right child of x
• right subtree of the right child of x
Idea: Cases 1 & 4 are solved by a single rotation
Cases 2 & 3 are solved by a double rotation
Single rotation example
15
x
5 20
3 10 17 21
2 4
1
3 10 17 21
2 4
1 15
3 20
2 5 17 21
1 4 10
Basic operation used in AVL trees:
A left child could legally have its parent as its right child !
Single rotation in general
a height h+2
before insert
b
h
Z
h
h+1 Y
X h -1
X<b<Y<a<Z
height h+2
b after insert
a
h+1 h h
X Y Z
Cases 2 & 3
a height h+2
before insert
b
h
Z
h
X h+1
Y h -1
b
a
h
X
h+1 h
Z
Y
4 10 16
3 6
5
15
8 17
6 10 16
4
3 5
Double rotation, step 2
15
8 17
6 10 16
4
3 5
15
6 17
4 8 16
3 5 10
Double rotation in general
height h+2
before insert
a
b
h0 c h
Z
h h-1
W h Y
X
h h h-1 h
Y
W X Z
A sequence of example insertions
Insert 3, 2, 1, 4, 5, 6, 7
After inserting 1
Rotate right
After inserting 3 After inserting 2
3 3 3
2 2
1 2
1 3
1 3
After inserting 4
Rotate left
2 2
1 3 1 4
4 3 5
2 4
1 4 2 5
3 5 1 3 6
2 5
1 3 6
After inserting 7
7
4
2 6
1 3 5 7
After singe left rotation
between 5 and 6.
Example
• Insert 16, 15, 14, 13, 12, 10, and 8, and 9 to the tree
obtained in the previous example
2 6
1 3 5 7
After inserting 16 16
4
After inserting 15 2 6
Case 3
1 3 5 7 k1
16 k3
15 k2
4
After right-left double rotation
among 7, 16, 15
2 6
1 3 5 15
7 16
4
k1
After inserting 14 2 6
Case 3
k3
1 3 5 15
k2 7 16
14
4
After right-left double rotation
among 6,15,7 k2
2 7
k1 k3
1 3 6 15
5 14 16
4 k1
k2
2 7
5 14 16
7 13
After single left rotation
between 4 and 7
4 15
2 6 14 16
1 3 5 13
7
After inserting 12
4 15
2 6 14 16
k2
1 3 5 13 k
1
4 15
2 6 13 k1 16
1 3 5 12 14 k2
7
4 15 k2
After inserting 11
2 6 13 k1 16
1 3 5 12 14
11 7
4 13
1 3 5 11 14 16
7
13
After inserting 10
4
2 6 12 15
1 3 5 11 14 16
10 7
4 13
1 3 5 10 12 14 16
7
4 13 After inserting 8
2 6 11 15
1 3 5 10 12 14 16
8
7
4 13 After inserting 9
2 6 11 15
1 3 5 10 12 14 16
7
8
4 13
9
2 6 11 15
1 2
5 15
0 0 0 1
2 9 12 20
0 0
17 30
Rotation
• Our basic operation for balancing is called rotation:
y right-rotate(y) x
x C A y
left-rotate(x)
A B B C
x := left[y]
left[y] := right[x] x’s right child becomes y’s left child
p[right[x]] := y
p[x] := p[y] y’s parent becomes x’s parent
right[x] := y y becomes x’s right child
p[y] := x x becomes parent of y
if p[y] ≠ nil Bug
then
if y = right[p[y]] if y is the right (left) child of
then right[p[y]] := x its parent then x becomes right
else left[p[y]] := x (left) child of y’s parent
return x
y x
x C A y
A B B C
Algorithm right-rotate(y):
{ Performs a right rotation on a given node y of a BST. Assumes left
child of y is not nil. }
x := left[y]
left[y] := right[x] x’s right child becomes y’s left child
p[right[x]] := y
p[x] := p[y] y’s parent becomes x’s parent
right[x] := y y becomes x’s right child
if p[y] ≠ nil
then
if y = right[p[y]] if y is the right (left) child of
then right[p[y]] := x its parent then x becomes right
else left[p[y]] := x (left) child of y’s parent
p[y] := x x becomes parent of y
y x
x C A y
A B B C
Algorithm left-rotate(x):
{ Performs a left rotation on a given node x of a BST. Assumes right
child of x is not nil. }
y := right[x]
right[x] := left[y] y’s left child becomes x’s right child
p[left[y]] := x
p[y] := p[x] x’s parent becomes y’s parent
left[y] := x x becomes y’s left child
if p[x] ≠ nil
then
if x = left[p[x]] if x is the left (right) child of
then left[p[x]] := y its parent then y becomes left
else right[p[x]] := y (right) child of x’s parent
p[x] := y y becomes parent of x
y x
x C A y
A B B C
Algorithm balance(x):
{ Takes a subtree rooted at x whose left and right subtrees are height
balanced. Returns the root of the height balanced subtree after the
rotations. }
if |height(right[x]) – height(left[x]))| ≤ 1
then
return x
if height(left[x]) > height(right(x))
then
y := left[x]
if height(right[y]) > height(left[y])
then left-rotate(y)
right-rotate(x)
else
y := right[x]
if height(left[y]) > height(right[y])
then right-rotate(y)
left-rotate(x)
return p[x]
Algorithm avl-insert(x, z):
{ x is a pointer to the root of an AVL tree. z is a pointer to a new
node to be inserted into the tree. Returns a pointer to root of the
tree after insertion. }
if x := nil
then
height[z] := 0
return z
parent[y] := x
height[x] := height[y] + 1
return balance(x)