Lecture15 (Presorting, BST)
Lecture15 (Presorting, BST)
Lecture #15
Transform and Conquer
Runtime?
Computing a mode
• A mode is a value that occurs most often in a list of
numbers
– e.g. the mode of [5, 1, 5, 7, 6, 5, 7] is 5
– If several different values occur most often any of them can be
considered the mode
• “Count Sort” approach: (assumes all values > 0; what if
they aren’t?)
max max(A)
freq[1..max] 0
for each x A
freq[x] +=1 Runtime?
mode freq[1]
for i 2 to max
if freq[i] > freq[mode] mode i
return mode
Presort Computing Mode
Sort A
i0
modefrequency 0
while i ≤ n-1
runlength 1; runvalue A[i]
while i+runlength ≤ n-1 and A[i+runlength] = runvalue
runlength += 1
if runlength > modefrequency
modefrequency runlength
modevalue runvalue
i+= runlength
return modevalue
AVL Animation Link
https://www.cs.usfca.edu/~galles/visualization/
AVLtree.html
Binary Search Tree - Best Time
1 4
2
2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6
1 3 5 7 7
Approaches to balancing trees
• Don't balance
– May end up with some nodes very deep
• Strict balance
– The tree must always be balanced perfectly
• Pretty good balance
– Only allow a little out of balance
• Adjust on access
– Self-adjusting
Balancing Binary Search Trees
1 5 8 1 4 6 9
AVL - Good but not Perfect Balance
height of node = h
balance factor = hleft-hright
empty height = -1
Node Heights after Insert 7
height of node = h
7
balance factor = hleft-hright
empty height = -1
Insert and Rotation in AVL Trees
2 2
6 6
1 2 1 1
4 9 4 8
0 0 1 0 0 0 0
1 5 8 1 5 7 9
0
7
Insertions in AVL Trees
Consider a valid
AVL subtree
j
k h
h
h
Z
X Y
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node j
k h
h+1 h Z
Y
X
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
Outside Case Completed
h+1
j
h h
X Y Z
AVL property has been restored!
AVL Insertion: Inside Case
Consider a valid
AVL subtree
j
k h
h h Z
X Y
AVL Insertion: Inside Case
Inserting into Y
destroys the j Does “right rotation”
restore balance?
AVL property
k
at node j h
h h+1 Z
X
Y
AVL Insertion: Inside Case
k “Right rotation”
does not restore
balance… now k is
h
j out of balance
X h+1
h
Z
Y
AVL Insertion: Inside Case
h h+1 Z
X
Y
AVL Insertion: Inside Case
Y = node i and
subtrees V and W
j
k h
h
i h+1 Z
X h or h-1
V W
AVL Insertion: Inside Case
j We will do a left-right
“double rotation” . . .
k
i Z
X
V W
Double rotation : first rotation
i
k Z
W
X V
Double rotation : second
rotation
i
k Z
W
X V
Double rotation : second
rotation
k j
h h
h or h-1
X V W Z
Implementation
balance (1,0,-1)
key
left right
No need to keep the height; just the difference in height, i.e. the
balance factor; this has to be modified on the path of insertion even if
you don’t perform rotations
Once you have performed a rotation (single or double) you won’t need to
go back up the tree
Single Rotation
V W
Insertion in AVL Trees
2
20 Insert 5, 40
0 1
10 30
0 0
25 35
Example of Insertions in an AVL
Tree
2
3
20 20
1 1 1 2
10 30 10 30
0 0 0 1
0 0
5 25 35 5 25 35
0
Now Insert 45
40
Single rotation (outside case)
3
3
20 20
1 2 1 2
10 30 10 30
0 0 2
0 0 1
5 25 35 5 25 40
0 0
35 45
Imbalance 1 40
0
45 Now Insert 34
Double rotation (inside case)
3
3
20 20
1 3 1 2
10 30 10 35
0 0 2
0 1
Imbalance 1
5 25 40 5 30 40
0
1 0 0 25 34 45
35 45
Insertion of 34 0
34
AVL Tree Deletion