13. Heaps & Maps
17/03/23
Heap
Insertion
- Method
insertItem
of the priority queue ADT corresponds to the insertion of a key to the heap - The insertion algorithm consists of three steps
- Find the insertion node
- Store at
- Restore the heap-order property
Upheap
- After the insertion of a new key , the heap-order property may be violated
- Algorithm up heap restores the heap-order property by swapping along an upward path from the insertion node
- Upheap terminates when the key reaches the root or a node whose parent has a key smaller than or equal to
- Since a heap has a height , upheap runs in time.
Removal
- Method
removeMin
of the priority queue ADT corresponds to the removal of the root key from the heap - The removal algorithm consists of three steps
- Replace the root key with the key of the last node
- Remove
- Restore the heap-order property (discussed next)
Downheap
- After replacing the root key with the key of the last node, the heap-order property may be violated
- Algorithm downheap restores the heap-order property by swapping key along a particular downward path from the root
- Downheap terminates when key reaches a leaf or a node whose children have keys greater than or equal to
- Since a heap has height downheap runs in time
Array Heap Implementation
Similar to trees
- Can represent a heap with keys by means of an Array (or vector) of length
- Links between nodes are not explicitly stored, instead for the node at index :
- the left child is at index
- the right child is at index
- the parent is at index
- The cell at index 0 is not used
- No gaps when storing a heap
- Upheap and downheap just swap elements within the array
Heap sort
- Using a heap-based priority queue, can sort a sequence of n elements in time. Resulting algorithm is called heap-sort
- Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort
Has two stages:
- Insert all elements into the heap one by one. Takes insertions with each for a total of
- Remove all elements one by one, using
removeMin()
, hence obtaining them in sorted order. This takes removals with each for a total of
Do NOT confuse binary tree implementations of heaps with binary search trees!!
Maps ADT : Using lists
- A map models a collection of key-value entries that is searchable 'by the key'
- The main operations of a map are for searching, inserting, and deleting items
- Multiple entries with the same key are not allowed
MAP: Access any key PQ: Access only the minimum key
Can implement a map using unsorted list
- Store the items of the map in a list S (doubly-linked list) in arbitrary order
- and a size counter n, so
getSize()
is
Performance of a List-Based Map
put
would have taken time if we could just insert the new item at the beginning (or end) of the sequenceget
andremove
take time since in the worse case (item not found) we traverse the entire sequence to look for an item with the given key- The unsorted list implementation is suitable only for maps of small size, sorted list still