Search⌘ K

Heap

Explore the concepts of heap data structures used in competitive programming. Understand min and max heap properties, how heaps are represented using arrays, and their performance characteristics. This lesson prepares you to implement heaps effectively in coding challenges.

We'll cover the following...

Structure

Heap is a complete binary tree. There are two types of heaps

  • Min Heap
  • Max Heap

Min heap

In min-heap, each node is smaller than its children. This means the root has the smallest key among all the nodes. Similarly, the left child of the root is the smallest among all nodes in the left subtree of the root, and so on.

Similarly, in a max heap, the key for each node is greater than both its children.

Representation

Due to the structure of the heap, it can be represented using an array. This means you don’t have to deal with pointers and the code is much simpler.

Let’s see how an array is visualized as a heap.

  • A[0]A[0]
...