Degenerate Binary Search Tree
BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17
14
15 4
9
7
18 3
5
16 20
Degenerate Binary Search Tree
BST for 3 4 5 7 9 14 15 16 17 18 20
14
15 4
9 7
18 3
5
16
Degenerate Binary Search Tree
BST for 3 4 5 7 9 14 15 16 17 18 20
Balanced BST
We should keep the tree balanced.
One idea would be to have the left and
Balanced BST
Does not force the tree to be shallow.
14
15
18 16
20 17
4
9 7
3
Balanced BST
We could insist that every node must have left
and right subtrees of same height.
But this requires that the tree be a complete
binary tree
To do this, there must have (2d+1 – 1) data
AVL Tree
AVL (Adelson-Velskii and Landis) tree.
Purpose: AVL is a self-balancing binary tree
This was an effort to propose the development
of a balanced search tree by considering the height as a standard.
An AVL tree is identical to a BST except
§
height of the left and right subtrees can differ by
at most 1.
AVL Tree
An AVL Tree
5
8 2
4
3
1 7
level 0
1
2
AVL Tree
Not an AVL tree
6
8 1
4
3 1
5
level 0
1
2
Balanced Binary Tree
The height of a binary tree is the maximum level
of its leaves (also called the depth).
The balance of a node in a binary tree is defined
as the height of its left subtree minus height of its right subtree.
For example, a tree is a balanced tree if each
Balanced Binary Tree
-1
0 1
0 0
0
0
-1 0
1
0
0 0
0 0
0 0
Balanced Binary Tree
Insertions and effect on balance
-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
U1 U2 U3 U4
U5 U6 U7 U8 U
9 U10 U11 U12
B B
B B
Balanced Binary Tree
Tree becomes unbalanced only if the newly
inserted node
§
is a left descendant of a node that previously
had a balance of 1 (U1 to U8),
§
or is a descendant of a node that previously had
Balanced Binary Tree
Insertions and effect on balance
-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
U1 U2 U3 U4
U5 U6 U7 U8 U
9 U10 U11 U12
B B
B B
Balanced Binary Tree
Consider the case of node that was previously 1
-1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0
U1 U2 U3 U4
U5 U6 U7 U8 U
9 U10 U11 U12
B B
B B
Inserting New Node in AVL Tree
1
0
A
B
T1
T3
Inserting New Node in AVL Tree
2
1
A
B
T1
T3
T2
new
1
Inserting New Node in AVL Tree
2 1 A B T1 T3 T2 new 1 2 0 0 A B T1 T3 T2 newAVL Tree Building Example
Let us work through an example that inserts
numbers in a balanced search tree.
We will check the balance after each insert
AVL Tree Building Example
Insert(1)
AVL Tree Building Example
Insert(2)
1
AVL Tree Building Example
Insert(3) single left rotation
1
2
3
AVL Tree Building Example
Insert(3) single left rotation
1
2
3
AVL Tree Building Example
Insert(3)
1
2
26
AVL Tree Building Example
Insert(4)
1
2
3
27
AVL Tree Building Example
Insert(5)
1
2
3
4
5
28
AVL Tree Building Example
Insert(5)
1
2
3
4
29
AVL Tree Building Example
Insert(6)
1
2
3
4
5
6
30
AVL Tree Building Example
Insert(6)
1
2
3 4
5
31
AVL Tree Building Example
Insert(7)
1
2
3 4
5
6
7
32
AVL Tree Building Example
Insert(7)
1
2
3 4
5
6
33
AVL Tree Building Example
1
2
3 4
5
6
7
34
AVL Tree Building Example
Insert(15)
1
2
3 4
5
6
7
16
15
35
AVL Tree Building Example
Insert(15)
1
2
3 4
5
6
7 16
15
36
Cases for Rotation
Single rotation does not seem to restore the
balance.
The problem is the node 15 is in an inner
subtree that is too deep.
37
Cases for Rotation
Let us call the node that must be rebalanced α .
Since any node has at most two children, and a
height imbalance requires that α’s two subtrees
differ by 2 (or –2), the violation will occur in four cases:
1.
An insertion into left subtree of the left child of α
2.
An insertion into right subtree of the left child of α
3.
An insertion into left subtree of the right child of α
4.
An insertion into right subtree of the right child
38
Cases for Rotation
Outside insertion:
The insertion occurs on the “outside” (i.e.,
left-left or right-right) in cases 1 and 4
Single rotation can fix the balance in cases 1
and 4.
Inside insertion:
Insertion occurs on the “inside” in cases 2
39
Cases for Rotation
Single right rotation to fix case 1. (LL)
How to add a new node as a child of node X.
40
Cases for Rotation
Single left rotation to fix case 4. (RR)
Similarly, How to add a new node as a child of
node X.
Level n Level n-1 Level n-2
k1
k2 X
Y
Z
k1
k2
41
Cases for Rotation
Single right rotation fails to fix case 2. (RL)
How about if the new node a child of node Y
k1 k2 Z X Level n Level n-1 Level n-2 k1 k2 Z X
α α
new
Y
new
42
Cases for Rotation
Y is non-empty because
the new node was inserted in Y.
Thus Y has a root and two
subtrees.
View the entire tree with
four subtrees connected with 3 nodes.
k1
k2
X
Y
43
Cases for Rotation
Y is non-empty because
the new node was inserted in Y.
Thus Y has a root and two
subtrees.
View the entire tree with
four subtrees connected with 3 nodes.
k1
k3
D
A
B C
44
Cases for Rotation
k3 k1 A new 1 2
New node inserted a either of the two spots
B C
k2
D
new’
§ Exactly one of tree B or C
is two levels deeper than
D; we are not sure which
one.
§ Good thing: it does not
matter.
45
Cases for Rotation
k3 k1 A new 1 2
New node inserted a either of the two spots
B C
k2
D
new’
§
A rotation between
k
3and
k
1(
k
3was
k
2then)
was shown to not work.
§
The only alternative is
to place
k
2as the new
root.
§
This forces
k
1to be
46
Cases for Rotation
Left-right
double
rotation to fix case 2.
New node inserted a either of the two spots
47
Cases for Rotation
Left-right
double
rotation to fix case 2.
A new B D C new’ A new B D C new’ k3 k2 k1
Rotate right k2
48
Cases for Rotation
Right-left
double
rotation to fix case 3 (LR)
k1
k3
D A
B C
k2
k1
k3
D A
B
C k2
49
Cases for Rotation
Right-left
double
rotation to fix case 3.
k1
k2
D
A B C
k3 k1
k3
D A
B
C k2
50
AVL Tree Building Example
Insert(15)
1
2
3 4
5
6
X
(null)
Z
(null) Y
15
k1
k2
7
51
AVL Tree Building Example
Insert(15) right-left double rotation
1
2
3 4
5
6
7
16
15
k1
k3
k2
52
AVL Tree Building Example
Insert(15) right-left double rotation
1
2
3 4
5
6
7
16 15
k1
k3 k2
53
AVL Tree Building Example
Insert(15) right-left double rotation
1
2
3 4
5
6
16
k3
15
k2
7
54
AVL Tree Building Example
Insert(14): right-left double rotation
1
2
3 4
5
6
16
k3
15
k2
7
k1
14
55
AVL Tree Building Example
Insert(14): right-left double rotation
1
2
3 4
5
6
16
k3
15
k2
7
k1
14
56
AVL Tree Building Example
Insert(14): right-left double rotation
1
2
3 4
5 16
k3
15
k2
7
6
k1
57
AVL Tree Building Example
Insert(13): single rotation
1
2
3 4
5 16
15 7
6
14
13
58
AVL Tree Building Example
Insert(13): single rotation
1
2
3 4
5
16 15
7
6 14
59
Deletion in AVL Tree
Delete is the inverse of insert: given a value X
and an AVL tree T, delete the node containing X and rebalance the tree, if necessary.
Turns out that deletion of a node is
60
Deletion in AVL Tree
Insertion in a height-balanced tree requires at
most one single rotation or one double rotation.
We can use rotations to restore the balance
when we do a deletion.
We may have to do a rotation at every level of
61
Deletion in AVL Tree
Here is a tree that causes this worse case
number of rotations when we delete A. At every node in N’s left subtree, the left subtree is one
shorter than the right subtree.
A C
D
N
E J
G
I F
H
K L
62
Deletion in AVL Tree
Deleting A upsets balance at C. When rotate (D
up, C down) to fix this
A C
D
N
E J
G
I F
H
K L
63
Deletion in AVL Tree
Deleting A upsets balance at C. When rotate (D
up, C down) to fix this
C
D
N
E J
G
I F
H
K L
64
Deletion in AVL Tree
The whole of F’s left subtree gets shorter. We fix this
by rotation about F-I: F down, I up.
C
D
N
E
J G
I F
H
K L
65
Deletion in AVL Tree
The whole of F’s left subtree gets shorter. We fix this by
rotation about F-I: F down, I up.
C
D
N
E
J G
I F
H
K L
66
Deletion in AVL Tree
This could cause imbalance at N.
The rotations propagated to the root.
C
D
N
E
J G
I
F
H
K
L
67
Deletion in AVL Tree
Procedure
Delete the node as in binary search tree (BST).
The node deleted will be either a leaf or have
just one subtree.
Since this is an AVL tree, if the deleted node has
one subtree, that subtree contains only one node (why?)
Traverse up the tree from the deleted node
68
Deletion in AVL Tree
There are 5 cases to consider.
Let us go through the cases graphically and
69
Deletion in AVL Tree
Case 1a: The parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree.
Action: change the balance of the parent node and
stop. No further effect on balance of any higher node.
70
Deletion in AVL Tree
Here is why; the height of left tree does not change.
How about if we delete(1). The parent is 2 with balance of 0.
1
2
3 4
5
6
7
0
1
71
Deletion in AVL Tree
1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
Here is why; the height of left tree does not change.
72
Deletion in AVL Tree
Case 1b: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree.
Action: (same as 1a) change the balance of the
parent node and stop. No further effect on balance of any higher node.
73
Deletion in AVL Tree
Here is why; the height of left tree does not change.
How about if we delete(7). The parent is 2 with balance of 0.
1
2
3 4
5
6
7
0
1
74
Deletion in AVL Tree
1 2 3 4 5 6 7 2 3 4 5 6 0 1 2 Remove(7)
Here is why; the height of left tree does not change.
How about if we delete(7). The parent is 2 with balance of 0.
75
Deletion in AVL Tree
Case 2b: the parent of the deleted node had a balance of –1 and the node was deleted in the parent’s right subtree.
Action: same as 2a: change the balance of the parent
node. May have caused imbalance in higher nodes so continue up the tree.
76
Deletion in AVL Tree
Case 3a: the parent had balance of –1 and the node was deleted in the parent’s left subtree,
77
Deletion in AVL Tree
Case 3a: the parent had balance of -1 and the node was deleted in the parent’s left subtree,
right subtree was balanced.
Action: perform single rotation, adjust balance. No
effect on balance of higher nodes so stop here.
78
Deletion in AVL Tree
Case 4a: parent had balance of –1 and the node was deleted in the parent’s left subtree, right
79
Deletion in AVL Tree
Case 4a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right
subtree was unbalanced.
Action: Double rotation at B. May have effected the balance of higher nodes, so continue up the tree.
80
Deletion in AVL Tree
Case 5a: parent had balance of –1 and the node was deleted in the parent’s left subtree, right
81
Deletion in AVL Tree
Case 5a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right
subtree was unbalanced.
Action: Single rotation at B. May have effected the
balance of higher nodes, so continue up the tree. rotate