Download presentation
Presentation is loading. Please wait.
Published bySilvester Leonard Modified over 9 years ago
1
1 Joe Meehean
2
We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on… This ADT is called a priority queue 2
3
Prioritized Keys (associated values) Highest Priority Mapping 3
4
Initialize create an empty priority queue Insert an item with a priority duplicates (priorities and items) are OK Remove and return highest/lowest priority item Is empty? 4
5
Insert 1, 5, 10, 2, 6 Then remove 5 times Results: normal Q: 1, 5, 10, 2, 6 priority Q: 10, 6, 5, 2, 1 5
6
How would we implement this ADT? We could use a BST or AVL finding largest/smallest is O(logN) supports more operations than we need Is there something better? 6
7
Heaps a balanced binary tree height always log # nodes each node has a comparable key value (priority) Must preserve order and shape properties 7
8
For every node n n’s key is >= its kid’s keys therefore, n’s key >= all keys in its subtrees 8
9
All leaves are at depth d or d-1 leaves may be at most one level apart All leaves at depth d-1 are to the right of all leaves at depth d At most 1 node has just 1 child node is the right most leaf at depth d child is stored as the left child 9
10
More simply All levels of tree completely filled Except (potentially) bottom level Bottom level must be filled from left to right 10
11
Root Level d-1 Level d Node with 1 kid 11
12
12 YES
13
NO: Bad leaf depth 13
14
14 YES
15
15 NO: Should be left child
16
16 NO: Should be right-most leaf at depth d
17
17 YES
18
18 YES
19
7 7 7 7 6 6 5 5 2 2 19 4 4 1 1 YES
20
7 7 7 7 6 6 5 5 2 2 20 4 4 6 6 NO: 5 !>= 6 (Order property)
21
Heaps are NOT BSTs both right and left child are less than the root Heaps with max at the root are called max-heaps Heaps with min at the root are called min-heaps value at n <= values of n’s kids 21
22
Use an array or array-list Each node takes one space in the array The root is in array[1] array[0] is not used For a node in array[k] left child is array[k * 2] right child is array[k * 2 +1] parent is in array[k/2] //using integer division 22
23
Shape property guarantees no holes in the array 23 6 6 5 5 1 1 4 4 2 2 3 3 6 6 1 5 5 2 4 4 3 2 2 40 1 1 5 3 3 6
24
24
25
Adding new value (priority) should maintain shape and order properties occur in reasonable time: O(logN) Add new value to end of array new rightmost leaf at depth d Or, new leaf at depth d + 1 Compare: If child > parent Then swap Repeat up the tree (to root if necessary) 25
26
26 6 6 5 5 1 1 4 4 2 2 3 3 6 6 1 5 5 2 4 4 3 2 2 40 1 1 5 3 3 6
27
27 6 6 5 5 1 1 4 4 2 2 3 3 6 6 1 5 5 2 4 4 3 2 2 40 1 1 5 3 3 6 8 8 7 8 8
28
28 6 6 5 5 1 1 4 4 2 2 3 3 6 6 1 5 5 2 8 8 3 2 2 40 1 1 5 3 3 6 4 4 7 8 8 8 4
29
29 6 6 5 5 1 1 4 4 2 2 3 3 8 8 1 5 5 2 6 6 3 2 2 40 1 1 5 3 3 6 4 4 7 8 8 8 4 6 8
30
30 8 8 5 5 1 1 6 6 2 2 3 3 8 8 1 5 5 2 6 6 3 2 2 40 1 1 5 3 3 6 4 4 7 4 4
31
31 8 8 5 5 1 1 6 6 2 2 3 3 8 8 1 5 5 2 6 6 3 2 2 40 1 1 5 3 3 6 4 4 7 7 7 8 7 7 4 4
32
32 8 8 7 7 1 1 6 6 5 5 3 3 8 8 1 7 7 2 6 6 3 5 5 40 1 1 5 3 3 6 4 4 7 2 2 8 2 2 4 4
33
Return root value max value is always the root Remove root replace root with last value in array right most leaf at depth d replace, not swap If root < either child swap with larger child repeat as necessary down the tree 33
34
34 8 8 7 7 1 1 6 6 5 5 3 3 8 8 1 7 7 2 6 6 3 5 5 40 1 1 5 3 3 6 4 4 7 2 2 8 2 2 4 4 current_size
35
35 8 8 7 7 1 1 6 6 5 5 3 3 2 2 1 7 7 2 6 6 3 5 5 40 1 1 5 3 3 6 4 4 7 2 2 8 2 4 4 current_size
36
36 8 8 7 7 1 1 6 6 5 5 3 3 7 7 1 2 2 2 6 6 3 5 5 40 1 1 5 3 3 6 4 4 7 2 2 8 2 2 7 4 4 current_size
37
37 8 8 7 7 1 1 6 6 5 5 3 3 7 7 1 5 5 2 6 6 3 2 2 40 1 1 5 3 3 6 4 4 7 2 2 8 2 2 7 2 5 4 4 current_size
38
Insert add value to end of array O(1) average case O(N) worst case swap values up tree: O(height) => O(logN) avg. case: O(logN), worst case: O(N) RemoveMax get value from root: O(1) replace with value from end of array: O(1) swap values down tree: O(height) => O(logN) 38
39
template T: data type to store Container: internal container to use as heap must have random access iterator Compare: functor(a,b) that returns true if a should be returned before b Defaults container default to vector compare defaults to less 39
40
push insert an element pop remove the top element top access but do not remove the top element size empty 40
41
Avg. complexity of insert and removeMax would be the same note: a general heap lookup could be expensive thankfully, heaps only need to return the root Heaps use less space, no pointers heaps are waaaaaay easier to program 41
42
42
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.