AVL TREE
AVL TREE
Nicki Dell
Spring 2014
Announcements
• TA Sessions
– Tomorrow: BST and AVL Trees
– Tuesday: Priority Queues and Binary Heaps
• Today
– Finish AVL Trees
– Start Priority Queues
Structural properties
1. Binary tree property (same as BST)
2. Order property (same as for BST)
3. Balance property:
balance of every node is between -1 and 1
• Only one case occurs because tree was balanced before insert
a h+3 b h+2
h+2
h+1
b h a
h+1 h+1 h
h h
Z
X
X Y Y Z
h+3
a
h+2 b h+2
h h+1
b h+1
a
X h h
h+1 h
Z
Y Z X Y
2
1 1 Violates order
1
6 property!
6
0 0
0 1 3
3
Spring 2014 CSE373: Data Structures & Algorithms 8
Two cases to go
1 1
6 3
0 0
3 6
Spring 2014 CSE373: Data Structures & Algorithms 9
Sometimes two wrongs make a right
• First idea violated the order property
• Second idea didn’t fix balance
• But if we do both single rotations, starting with the second, it
works! (And not just for this example.)
• Double rotation:
1. Rotate problematic child and grandchild
2. Then rotate between self and new child
2 2
1 1 1
6
1
1 3
3
0 0
0 0
3 6 1 6
Spring 2014 CSE373: Data Structures & Algorithms 10
The general right-left case
h+3
a h+2
h h+1 b
h
c
X h h-1
V Z
U
h+2
c
h+3
a h+1 h+1
h+2 a b
h c h
h+1 h h
h h-1
X b U
h-1 V
U h
X Z
V
Z
Spring 2014 CSE373: Data Structures & Algorithms 11
Comments
• Like in the left-left and right-right cases, the height of the subtree
after rebalancing is the same as before the insert
– So no ancestor in the tree will need rebalancing
• Does not have to be implemented as two rotations; can just do:
h+3 h+2
a h+2 c
h h+1 b h+1 h+1
h a b
c h
X h h-1 h h
h-1
V Z U V
U X Z
• Easier to remember than you may think:
Move c to grandparent’s position
Put a, b, X, U, V, and Z in the only legal positions for a BST
Spring 2014 CSE373: Data Structures & Algorithms 12
The last case: left-right
h+3
a h+2
h+2 c
b h h+1
h+1 h+1
b a
c Z h
h h h
h h-1
h-1 U V
X U V X Z
next up…
6 2
• Operations: 15 23
– insert insert 12 18 deleteMin
– deleteMin 45 3 7
– is_empty
• Key property: deleteMin returns and deletes the item with greatest
priority (lowest priority value)
– Can resolve ties arbitrarily
Spring 2014 CSE 373 18
Example
a heap 10
not a heap 10
20 80
20 80
40 60 85 99
30 15
50 700
So:
• Where is the highest-priority item?
• What is the height of a heap with n items?
Spring 2014 CSE 373 23
Operations: basic idea
1
4 3
7 5 8 9
11 9 6 10
11 9 6 10
Spring 2014 CSE 373 26
DeleteMin: Restore the Heap Property
Percolate down:
• Keep comparing priority of item with both children
• If priority is less important, swap with the most important child and
go down one level
• Done if both children are less important than the item or we’ve
reached a leaf node
? 10
10 3
3 ?
4 3 4 4 8
7 5 8 9 7 5 8 9 7 5 10 9
11 9 6 11 9 6 11 9 6
Why is this correct?
What is the run time?
Spring 2014 CSE 373 27
DeleteMin: Run Time Analysis
11 9 6
CSE 373
Spring 2014 29
Insert: Maintain the Structure Property
11 9 6
2 ?
1 1 1
4 8 4 8 2 8
7 5 10 9 7 10 9 7 4 10 9
? ?
11 9 6 11 9 6 5 11 9 6 5
2 2
What is the running time?
Like deleteMin, worst-case time proportional to tree height: O(log n)
Spring 2014 CSE 373 31
Summary
• Priority Queue ADT:
6 2
– insert comparable object, 1523
insert 12 18 deleteMin
– deleteMin 45 3 7