Professional Documents
Culture Documents
CSE 326: Data Structures AVL Trees The AVL Balance Condition
CSE 326: Data Structures AVL Trees The AVL Balance Condition
Ordering property
15
› Same as for BST
Circle One:
6
4 8 AVL
› M(0) = 1, M(1) = 2
• Induction
1 5 7 11
› M(h) = M(h-1) + M(h-2) + 1 h-2
Not AVL h-1
3 • Solution
2 › M(h) > φh - 1 (φ = (1+√5)/2 ≈ 1.62)
Student Activity If not AVL, put a box around nodes where AVL property is violated.
1
Proof that M(h) > φh Height of an AVL Tree
• Basis: M(0) = 1 > φ0 -1, M(1) = 2 > φ1-1 • M(h) > φh (φ ≈ 1.62)
• Induction step. • Suppose we have N nodes in an AVL
M(h) = M(h-1) + M(h-2) + 1 tree of height h.
> (φh-1 - 1) + (φh-2 - 1) + 1 › N > M(h)
= φh-2 (φ +1) - 1 › N > φh - 1
= φh - 1 (φ2 = φ +1) › logφ(N+1) > h (relatively well balanced
tree!!)
2 2 balance factor
2 3 1-(-1) = 2
6 6 6 6
1 0 1 1 1 1 1 2
4 9 4 9 4 9 4 9
0 0 0 0 0 0 0 0 0 0 1 -1
1 5 1 5 8 1 5 7 1 5 8
0
7
height of node = h height of node = h
balance factor = hleft-hright balance factor = hleft-hright
empty height = -1 empty height = -1
2
Insertions in AVL Trees Bad Case #1
Let the node that needs rebalancing be α.
Insert(6)
There are 4 cases:
Outside Cases (require single rotation) : Insert(3)
1. Insertion into left subtree of left child of α. Insert(1)
2. Insertion into right subtree of right child of α.
Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of α.
4. Insertion into left subtree of right child of α.
The rebalancing is performed through four
separate rotation algorithms.
3
1
0 0 k h
1 6
1
0 h
h
Z
X Y
Single Rotation:
1. Rotate between x and child
h+1 h Z h+1 h Z
Y Y
X X
3
Single rotation from left Outside Case Completed
j k
“rotation from left” done!
(“rotation from right” is
mirror symmetric)
k h
h+1
j
h+1 h Z h h
Y X Y Z
X AVL property has been restored!
3 10 17 21 Insert(1)
2 4 Insert(6)
1 15 Insert(3)
3 20
2 5 17 21
1 4 10
4
AVL Insertion: Inside Case AVL Insertion: Inside Case
Inserting into Y
destroys the j Does “rotation from left”
restore balance? k “Rotation from left”
does not restore
AVL property balance… now k is
at node j
k h
h j out of balance
h h+1 Z X h+1
h
X Z
Y Y
h h+1 Z h
i h+1 Z
X X h or h-1
Y
V W
5
Double rotation : second Double rotation : second
rotation rotation
j right rotation complete
k Z k j
W h h or h-1
h
X V X V W Z
4 10 16 6 10 16
4
3 6
3 5
5
15 15
8 17 6 17
6 10 16 4 8 16
4 3 5 10
3 5
Single Rotation
1. Rotate between x and child
Double Rotation
1. Rotate between x’s child and grandchild
2. Rotate between x and x’s new child
Student Activity
6
Single and Double Rotations:
Inserting what integer values Insertion into AVL tree
would cause the tree to need a:
3
Single Rotation3 Hard Insert (Bad Case #2)
10 10 3
Insert(18) 10
2 3 2 2
5 15 5 20 2 2
5 15
1 0 0 2 1 0 1 1
2 9 12 20 2 9 15 30 1 0 0 1
2 9 12 20
0 0 1 0 0 0 0
3 17 30 3 12 17 33 0 0 0
Unbalanced? 3 17 30
0
33 How to fix?
7
Double Rotation (Step #1)
Single Rotation (oops!) 3 3
3 3 10 10
10 10
2 3 2 3
2 3 2 3 5 15 5 15
5 15 5 20
1 0 0 2 1 0 0 2
1 0 0 2 1 0 2 0 2 9 12 20 2 9 12 17
2 9 12 20 2 9 15 30
0 1 0 0 1
0 1 0 0 0 1 3 17 30 3 20
3 17 30 3 12 17
0 0 0
0 0 18 18 30
18 18
8
Implementation Single Rotation
RotateFromRight(n : reference node pointer) {
balance (1,0,-1) p : node pointer;
key p := n.right; n
n.right := p.left;
left right p.left := n;
n := p
}
Y Z
V W