Académique Documents
Professionnel Documents
Culture Documents
Motivating Examples
Printing: A shared server has a list of print jobs to print. It wants to print
them in chronological order, but each print job also has a priority, and
higher-priority jobs always print before lower-priority jobs
Using PriorityQueues
PriorityQueue<E>() constructs a PriorityQueue that orders the
elements according to their compareTo
(element type must implement Comparable)
add(element)
remove()
peek()
Potential Implementations
insert
Unsorted list (Array)
Unsorted list (Linked-List)
Sorted list (Array)
Sorted list (Linked-List)
Binary Search Tree
AVL Trees
5
deleteMin
Potential Implementations
insert
deleteMin
(1)
(n)
(1)
(n)
(n)
(1)*
(n)
(1)
(n) worst
(n) worst
AVL Trees
(log n)
(log n)
Heap properties
1. completeness
complete tree: every level is full except possibly the lowest
level, which must be filled from left to right with no leaves to
the right of a missing node (i.e., a node may not have any
children until all of its possible siblings exist)
Heap shape:
Heap properties 2
2. heap ordering
a tree has heap ordering if P <= X for every element X with
parent P
10 wrong!
wrong!
20
80
40
15
20
40
50 700
9
80
60
85
99
700
80
60
10
85
50
10
20wrong!
40
60
20
40
99
10 wrong!
20
80
60
85
50 700
10
99
80
40
80
60
99
48
21
20
80
10
25
14
24
10
17
3
50
30
33
10
30
10
30
40
22
wrong!
11
35
28
18
n-node
complete tree
of height h:
2h n 2h+1 1
h = log n
11
Implementation of a heap
12
index of n.left = 2i
index of n.right = 2i + 1
parent index?
Adding to a heap
10
20
40
50
14
700
10
80
60
65
85
20
99
40
50
700
80
60
65
85
15
99
10
20
40
50
15
700
10
80
60
65
85
15
15
99
40
50
700
80
20
65
85
60
99
Adding to a max-heap
16
16
5
18
16
18
11
3
18
11
16
3
11
17