Topics:
- priority queue abstract data type, implementations: unsorted list, sorted list, bucket queue
- heap data structure, binary heap representations: binary tree, array
- heap sort, compare to insertion sort and selection sort
- sorting algorithm stability
- in-place algorithms
Resources:
- review Make School's heap slides
- watch Make School's heap video lecture
- watch this cute robot heap sort animation video
- play with VisuAlgo's interactive heap visualization
- read about sorting algorithms implemented with priority queue
Challenges:
- implement
MinHeap
class using dynamic array with the following instance methods using heap starter code:is_empty
- check if the heap is emptysize
- return the number of items in the heapget_min
- return the minimum item at the root of the heapremove_min
- remove and return the minimum item at the root of the heapreplace_min(item)
- remove and return the minimum item, and insertitem
into this heapinsert(item)
- insertitem
into this heap_bubble_up(index)
- ensure the heap-ordering property is true aboveindex
, swapping out of order items_bubble_down(index)
- ensure the heap-ordering property is true belowindex
, swapping out of order items
- run
pytest test_heap.py
to run the heap unit tests and fix any failures - implement heap sort with binary heap (this is super easy)
- implement
PriorityQueue
class using binary heap with the following instance methods using priority queue starter code:is_empty
- check if the priority queue is emptylength
- return the number of items in the priority queueenqueue(item, priority)
- insertitem
into the priority queue in order according topriority
front
- return the item at the front of the priority queue without removing it, or Nonedequeue
- remove and return the item at the front of the priority queue, or raise ValueErrorpush_pop(item, priority)
- remove and return the item at the front of this priority queue, and insertitem
in order according topriority
- annotate class instance methods with running time complexity analysis
Stretch Challenges:
- implement priority queue with binary search tree
- implement stack with priority queue
- generalize binary heap with min or max initialization option