Heaps, also referred to as Binary Heaps, are advanced data structures based on Binary Trees.
A Heap is an abstract data structure since it internally uses other data structures, such as Arrays, to provide the Heap functionality.
Similar to other data structures, it is important to be thorough with the concepts of Heaps, the common operations performed on Heaps, and the efficiency of these operations.
Below interview questions on Heaps addresses some of these topics. Java programming language will be used for code snippets when applicable to depict the concepts of Heaps.
Heaps are data structures that are based on binary trees. Heaps follow the following two rules.
1. The binary tree must be a complete binary tree. In a complete binary tree -
2. The node must be ordered based on Heap property. Heap property can be one of the following.
The nodes in a Heap are build using the Heap property. Heap property can be either Min Heap or Max Heap.
Min Heap: In Min Heap the parent node keys must be less than or equal to the child node keys. Hence, in a Min Heap, the root node has the smallest key.
Max Heap: In Max Heap the parent node keys must be greater than or equal to the child node keys. Hence, in a Max Heap, the root node has the largest key.
Heap data structures are suitable for many applications and algorithms.
1. Sorting - Heapsort, one of the effieient sorting algorithm, uses Heap data structure.
2. Selection Algorithms - Selection algorithms use Heap data structures since min element or the max element can be accessed from a Heap in constant time. Others selections such as finding the median or the Kth element can be done in sub-linear time.
3. Graph Algorithms: Graph algorithms such as Prim's algorithm and Dijkstra's shortest-path algorithm uses Heaps as internal traversal data strctures.
4. Priority Queue: Priority Queue, an abstract data structure, is usually implemented with a Heap data structure.
5. K-Way merge: Heap data structures are used in K-Way merge where multiple sorted input streams are merged to a single output stream.
Heap data structure is implemented using Arrays. The first element of the array contains the root node of the Heap.
The next two elements contain the two child nodes of the root. The next four elements contain the four child nodes of the two level 2 parent nodes. And so on
Building the array in above way results in the following.
A node can be inserted into a Heap in the following way.
1. Add the new node at the end of the Heap at the first available place.
2. Check if the Heap property is violated.
3. If the Heap propert is violated then sift-up the new node until the heap property is established and the Heap is re-balanced. This is known as swim operation
When you extract from a Heap, the root node is removed and the Heap is reordered based on the Heap property.
A node can be extracted from a Heap in the following way.
1. Remove the root node and replace it with the last node of the Heap.
2. Check if the Heap property is violated.
3. If the Heap property is violated then sift-down the node until the heap property is established and the Heap is re-balanced. This is known as sink operation.
When you replace from a Heap, the root node is removed and replaced by the new node.
A node can be replaced from a Heap in the following way.
1. Remove the root node and replace it with the new node.
2. Check if the Heap property is violated.
3. If the Heap property is violated then sift-down the node until the heap property is established and the Heap is re-balanced (sink operation).