cs350: data structures heaps and priority queues · 2017-12-09 · cs350: data structures binary...
TRANSCRIPT
CS350: Data Structures © James Moscola
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATALO
G 2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania
CS350: Data StructuresHeaps and Priority Queues
CS350: Data Structures
Priority Queue
• An abstract data type of a queue that associates a priority with each of the elements inserted
• Elements are enqueued with some priority
• Elements are dequeued in priority order - Highest priority elements are dequeued first
2
CS350: Data Structures
Binary Heaps
• Great for an implementation of a priority queue
• Similar in structure to a binary search tree
• Sorting of elements in a heap is much weaker than in a BST, however it is sufficient to implement a priority queue
• A binary heap can be either a Min Heap or a Max Heap - Min Heap - smallest key values have highest priority- Max Heap - largest key values have highest priority
3
CS350: Data Structures
Complete Binary Tree
• A heap is implemented as a complete binary tree which can be stored efficiently in an array
• A complete binary tree has the following properties: - Tree is filled in level-order from left to right- Tree has the maximum number of nodes at every level except for
possibly the bottom level- There are no holes allowed in the tree
4
CS350: Data Structures
Complete Binary Tree
5
LJ KH I
GFED
B C
A
N OLJ KH I
GFED
B C
A
This IS a complete binary tree
This IS NOT a complete binary tree
CS350: Data Structures
Complete Binary Tree
• Using a complete binary tree to represent a heap makes traversing the heap easy
• The complete binary tree can easily be stored in an array
6
LJ KH I
GFED
B C
A
A B C D E F G H I J K L0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left child = 2 * iright child = 2 * i + 1
parent = ⎣i / 2⎦
CS350: Data Structures
Binary Heap Properties
• Just as with all other data structures, there are properties that must be maintained for the binary heap
• Elements in the heap must maintain the heap-order property - Heap-order property (for a Min Heap):
• In a heap, for every node X with parent P, the key in P is less than or equal to the key in X (i.e. the parent’s key is less than a child’s key)
7
X
P
P ≤ X
CS350: Data Structures
Binary Heap Operations: insert
• The basic idea for insert:
- Create a ‘hole’ (an empty node) in the next available complete tree location
- If the new element can be inserted into the hole without violating the heap-order property, then insert the new element into the hole
- If inserting the new element into the hole will violate the heap-order property, then move the hole up the heap until a location is found where the new element can be inserted without violating the heap-order property• This operation is called percolateUp
8
CS350: Data Structures
Binary Heap Operations: percolateUp
• Select the node that is to be percolated up the heap
• Compare the node with its parent
• If the node is less than it’s parent, then swap the node with its parent
• Compare the node to its new parent
• Continue moving the node up the tree until the node is greater than or equal to its parent node (i.e. it’s parent is smaller)
9
CS350: Data Structures
Binary Heap Operations: insert Example
10
3265 26
68193124
21 16
13
Insert node 14 First create a hole in the next available heap location
Can 14 be inserted into new hole without violating heap the property?
CS350: Data Structures
Binary Heap Operations: insert Example
11
32 3165 26
681924
21 16
13
Insert node 14 Swap the hole with its parent to move it up the heap
Can 14 be inserted into this location without violating heap the property?
CS350: Data Structures
Binary Heap Operations: insert Example
12
32 3165 26
68192124
16
13
Insert node 14 Continually swap the hole with its parent to move the hole up the heap until a valid location is found to insert the new node
Can 14 be inserted into this location without violating heap the property?
This is typically referred to as percolateUp
CS350: Data Structures
Binary Heap Operations: insert Example
13
32 3165 26
68192124
14 16
13
Insert node 14 Done with insertion
Insert node 14 into this location Done with insertion since (14 > 13)
CS350: Data Structures
Binary Heap Operations: insert
• Time required to do insertion could be as much as O(log N) if the value getting inserting is the new minimum value in the heap - A newly inserted value that is the minimum value must percolate all
the way up the tree
14
CS350: Data Structures
Binary Heap Operations: deleteMin
• The basic idea for deleteMin:
- Delete the root node of the heap (will be the minimum node)- Create a ‘hole’ in the location where the root node was ... this hole
will eventually be filled with the last node in the heap (the rightmost node on the bottom level)
- Move the hole down the tree until a location is found that permits the last node in the heap to get moved while still maintaining the heap-order property• This operation is called percolateDown
15
CS350: Data Structures
Binary Heap Operations: percolateDown
• Select the node that is to be percolated down the heap
• Compare the node the lesser of its two children
• If the node is greater than the lesser of its two children, then swap the node with that child
• Compare the node to its new children
• Continue moving the node down the tree until the node is less than or equal to both of its children
16
CS350: Data Structures
Binary Heap Operations: deleteMin Example
17
32 3165 26
68192119
14 16
13
Delete the minimum node The minimum node is node 13
In a minHeap, the minimum node will always be at the top of the heap
CS350: Data Structures
31
Binary Heap Operations: deleteMin Example
18
3265 26
68192119
14 16
Delete the minimum node A hole is created where the minimum value was removed The size of the heap decreases
After node 13 is removed, a hole is created in its place
Because the size of the heap is decremented after a node is deleted, the last node in the heap is removed. Must find a new location to store the value that was in the last node
CS350: Data Structures
31
Binary Heap Operations: deleteMin Example
19
3265 26
68192119
16
14
Delete the minimum node Swap the hole with the smaller of its children to move it down the heap
Can the 31 we’re trying to place be moved to this location without violating the heap property?
Node 14 < Node 16Move the hole down the tree
CS350: Data Structures
31
Binary Heap Operations: deleteMin Example
20
3265 26
681921
19 16
14
Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value
Can the 31 we’re trying to place be moved to this location without violating the heap property?
This is typically referred to as percolateDown
CS350: Data Structures
31
Binary Heap Operations: deleteMin Example
21
3265
68192126
19 16
14
Delete the minimum node Continually swap the hole with its smaller child to move the hole down the heap until a valid location is found to place the last value
Can the 31 we’re trying to place be moved to this location without violating the heap property?
This is typically referred to as percolateDown
CS350: Data Structures
Binary Heap Operations: deleteMin Example
22
3265 31
68192126
19 16
14
Delete the minimum node Once a location is found for the 31, fill the hole with that value
Fill the hole with the value that was last in the heap prior to the deleteMin operation
CS350: Data Structures
Binary Heap Operations: buildHeap
• The buildHeap operation takes a tree that is not in heap-order and puts it into heap-order
• Idea: - Call percolateDown on all non-leaf nodes in reverse level-order
• No need to call percolateDown on leaf nodes since they cannot be moved down
• The first non-leaf node is located in the array index ⎣currentSize/2⎦
• Easily implemented by iteratively visiting each node in the heap array in reverse order starting at the first non-leaf node
23
CS350: Data Structures
Binary Heap Operations: buildHeap Example
24
83 7325 6455 3761 17
63451220
47 21
92
Fixing the heap order No need to call percolateDown on the leaf nodes
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
25
83 7325 6455 3761 17
63451220
47 21
92Compare with lesser of children, no swap needed
Heap size is 15, so first non-leaf node is ⎣15/2⎦= 7 Processing node at array index 7
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
26
83 7325 6455 3761 17
63451220
47 21
92
Compare with lesser of children, need to swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 6
CS350: Data Structures
Binary Heap Operations: buildHeap Example
27
83 7345 6455 3761 17
63251220
47 21
92
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
28
83 7345 6455 3761 17
63251220
47 21
92
Compare with lesser of children, no swap needed
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 5
CS350: Data Structures
Binary Heap Operations: buildHeap Example
29
83 7345 6455 3761 17
63251220
47 21
92
Compare with lesser of children, need to swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 4
CS350: Data Structures
Binary Heap Operations: buildHeap Example
30
83 7345 6455 3761 20
63251217
47 21
92
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
31
83 7345 6455 3761 20
63251217
47 21
92Compare with lesser of children, no swap needed
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 3
CS350: Data Structures
Binary Heap Operations: buildHeap Example
32
83 7345 6455 3761 20
63251217
47 21
92Compare with lesser of children, need to swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 2
CS350: Data Structures
Binary Heap Operations: buildHeap Example
33
83 7345 6455 3761 20
63254717
12 21
92
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
34
83 7345 6455 3761 20
63254717
12 21
92
Continue to compare with lesser of children, Need another swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
35
83 7345 6455 4761 20
63253717
12 21
92
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
36
83 7345 6455 4761 20
63253717
12 21
92
Compare with lesser of children, need to swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Processing node at array index 1
CS350: Data Structures
Binary Heap Operations: buildHeap Example
37
83 7345 6455 4761 20
63253717
92 21
12Swapped with child1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
38
83 7345 6455 4761 20
63253717
92 21
12Continue to compare with lesser of children, Need another swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
39
83 7345 6455 4761 20
63253792
17 21
12
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
40
83 7345 6455 4761 20
63253792
17 21
12
Continue to compare with lesser of children, Need another swap
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
41
83 7345 6455 4761 92
63253720
17 21
12
Swapped with child
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
CS350: Data Structures
Binary Heap Operations: buildHeap Example
42
83 7345 6455 4761 92
63253720
17 21
12
Fixing the heap order Now in heap order
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15