Implement max heap

Max heap is a complete binary tree, in which all levels are completely filled and all the nodes in the last level are as left as possible. Max heap also meets this criteria: the parent’s key is larger than both children’s keys. The largest value is at the root. Heap is related to priority queue and heapsort. This post is about how to implement max heap.

max heap

Table of Content


Concepts

A heap is a complete binary tree conceptually. The underlying data structure is an array. Each node’s position in the heap is corresponding to the index in the array. The root’s position is 0, corresponding to the index 0. The node’s position increases by 1 from left to right, from upper level to lower level.

The formula to get the parent and two children’s positions from the current node’s position in a heap:
parentPos = (pos-1)/2
left = 2*pos+1
right=2*pos+2


Insert

In MaxHeap class, there are three attributes: heap, length and maxSize. heap is an array. maxSize is the max length of the array. It is used to initialize the array. length is actual number of elements in the array. It starts from 0.

To insert a new element, you put the new value at the end of the array. Then you move it up based on its value compared to its parent. If it’s value larger than its parent, it should be switched the position with its parent, until it is below a node with a larger value and above nodes with smaller values. We call this process bubble up (or heap up).

Java

Javascript

Python


Remove

The removal operation is to remove the element with the highest value, which is the first one in the array. When you remove the first element in the array, you move the last element in the array to fill out the empty spot. But this element’s value might be smaller than its children’s. To correct this, you compare its value with its children’s values, and switch with the child with the larger value, until it is below a larger node and above a smaller one. This process is called bubble down or heap down. Bubble down is more complicated than bubble up because it has two children to compare.

Java

Javascript

Python

Since the underlying data structure is an array, you iterate through all elements to find the one matched with the key.

Java

Javascript

Python


Traversal

Traversal is to visit all nodes in a tree in some specified order. Please note the heap is a tree in conceptual context, the underlying implementation is array. Therefore there are no actual nodes (there is no root node). But you still can traverse all elements in the same way as any tree traversal, by level order, preorder, inorder and postorder.

Level order is to visit node level by level from top to bottom. The order is the same as visiting array from index 0 to its length.

Java

Javascript

Python

Preorder is to visit the root, traverse the left subtree, traverse the right subtree. The implementation is using DFS with recursion. To get the children “node”, you use the formula 2pos+1 for left child, and 2pos+2 for right child.

Java

JavaScript

Python

Inorder is to traverse the left subtree, visit the root, traverse the right subtree. The implementation is using DFS with recursion. To get the children “node”, you use the formula 2*pos+1 for left child, and 2*pos+2 for right child.

Java

Javascript

Python

Postorder is to traverse the left subtree, traverse the right subtree, visit the root. The implementation is using DFS with recursion. To get the children “node”, you use the formula 2*pos+1 for left child, and 2*pos+2 for right child.

Java

JavaScript

Python


Free download

Download MaxHeap.java
Download MaxHeap.js
Download MaxHeap.py
Implement min heap

Comments are closed