Skip to content

Latest commit

 

History

History
66 lines (58 loc) · 4.11 KB

Heaps.md

File metadata and controls

66 lines (58 loc) · 4.11 KB

Priority Queues & Heaps

Topics

Resources

Challenges

  • Implement BinaryMinHeap class using dynamic array with the following instance methods using binary heap starter code:
    • is_empty - check if the heap is empty
    • size - return the number of items in the heap
    • insert(item) - insert item into this heap
    • get_min - return the minimum item at the root of the heap
    • delete_min - remove and return the minimum item at the root of the heap
    • replace_min(item) - remove and return the minimum item, and insert item into this heap
    • _bubble_up(index) - ensure the heap ordering property is true above index, swapping out of order items
    • _bubble_down(index) - ensure the heap ordering property is true below index, swapping out of order items
  • Run python binaryheap.py to test BinaryMinHeap class instance methods on a small example
  • Run pytest binaryheap_test.py to run the binary heap unit tests and fix any failures
  • Implement heap_sort algorithm with binary heap (Hint: 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 empty
    • length - return the number of items in the priority queue
    • enqueue(item, priority) - insert item into the priority queue in order according to priority
    • front - return the item at the front of the priority queue without removing it, or None
    • dequeue - remove and return the item at the front of the priority queue, or raise ValueError
    • push_pop(item, priority) - remove and return the item at the front of this priority queue, and insert item in order according to priority
  • Annotate methods with complexity analysis of running time and space (memory)

Stretch Challenges

  • Implement stack with priority queue (Hint: this is simple if you use priority values cleverly)
  • Implement double-ended priority queue with binary search tree (allow access to min and max item)
  • Create a generic BinaryHeap class with an initialization option to choose min or max heap order