

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Priority Queues
incomplete
2: Priority Queue Code
incomplete
3: Heaps
incomplete
4: Why Use Heaps?
incomplete
5: Pop
incomplete
6: Priority Queue With a Heap
incomplete
This lesson's interactive features are locked, please to keep using them
Heaps are fast because they're designed to be hyper-efficient for one task: finding the minimum or maximum element in a collection. We don't need to be able to search in the heap, or even index into it. We just need to quickly find the minimum or maximum element and maintain that order as we add and remove elements.
insert: O(log(n)). We need to do one operation per "level" of the tree, that is, once for each parent of the node being inserted.peek: O(1). We just need to look at the root node.The first level of a heap will have 1 element, i.e. the root. The second level will have up to 2 elements; the third level up to 4; etc.