Learn
Nice work! You’ve implemented a MaxHeap
class in Python. Heaps are useful because they’re efficient in maintaining their heap properties.
Let’s recap what we learned:
- A max-heap tracks the maximum element as the element at index
1
within an internal Python list. - Max-heaps must maintain the heap property that the parent values must be greater than their children.
- When adding elements, we use
.heapify_up()
to compare the new element with its parent; if it violates the heap property, then we must swap the two values.
In the next lesson, we’ll learn how to extract the root value of a heap as well as how to sort data using heapsort!
Instructions
Take a look at the final code in max_heap.py and script.py. Feel free to play around and edit the code to get a better understanding of heaps in Python!
Sign up to start coding
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.