Max Heap: Introduction

This lesson will give a brief introduction about Max Heap and how elements are inserted or removed from Max-Heap.

Building a Max-Heap

As mentioned in the previous lesson, a Max Heap follows the Max Heap property, which means the key at the parent node is always greater than keys at both child nodes. The following steps illustrate how we build a Max Heap:

  1. Create a new node at the end of the heap.
  2. Assign a new value to the node.
  3. Compare the value of this child node with its parent.
  4. If the value of the parent is less than that of the child, then swap them.
  5. Repeat steps 3 & 4 until the Heap property holds.

Implementing a

...