Heaps Heaps are an awesome data structure. A heap data structure, especially a priority queue implemented as a heap, has real-life equivalents where elements are served based on priority, not arrival time.
Important use cases:
Emergency Room Triage Patients are treated based on the severity of their condition (priority), not by the order they arrived.
Task Scheduling in Operating Systems Tasks with higher priority (e.g., real-time processes) are scheduled before lower-priority ones.
Sample Code
Main functions to be used
heapq.heapify(input_list) : Pass the list that needs to heaped. Heapify will be default only do the min heap. So if we want to do a max heap, reverse the list, before passing to tbe heapified.
heapq.heappush(input_list, value) : Useful to push a new element. For min heap, pass the same value. For max heap, negate and pass in.
heapq.heappop(input_list) : Remove the lowest / max element from the heap. For lowest element, nothing additional to be done. For the max_element, after the top is removed, negate it to get the actual value