BinaryHeap: An Implicit Binary Tree

Learn the binary heap and its operations.

Heap overview

Here, we discuss two implementations of the extremely useful priority Queue data structure. Both of these structures are a special kind of binary tree called a heap, which means “a disorganized pile.” This is in contrast to binary search trees that can be thought of as a highly organized pile.

The first heap implementation uses an array to simulate a complete binary tree. This very fast implementation is the basis of one of the fastest known sorting algorithms, namely heapsort. The second implementation is based on more flexible binary trees. It supports a meld(h) operation that allows the priority queue to absorb the elements of a second priority queue h.

BinaryHeap: An implicit binary tree

Our first implementation of a (priority) Queue is based on a technique that is over four hundred years old. Eytzinger’s method allows us to represent a complete binary tree as an array by laying out the nodes of the tree in breadth-first order. In this way, the root is stored at position 00, the root’s left child is stored at position 11, the root’s right child at position 22, the left child of the left child of the root is stored at position 33, and so on.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy