8
5
11
Order property
All keys in left subtree smaller 2
than nodes key
All keys in right subtree larger
4
than nodes key
Result: easy to find any given
key
Data Structures & Algorithms
10
7
12
14
13
1
BST: Efficiency of
Operations?
Problem: operations may be inefficient if
BST is unbalanced.
Find, insert, delete
O(n) in the worst case
BuildTree
O(n2) in the worst case
2.
4.
6
4
1
8
0
2
11
7
0 10
1
12 0
5
3
1
11 0
10
An AVL Tree
Node object
3
10
2
0
9
0
7
1
15
key
value
height
children
20
1
10
0
30
0
17
Track height at all times!
11
12
13
6
3
3
1
3
1
14
6
3
1
0
1
0
Childs new-height = old-height-before-insert
Data Structures & Algorithms
15
h+1
h+2
h
Z
X
h+2
h+3
h
h+1
h
Z
16
17
18
h+2
h+3
h+1
Z
X
h+1
h
Y
h+2
a
h+1
h
X
Y
19
22
24
19
10
6
17
20
16
15
8
4
3
19
10
6
Data Structures & Algorithms
22
17
16
20
24
20
h+3
h+2
h+1
b
X
h+2
h+1
h+1
h
Z
21
Two cases to go
Unfortunately, single rotations are not enough for insertions in the
left-right subtree or the right-left subtree
Simple example: insert(1), insert(6), insert(3)
First wrong idea: single rotation like we did for left-left
2
1
6
3
Violates order
property!
1
Data Structures & Algorithms
3
22
Two cases to go
Unfortunately, single rotations are not enough for insertions in the
left-right subtree or the right-left subtree
Simple example: insert(1), insert(6), insert(3)
Second wrong idea: single rotation on the child of the
unbalanced node
1
6
Still unbalanced!
1
0
6
Data Structures & Algorithms
23
1
6
3
6
Data Structures & Algorithms
6
24
h+2
b
h+1
X
h-1
c
a
h
h
U
h+1
h+2
c
X
h+3
h-1
h+1
h
a
U
h+1
h
X
h+2
h-1
V
Z
Data Structures & Algorithms
25
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
26
h
X
b
c
h+1
Z
h
U
h-1
V
h+1
h
X
h+2
h+
1
h
h-1
V
27
Insert, summarized
Insert as in a BST
Check back up path for imbalance, which will be 1 of 4 cases:
Nodes left-left grandchild is too tall
Nodes left-right grandchild is too tall
Nodes right-left grandchild is too tall
Nodes right-right grandchild is too tall
Only one case occurs because tree was balanced before insert
After the appropriate single or double rotation, the smallestunbalanced subtree has the same height as before the insertion
So all ancestors are now balanced
28
Example
3
10
2
0
20
1
9
0
7
1
15
0
30
0
17
29
Insert a 6
10
3
5
left-left
20
2
What do we do?
9
7
1
15
0
30
0
17
30
Insert a 6
3
10
2
5
0
20
1
1
15
7
0 6
0
30
0
17
31
Insert a 13
3
10
2
5
0
20
1
7
0 6
1
15
0
30
0
0 13 17
9
32
Insert a 14
4
10
2
5
0
20
1
7
0 6
3
2
15
1
0 13
9
0
30
0
17
14 0
33
Insert a 14
4
10
2
5
0
20
1
7
0 6
3
2
15
1
0 13
9
0
30
0
17
14 0
34
Insert a 14
3
10
2
5
0
15
1
7
0 6
1 13
9
20 1
0 14 17 0 30 0
35
Now efficiency
Worst-case complexity of find: O(log n)
Tree is balanced
Worst-case complexity of insert: O(log n)
Tree starts balanced
A rotation is O(1) and theres an O(log n) path to root
Tree ends balanced
Worst-case complexity of buildTree: O(n log n)
Takes some more rotation action to handle delete
36
37
38
39
40
41
unsorted array
add at end
O(1)
search
O(n)
add at front
O(1)
search
O(n)
search / shift
O(n)
move front
O(1)
AVL tree
42
O(n)
O(log n)
10
20
10
a heap
20
80
40
30
15
50
80
60
85
99
700
So:
Where is the most important item?
What is the height of a heap with n items?
43
10
20
40
50
80
60
85
99
700
Overall strategy:
Preserve structure property
Break and restore heap
property
44
DeleteMin
Delete (and later return) value at root node
1
4
11 9
6 10
45
11 9
6 10
7
11 9
5
6
10
46
3
4
11 9
10
3
4
4
9
7
11 9
5
6
7
11 9
10
48
Insert
Add a value to the tree
1
4
7
11 9
10
49
7
11 9
10
50
10
10
11 9
?
2
?
11 9
7
11 9
4
6
10
Summary
Priority Queue ADT:
insert comparable object,
deleteMin
Binary heap data structure:
Complete binary tree
Each node has less important
priority value than its parent
6
insert
15
23
12 18
45 3 7
deleteMin
10
20
40
700
80
60
85
99
50