Heaps are an often overlooked data structure, but come up quite often in interview problems. Heaps are special tree based data structures that satisfy two properties:
All nodes are ordered in a specific way, depending on the type of heap. There are two types of heaps: min heaps and max heaps.
It is a complete binary tree. A binary tree’s nodes will have at most two children: a left child, and a right child. A heap is a complete binary tree, which means that it fills each level entirely except the last level. Another way of thinking about this is that all the nodes in one level will have children before any of those nodes will have grandchildren.
In order to understand the runtimes of heap operations, it is vital to understand how insertion and deletion work within a heap.
When a new element is inserted into a heap, it is added in the next empty spot in the heap, in the right most position in the last level of the heap, in order to maintain the full shape of the heap. However, this new item may violate the other key property of the heap, its ordering.
In a min heap, if the parent of the new element is greater than it, it gets swapped with the parent. This element keeps getting bubbled up in the tree until it either reaches the root of the heap or it has been placed in the right order. This same process applies to max heaps as well, but the check to ensure that the node is in the proper position is that the parent node is greater than the new node.
When removing from a heap, the root node is always removed. Then, the last element, the right most node in the last level of the heap, is removed and set as the root. This removal process retains the heap shape, but this new ordering may violate the proper ordering of the heap.
In a min heap, if either one of the new element's children are less than their parent, the new element is swapped with the smaller of the two children. This element keeps getting bubbled down in the tree until it either reaches the last level of the heap or it has been placed in the right position. The same process applies to max heaps as well, but the ordering is such that the children are both greater than the current node.
One approach to building a heap from a list of N elements is starting with an empty heap and adding each item from a list, one at a time. This approach takes O(N log N) time because it performs N insertions, each of which takes log N time. However, this approach is suboptimal and the optimal approach of building a heap from N items only takes O(N) time!
The math and implementation behind this optimization are a bit complex, but are explained well in the Wikipedia page. It’s a good idea to get a general understanding of the optimization.
Surprisingly, this complex data structure can be represented using an array! Given that the root node will always be either the least or greatest element in the heap, we can place this element as the first element in the array. This underlying array is then filled up by going through each level of the heap, from left to right, top to bottom.
With the guarantee of fullness and the binary tree property of the heap, we can easily calculate the indices of the children and parents of each node using these formulas:
These calculations enable it to easily implement the insertion and removal procedures within the array. When we perform insertion, we need to calculate the location of the parent to swap with it to maintain the heap property. We also need to use the calculation of the left and right child in case an element needs to be swapped with one of its children during insertion operations.
In the worst case scenario, the swapping procedure for insertions and deletions will move the element through the height of the heap. Because heaps are binary trees that are guaranteed to be as complete as possible, the number of levels in the heap will be log n.
|Reading largest or smallest element||O(1)|
|Creating a heap from a list||O(n)|
Binary tree: a tree that has at most two children.
Bubble down: the process of moving an element down within the heap by swapping it with one of its children until it is placed in the proper position that satisfies the heap ordering.
Bubble up: the process of moving an element up within the heap by swapping it with its parent until it is placed in the proper position that satisfies the heap ordering.
Max heap: tree based structure in which the root node contains the largest element and all the nodes in the heap contain elements that are greater than their child nodes.
Min heap: tree based structure in which the root node contains the smallest element and all the nodes in the heap contain elements that are less than their child nodes.
To get a more thorough understanding of the internals of heaps, here are a few links to helpful resources.