Title: AVL Tree: Balanced Binary Search Tree
1AVL Tree Balanced Binary Search Tree
6
2
8
4
9
1
3
2AVL Tree Balanced Binary Search Tree
BST Property At every node X, values
in left subtree are smaller than the value in X,
and values in right subtree are larger than the
value in X.
6
2
8
4
9
1
3
3AVL Tree Balanced Binary Search Tree
BST Property At every node X, values
in left subtree are smaller than the value in X,
and values in right subtree are larger than the
value in X.
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
6
2
8
4
9
1
3
4AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
2
8
4
9
1
3
5AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
Is this is an AVL tree?
2
8
4
9
1
3
6AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
2
8
4
9
0
1
0
0
3
7AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
1
2
8
4
9
0
1
1
0
0
3
8AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
1
2
2
8
4
9
0
1
1
0
0
3
9AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
Yes, this is an AVL tree.
1
2
2
8
4
9
0
1
1
0
0
3
10AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
Suppose we delete the 9 node.
1
2
2
8
4
9
0
1
1
0
0
3
11AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
0
2
2
8
4
1
1
0
0
3
12AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
AVL balance property fails at 6 node.
0
2
2
8
4
1
1
0
0
3
13Tree Rotations to Restore Balance
j
Suppose we have balance . . .
k
Z
X
Y
14Tree Rotations to Restore Balance
j
Suppose we have balance . . .
k
Z
Y
X
. . . but we insert here, destroying the balance.
15Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
16Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
17Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
18Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
19Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
20Tree Rotations to Restore Balance
Do a single rotation
k
j
X
Z
Y
21Tree Rotations to Restore Balance
Do a single rotation
k
Done!
j
X
Z
Y
22Tree Rotations to Restore Balance
j
In one step
k
Z
Y
X
23Tree Rotations to Restore Balance
k
j
X
Z
Y
24Tree Rotations to Restore Balance
j
Again, suppose we have balance . . .
k
Z
X
Y
25Tree Rotations to Restore Balance
j
Again, suppose we have balance . . .
k
Z
X
Y
. . . but this time we insert here.
26Tree Rotations to Restore Balance
j
Again, we do a single rotation . . .
k
Z
X
Y
27Tree Rotations to Restore Balance
Again, we do a single rotation . . .
k
. . . but it fails to restore balance
j
X
Z
Y
28Tree Rotations to Restore Balance
j
Go back to where we started . . .
k
Z
X
Y
29Tree Rotations to Restore Balance
j
Go back to where we started . . .
k
Z
X
. . . and consider structure of Y
Y
30Tree Rotations to Restore Balance
j
k
Z
i
X
V
W
31Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
32Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
33Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
34Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
35Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
36Tree Rotations to Restore Balance
Now do a double rotation. . .
i
j
k
Z
V
W
X
37Tree Rotations to Restore Balance
Now do a double rotation. . .
i
j
k
Done!
Z
V
W
X