We’re going to implement a max-heap in Python. Max-heaps efficiently keep track of the maximum value in a dataset, even as we add and remove elements.
Max-heaps are nearly identical to a min-heap; however, min-heaps keep track of the minimum value in a data set. We’ll be focusing on max-heaps in this lesson, but much of what we cover in this lesson can be applied to a min-heap as well.
Heaps enable solutions for complex problems such as finding the shortest path (Dijkstra’s Algorithm) or efficiently sorting a dataset (heapsort).
They’re an essential tool for confidently navigating some of the difficult questions posed in a technical interview.
By understanding the operations of a heap, you will have made a valuable addition to your problem-solving toolkit.
The code in script.py creates a max-heap one element at a time from a random collection of numbers.
Run the code a few times to see the max-heap operations printed to the screen.
Click “Next” when you’re ready to get started!