Key Concepts

Review core concepts you need to learn to master this subject

Heaps as Binary Trees

A proper representation of a heap is a complete binary tree, which is a tree whose nodes have at most two children, and whose levels are filled completely from left to right (with no gaps in children). It’s possible for the last level to be semi-completely filled, but all of its nodes are as far left as possible.

Heaps: Conceptual
Lesson 1 of 2
  1. 1
    Heaps are used to maintain a maximum or minimum value in a dataset. Our examples use numbers since this is a straight-forward value, but heaps have many practical applications. Imagine you have a…
  2. 2
    We can picture min-heaps as binary trees, where each node has at most two children. As we add elements to the heap, they’re added from left to right until we’ve filled the entire level. At th…
  3. 3
    Sometimes you will add an element to the heap that violates the heap’s essential properties. We’re adding 3 as a left child of 11, which violates the min-heap property that children must be larg…
  4. 4
    Maintaining a minimum value is no good if we can never retrieve it, so let’s explore how to remove the root node. In the diagram, you can see removing the top node itself would be messy: there wo…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo