JavaAlgorithms/DataStructures/Heaps
2021-10-09 11:48:07 +03:00
..
EmptyHeapException.java
GenericHeap
Heap.java
HeapElement.java
MaxHeap.java
MinHeap.java
MinPriorityQueue.java
Readme.md Add Readme.md for Heap Data Structure (#2440) 2021-10-09 11:48:07 +03:00

HEAP DATA STRUCTURE

A Heap is a special Tree-based data structure in which the tree is a complete binary tree.

Complete Binary Tree

A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node should be completely filled, and all the nodes should be left-justified.

                            10
                          /    \
                        20      30
                       /  \    
                      40   50
                      
                    COMPLETE BINARY TREE

Types of Heap

Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of its children. The same property must be recursively true for all sub-trees in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of its children. The same property must be recursively true for all sub-trees in that Binary Tree.

                            10
                          /    \
                        20      30
                       /  \    /  \
                      40  50  60  70
                      
                          MIN HEAP
                            70
                          /    \
                        50      60
                       /  \    /  \
                      40  30  10   20
                      
                          MAX HEAP

Min Heap Construction Algorithm

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

                            10                         10                     10
                          /    \                     /   \                  /    \
                        20      30    ------>      20     30   ------>     20     15
                       /  \                       /  \   /                /  \    /  
                      40  50                    40   50  15              40  50  30
                                          
                                          

Min Heap Deletion Algorithm

Step 1  Remove root node.
Step 2  Move the last element of last level to root.
Step 3  Compare the value of this child node with its parent.
Step 4  If value of parent is more than child, then swap them.
Step 5  Repeat step 3 & 4 until Heap property holds.
Delete 10

                            10                        50                     20                   20
                          /    \                     /   \                  /   \                /  \
                        20      30    ------>      20     30   ------>     50    30   ------>   40   30
                       /  \                       /                       /                    /
                      40  50                    40                       40                   50
                                          
                                          

Time Complexity (Min Heap)

Operations Sorted Array UnSorted Array Heap
Add O(N) O(1) O(logN)
Delete Minimum O(N) O(N) O(logN)
Get Minimum O(1) O(N) O(1)

Applications of Heap Data Structure

Heapsort: Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used.

Priority Queues: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also in O(logn) time which is a O(n) operation in Binary Heap. Heap Implemented priority queues are used in Graph algorithms like Prims Algorithm and Dijkstras algorithm.

Order statistics: The Heap data structure can be used to efficiently find the kth smallest (or largest) element in an array.