Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Heap
  • Practice Heap
  • MCQs on Heap
  • Heap Tutorial
  • Binary Heap
  • Building Heap
  • Binomial Heap
  • Fibonacci Heap
  • Heap Sort
  • Heap vs Tree
  • Leftist Heap
  • K-ary Heap
  • Advantages & Disadvantages
Open In App
Next Article:
Binary Heap
Next article icon

Introduction to Heap - Data Structure and Algorithm Tutorials

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Heap is a special tree-based data structure with the following properties:

  • It is a complete binary tree (all levels are fully filled except possibly the last, which is filled from left to right).
  • It satisfies either the max-heap property (every parent node is greater than or equal to its children) or the min-heap property (every parent node is less than or equal to its children).

Note: This definition applies specifically to binary heaps. Other types of heaps (like Fibonacci heaps or binomial heaps) may not be complete binary trees but still maintain the heap property in their own way. So if you mean binary heap, your original statement is correct and standard.

Max-Heap

The value of the root node must be the greatest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.

Min-Heap

The value of the root node must be the smallest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.

Properties of Heap:

  • The minimum or maximum element is always at the root of the heap, allowing constant-time access.
  • The relationship between a parent node at index 'i' and its children is given by the formulas: left child at index 2i+1 and right child at index 2i+2 for 0-based indexing of node numbers.
  • As the tree is complete binary, all levels are filled except possibly the last level. And the last level is filled from left to right.
  • When we insert an item, we insert it at the last available slot and then rearrange the nodes so that the heap property is maintained.
  • When we remove an item, we swap root with the last node to make sure either the max or min item is removed. Then we rearrange the remaining nodes to ensure heap property (max or min)

Operations Supported by Heap:

Operations supported by min - heap and max - heap are same. The difference is just that min-heap contains minimum element at root of the tree and max - heap contains maximum element at the root of the tree.

Heapify: It is the process to rearrange the elements to maintain the property of heap data structure. It is done when root is removed (we replace root with the last node and then call heapify to ensure that heap property is maintained) or heap is built (we call heapify from the last internal node to root) to make sure that the heap property is maintained. This operation also takes O(log n) time.

  • For max-heap, itmakes sure the maximum element is the root of that binary tree and all descendants also follow the same property.
  • For min-heap, it balances in such a way that the minimum element is the root and all descendants also follow the same property.

Insertion: When a new element is inserted into the heap, it can disrupt the heap's properties. To restore and maintain the heap structure, a heapify operation is performed. This operation ensures the heap properties are preserved and has a time complexity of O(log n).

Examples:

Assume initially heap(taking max-heap) is as follows

           8
        /   \
     4     5
   / \
1   2

Now if we insert 10 into the heap
             8
        /      \
      4       5
   /  \      /
1     2  10 

After repeatedly comparing with the parent nodes and swapping if required, the final heap will be look like this
           10
         /    \
      4      8
   /  \     /
1     2 5

Deletion:

  • If we delete the element from the heap it always deletes the root element of the tree and replaces it with the last element of the tree.
  • Since we delete the root element from the heap it will distort the properties of the heap so we need to perform heapify operations so that it maintains the property of the heap. 

It takes O(log n) time.

Example:

Assume initially heap(taking max-heap) is as follows
           15
         /   \
      5     7
   /  \
2     3

Now if we delete 15 into the heap it will be replaced by leaf node of the tree for temporary.
           3
        /   \
     5     7
   /    
2

After heapify operation final heap will be look like this
           7
        /   \
     5     3
   /   
2

getMax (For max-heap) or getMin (For min-heap):

It finds the maximum element or minimum element for max-heap and min-heap respectively and as we know minimum and maximum elements will always be the root node itself for min-heap and max-heap respectively. It takes O(1) time.

removeMin or removeMax:

This operation returns and deletes the maximum element and minimum element from the max-heap and min-heap respectively. In short, it deletes the root element of the heap binary tree.

Implementation of Heap Data Structure:-

The following code shows the implementation of a max-heap.

Let's understand the maxHeapify function in detail:-

maxHeapify is the function responsible for restoring the property of the Max Heap. It arranges the node i, and its subtrees accordingly so that the heap property is maintained.

  1. Suppose we are given an array, arr[] representing the complete binary tree. The left and the right child of ith node are in indices 2*i+1 and 2*i+2.
  2. We set the index of the current element, i, as the ‘MAXIMUM’.
  3. If arr[2 * i + 1] > arr[i], i.e., the left child is larger than the current value, it is set as ‘MAXIMUM’.
  4. Similarly if arr[2 * i + 2] > arr[i], i.e., the right child is larger than the current value, it is set as ‘MAXIMUM’.
  5. Swap the ‘MAXIMUM’ with the current element.
  6. Repeat steps 2 to 5 till the property of the heap is restored.
C++
// C++ code to depict // the implementation of a max heap.  #include <bits/stdc++.h> using namespace std;  // A class for Max Heap. class MaxHeap {     // A pointer pointing to the elements     // in the array in the heap.     int* arr;      // Maximum possible size of     // the Max Heap.     int maxSize;      // Number of elements in the     // Max heap currently.     int heapSize;  public:     // Constructor function.     MaxHeap(int maxSize);      // Heapifies a sub-tree taking the     // given index as the root.     void MaxHeapify(int);      // Returns the index of the parent     // of the element at ith index.     int parent(int i)     {         return (i - 1) / 2;     }      // Returns the index of the left child.     int lChild(int i)     {         return (2 * i + 1);     }      // Returns the index of the     // right child.     int rChild(int i)     {         return (2 * i + 2);     }      // Removes the root which in this     // case contains the maximum element.     int removeMax();      // Increases the value of the key     // given by index i to some new value.     void increaseKey(int i, int newVal);      // Returns the maximum key     // (key at root) from max heap.     int getMax()     {         return arr[0];     }      int curSize()     {         return heapSize;     }      // Deletes a key at given index i.     void deleteKey(int i);      // Inserts a new key 'x' in the Max Heap.     void insertKey(int x); };  // Constructor function builds a heap // from a given array a[] // of the specified size. MaxHeap::MaxHeap(int totSize) {     heapSize = 0;     maxSize = totSize;     arr = new int[totSize]; }  // Inserting a new key 'x'. void MaxHeap::insertKey(int x) {     // To check whether the key     // can be inserted or not.     if (heapSize == maxSize) {         cout << "\nOverflow: Could not insertKey\n";         return;     }      // The new key is initially     // inserted at the end.     heapSize++;     int i = heapSize - 1;     arr[i] = x;      // The max heap property is checked     // and if violation occurs,     // it is restored.     while (i != 0 && arr[parent(i)] < arr[i]) {         swap(arr[i], arr[parent(i)]);         i = parent(i);     } }  // Increases value of key at // index 'i' to new_val. void MaxHeap::increaseKey(int i, int newVal) {     arr[i] = newVal;     while (i != 0 && arr[parent(i)] < arr[i]) {         swap(arr[i], arr[parent(i)]);         i = parent(i);     } }  // To remove the root node which contains // the maximum element of the Max Heap. int MaxHeap::removeMax() {     // Checking whether the heap array     // is empty or not.     if (heapSize <= 0)         return INT_MIN;     if (heapSize == 1) {         heapSize--;         return arr[0];     }      // Storing the maximum element     // to remove it.     int root = arr[0];     arr[0] = arr[heapSize - 1];     heapSize--;      // To restore the property     // of the Max heap.     MaxHeapify(0);      return root; }  // In order to delete a key // at a given index i. void MaxHeap::deleteKey(int i) {     // It increases the value of the key     // to infinity and then removes     // the maximum value.     increaseKey(i, INT_MAX);     removeMax(); }  // To heapify the subtree this method // is called recursively void MaxHeap::MaxHeapify(int i) {     int l = lChild(i);     int r = rChild(i);     int largest = i;     if (l < heapSize && arr[l] > arr[i])         largest = l;     if (r < heapSize && arr[r] > arr[largest])         largest = r;     if (largest != i) {         swap(arr[i], arr[largest]);         MaxHeapify(largest);     } }  // Driver program to test above functions. int main() {     // Assuming the maximum size of the heap to be 15.     MaxHeap h(15);      // Asking the user to input the keys:     int k, i, n = 6, arr[10];     cout << "Entered 6 keys:- 3, 10, 12, 8, 2, 14 \n";     h.insertKey(3);     h.insertKey(10);     h.insertKey(12);     h.insertKey(8);     h.insertKey(2);     h.insertKey(14);      // Printing the current size     // of the heap.     cout << "The current size of the heap is "          << h.curSize() << "\n";      // Printing the root element which is     // actually the maximum element.     cout << "The current maximum element is " << h.getMax()          << "\n";      // Deleting key at index 2.     h.deleteKey(2);      // Printing the size of the heap     // after deletion.     cout << "The current size of the heap is "          << h.curSize() << "\n";      // Inserting 2 new keys into the heap.     h.insertKey(15);     h.insertKey(5);     cout << "The current size of the heap is "          << h.curSize() << "\n";     cout << "The current maximum element is " << h.getMax()          << "\n";      return 0; } 
Java
// Java code to depict // the implementation of a max heap. import java.util.Arrays; import java.util.Scanner;  public class MaxHeap {     // A pointer pointing to the elements     // in the array in the heap.     int[] arr;      // Maximum possible size of     // the Max Heap.     int maxSize;      // Number of elements in the     // Max heap currently.     int heapSize;      // Constructor function.     MaxHeap(int maxSize) {         this.maxSize = maxSize;         arr = new int[maxSize];         heapSize = 0;     }      // Heapifies a sub-tree taking the     // given index as the root.     void MaxHeapify(int i) {         int l = lChild(i);         int r = rChild(i);         int largest = i;         if (l < heapSize && arr[l] > arr[i])             largest = l;         if (r < heapSize && arr[r] > arr[largest])             largest = r;         if (largest != i) {             int temp = arr[i];             arr[i] = arr[largest];             arr[largest] = temp;             MaxHeapify(largest);         }     }      // Returns the index of the parent     // of the element at ith index.     int parent(int i) {         return (i - 1) / 2;     }      // Returns the index of the left child.     int lChild(int i) {         return (2 * i + 1);     }      // Returns the index of the     // right child.     int rChild(int i) {         return (2 * i + 2);     }      // Removes the root which in this     // case contains the maximum element.     int removeMax() {         // Checking whether the heap array         // is empty or not.         if (heapSize <= 0)             return Integer.MIN_VALUE;         if (heapSize == 1) {             heapSize--;             return arr[0];         }          // Storing the maximum element         // to remove it.         int root = arr[0];         arr[0] = arr[heapSize - 1];         heapSize--;          // To restore the property         // of the Max heap.         MaxHeapify(0);          return root;     }      // Increases value of key at     // index 'i' to new_val.     void increaseKey(int i, int newVal) {         arr[i] = newVal;         while (i != 0 && arr[parent(i)] < arr[i]) {             int temp = arr[i];             arr[i] = arr[parent(i)];             arr[parent(i)] = temp;             i = parent(i);         }     }      // Returns the maximum key     // (key at root) from max heap.     int getMax() {         return arr[0];     }      int curSize() {         return heapSize;     }      // Deletes a key at given index i.     void deleteKey(int i) {         // It increases the value of the key         // to infinity and then removes         // the maximum value.         increaseKey(i, Integer.MAX_VALUE);         removeMax();     }      // Inserts a new key 'x' in the Max Heap.     void insertKey(int x) {         // To check whether the key         // can be inserted or not.         if (heapSize == maxSize) {             System.out.println("\nOverflow: Could not insertKey\n");             return;         }          // The new key is initially         // inserted at the end.         heapSize++;         int i = heapSize - 1;         arr[i] = x;          // The max heap property is checked         // and if violation occurs,         // it is restored.         while (i != 0 && arr[parent(i)] < arr[i]) {             int temp = arr[i];             arr[i] = arr[parent(i)];             arr[parent(i)] = temp;             i = parent(i);         }     }      // Driver program to test above functions.     public static void main(String[] args) {         // Assuming the maximum size of the heap to be 15.         MaxHeap h = new MaxHeap(15);          // Asking the user to input the keys:         int k, i, n = 6;         System.out.println("Entered 6 keys:- 3, 10, 12, 8, 2, 14 \n");         h.insertKey(3);         h.insertKey(10);         h.insertKey(12);         h.insertKey(8);         h.insertKey(2);         h.insertKey(14);          // Printing the current size         // of the heap.         System.out.println("The current size of the heap is "                 + h.curSize() + "\n");          // Printing the root element which is         // actually the maximum element.         System.out.println("The current maximum element is " + h.getMax()                 + "\n");          // Deleting key at index 2.         h.deleteKey(2);          // Printing the size of the heap         // after deletion.         System.out.println("The current size of the heap is "                 + h.curSize() + "\n");          // Inserting 2 new keys into the heap.         h.insertKey(15);         h.insertKey(5);         System.out.println("The current size of the heap is "                 + h.curSize() + "\n");         System.out.println("The current maximum element is " + h.getMax()                 + "\n");     } } 
Python
# Python code to depict # the implementation of a max heap.  class MaxHeap:     # A pointer pointing to the elements     # in the array in the heap.     arr = []      # Maximum possible size of     # the Max Heap.     maxSize = 0      # Number of elements in the     # Max heap currently.     heapSize = 0      # Constructor function.     def __init__(self, maxSize):         self.maxSize = maxSize         self.arr = [None]*maxSize         self.heapSize = 0      # Heapifies a sub-tree taking the     # given index as the root.     def MaxHeapify(self, i):         l = self.lChild(i)         r = self.rChild(i)         largest = i         if l < self.heapSize and self.arr[l] > self.arr[i]:             largest = l         if r < self.heapSize and self.arr[r] > self.arr[largest]:             largest = r         if largest != i:             temp = self.arr[i]             self.arr[i] = self.arr[largest]             self.arr[largest] = temp             self.MaxHeapify(largest)      # Returns the index of the parent     # of the element at ith index.     def parent(self, i):         return (i - 1) // 2      # Returns the index of the left child.     def lChild(self, i):         return (2 * i + 1)      # Returns the index of the     # right child.     def rChild(self, i):         return (2 * i + 2)      # Removes the root which in this     # case contains the maximum element.     def removeMax(self):         # Checking whether the heap array         # is empty or not.         if self.heapSize <= 0:             return None         if self.heapSize == 1:             self.heapSize -= 1             return self.arr[0]          # Storing the maximum element         # to remove it.         root = self.arr[0]         self.arr[0] = self.arr[self.heapSize - 1]         self.heapSize -= 1          # To restore the property         # of the Max heap.         self.MaxHeapify(0)          return root      # Increases value of key at     # index 'i' to new_val.     def increaseKey(self, i, newVal):         self.arr[i] = newVal         while i != 0 and self.arr[self.parent(i)] < self.arr[i]:             temp = self.arr[i]             self.arr[i] = self.arr[self.parent(i)]             self.arr[self.parent(i)] = temp             i = self.parent(i)      # Returns the maximum key     # (key at root) from max heap.     def getMax(self):         return self.arr[0]      def curSize(self):         return self.heapSize      # Deletes a key at given index i.     def deleteKey(self, i):         # It increases the value of the key         # to infinity and then removes         # the maximum value.         self.increaseKey(i, float("inf"))         self.removeMax()      # Inserts a new key 'x' in the Max Heap.     def insertKey(self, x):         # To check whether the key         # can be inserted or not.         if self.heapSize == self.maxSize:             print("\nOverflow: Could not insertKey\n")             return          # The new key is initially         # inserted at the end.         self.heapSize += 1         i = self.heapSize - 1         self.arr[i] = x          # The max heap property is checked         # and if violation occurs,         # it is restored.         while i != 0 and self.arr[self.parent(i)] < self.arr[i]:             temp = self.arr[i]             self.arr[i] = self.arr[self.parent(i)]             self.arr[self.parent(i)] = temp             i = self.parent(i)   # Driver program to test above functions. if __name__ == '__main__':     # Assuming the maximum size of the heap to be 15.     h = MaxHeap(15)      # Asking the user to input the keys:     k, i, n = 6, 0, 6     print("Entered 6 keys:- 3, 10, 12, 8, 2, 14 \n")     h.insertKey(3)     h.insertKey(10)     h.insertKey(12)     h.insertKey(8)     h.insertKey(2)     h.insertKey(14)      # Printing the current size     # of the heap.     print("The current size of the heap is "           + str(h.curSize()) + "\n")      # Printing the root element which is     # actually the maximum element.     print("The current maximum element is " + str(h.getMax())           + "\n")      # Deleting key at index 2.     h.deleteKey(2)      # Printing the size of the heap     # after deletion.     print("The current size of the heap is "           + str(h.curSize()) + "\n")      # Inserting 2 new keys into the heap.     h.insertKey(15)     h.insertKey(5)     print("The current size of the heap is "           + str(h.curSize()) + "\n")     print("The current maximum element is " + str(h.getMax())           + "\n") 
JavaScript
// JavaScript code to depict // the implementation of a max heap.  class MaxHeap {     constructor(maxSize) {         // the array in the heap.         this.arr = new Array(maxSize).fill(null);          // Maximum possible size of         // the Max Heap.         this.maxSize = maxSize;          // Number of elements in the         // Max heap currently.         this.heapSize = 0;     }      // Heapifies a sub-tree taking the     // given index as the root.     MaxHeapify(i) {         const l = this.lChild(i);         const r = this.rChild(i);         let largest = i;         if (l < this.heapSize && this.arr[l] > this.arr[i]) {             largest = l;         }         if (r < this.heapSize && this.arr[r] > this.arr[largest]) {             largest = r;         }         if (largest !== i) {             const temp = this.arr[i];             this.arr[i] = this.arr[largest];             this.arr[largest] = temp;             this.MaxHeapify(largest);         }     }      // Returns the index of the parent     // of the element at ith index.     parent(i) {         return Math.floor((i - 1) / 2);     }      // Returns the index of the left child.     lChild(i) {         return 2 * i + 1;     }      // Returns the index of the     // right child.     rChild(i) {         return 2 * i + 2;     }      // Removes the root which in this     // case contains the maximum element.     removeMax() {         // Checking whether the heap array         // is empty or not.         if (this.heapSize <= 0) {             return null;         }         if (this.heapSize === 1) {             this.heapSize -= 1;             return this.arr[0];         }          // Storing the maximum element         // to remove it.         const root = this.arr[0];         this.arr[0] = this.arr[this.heapSize - 1];         this.heapSize -= 1;          // To restore the property         // of the Max heap.         this.MaxHeapify(0);          return root;     }      // Increases value of key at     // index 'i' to new_val.     increaseKey(i, newVal) {         this.arr[i] = newVal;         while (i !== 0 && this.arr[this.parent(i)] < this.arr[i]) {             const temp = this.arr[i];             this.arr[i] = this.arr[this.parent(i)];             this.arr[this.parent(i)] = temp;             i = this.parent(i);         }     }      // Returns the maximum key     // (key at root) from max heap.     getMax() {         return this.arr[0];     }      curSize() {         return this.heapSize;     }      // Deletes a key at given index i.     deleteKey(i) {         // It increases the value of the key         // to infinity and then removes         // the maximum value.         this.increaseKey(i, Infinity);         this.removeMax();     }      // Inserts a new key 'x' in the Max Heap.     insertKey(x) {         // To check whether the key         // can be inserted or not.         if (this.heapSize === this.maxSize) {             console.log("\nOverflow: Could not insertKey\n");             return;         }          let i = this.heapSize;         this.arr[i] = x;          // The new key is initially         // inserted at the end.         this.heapSize += 1;            // The max heap property is checked         // and if violation occurs,         // it is restored.         while (i !== 0 && this.arr[this.parent(i)] < this.arr[i]) {             const temp = this.arr[i];             this.arr[i] = this.arr[this.parent(i)];             this.arr[this.parent(i)] = temp;             i = this.parent(i);         }     } }   // Driver program to test above functions.  // Assuming the maximum size of the heap to be 15. const h = new MaxHeap(15);  // Asking the user to input the keys: console.log("Entered 6 keys:- 3, 10, 12, 8, 2, 14 \n");  h.insertKey(3); h.insertKey(10); h.insertKey(12); h.insertKey(8); h.insertKey(2); h.insertKey(14);   // Printing the current size // of the heap. console.log(     "The current size of the heap is " + h.curSize() + "\n" );   // Printing the root element which is // actually the maximum element. console.log(     "The current maximum element is " + h.getMax() + "\n" );   // Deleting key at index 2. h.deleteKey(2);   // Printing the size of the heap // after deletion. console.log(     "The current size of the heap is " + h.curSize() + "\n" );   // Inserting 2 new keys into the heap. h.insertKey(15); h.insertKey(5);  console.log(     "The current size of the heap is " + h.curSize() + "\n" );  console.log(     "The current maximum element is " + h.getMax() + "\n" );  // Contributed by sdeadityasharma 

Output
Entered 6 keys:- 3, 10, 12, 8, 2, 14  The current size of the heap is 6 The current maximum element is 14 The current size of the heap is 5 The current size of the heap is 7 The current maximum element is 15

Please refer the following articles for more details about Heap Data Structure

  • Advantages and Disadvantages of Heap
  • Applications of Heap Data Structure

Library Implementations of Heap or Priority Queue

  • Heap in C++ STL
  • priority_queue in C++
  • PriorityQueue in Java
  • heapq in Python

Next Article
Binary Heap

H

himanshuparihar1600
Improve
Article Tags :
  • Heap
  • DSA
  • Tutorials
Practice Tags :
  • Heap

Similar Reads

    Heap Data Structure
    A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is greater than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.Basics
    2 min read
    Introduction to Heap - Data Structure and Algorithm Tutorials
    A Heap is a special tree-based data structure with the following properties:It is a complete binary tree (all levels are fully filled except possibly the last, which is filled from left to right).It satisfies either the max-heap property (every parent node is greater than or equal to its children) o
    15+ min read
    Binary Heap
    A Binary Heap is a complete binary tree that stores data efficiently, allowing quick access to the maximum or minimum element, depending on the type of heap. It can either be a Min Heap or a Max Heap. In a Min Heap, the key at the root must be the smallest among all the keys in the heap, and this pr
    13 min read
    Advantages and Disadvantages of Heap
    Advantages of Heap Data StructureTime Efficient: Heaps have an average time complexity of O(log n) for inserting and deleting elements, making them efficient for large datasets. We can convert any array to a heap in O(n) time. The most important thing is, we can get the min or max in O(1) timeSpace
    2 min read
    Time Complexity of building a heap
    Consider the following algorithm for building a Heap of an input array A. A quick look over the above implementation suggests that the running time is O(n * lg(n)) since each call to Heapify costs O(lg(n)) and Build-Heap makes O(n) such calls. This upper bound, though correct, is not asymptotically
    2 min read
    Applications of Heap Data Structure
    Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used. Priority Queues: Heaps are commonly used to implement priority queues, where elements with higher prior
    2 min read
    Comparison between Heap and Tree
    What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Types of Heap Data Structure: 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 sa
    3 min read
    When building a Heap, is the structure of Heap unique?
    What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property sta
    4 min read

    Some other type of Heap

    Binomial Heap
    The main application of Binary Heap is to implement a priority queue. Binomial Heap is an extension of Binary Heap that provides faster union or merge operation with other operations provided by Binary Heap. A Binomial Heap is a collection of Binomial Trees What is a Binomial Tree? A Binomial Tree o
    15 min read
    Fibonacci Heap | Set 1 (Introduction)
    INTRODUCTION:A Fibonacci heap is a data structure used for implementing priority queues. It is a type of heap data structure, but with several improvements over the traditional binary heap and binomial heap data structures.The key advantage of a Fibonacci heap over other heap data structures is its
    5 min read
    Leftist Tree / Leftist Heap
    INTRODUCTION:A leftist tree, also known as a leftist heap, is a type of binary heap data structure used for implementing priority queues. Like other heap data structures, it is a complete binary tree, meaning that all levels are fully filled except possibly the last level, which is filled from left
    15+ min read
    K-ary Heap
    Prerequisite - Binary Heap K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2. Just like binary heap, it follows two properties: Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to r
    15 min read

    Easy problems on Heap

    Check if a given Binary Tree is a Heap
    Given a binary tree, check if it has heap property or not, Binary tree needs to fulfil the following two conditions for being a heap: It should be a complete tree (i.e. Every level of the tree, except possibly the last, is completely filled, and all nodes are as far left as possible.).Every node’s v
    15+ min read
    How to check if a given array represents a Binary Heap?
    Given an array, how to check if the given array represents a Binary Max-Heap.Examples: Input: arr[] = {90, 15, 10, 7, 12, 2} Output: True The given array represents below tree 90 / \ 15 10 / \ / 7 12 2 The tree follows max-heap property as every node is greater than all of its descendants. Input: ar
    11 min read
    Iterative HeapSort
    HeapSort is a comparison-based sorting technique where we first build Max Heap and then swap the root element with the last element (size times) and maintains the heap property each time to finally make it sorted. Examples: Input : 10 20 15 17 9 21 Output : 9 10 15 17 20 21 Input: 12 11 13 5 6 7 15
    11 min read
    Find k largest elements in an array
    Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai
    15+ min read
    K’th Smallest Element in Unsorted Array
    Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
    15 min read
    Height of a complete binary tree (or Heap) with N nodes
    Consider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we tak
    3 min read
    Heap Sort for decreasing order using min heap
    Given an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1}Output : arr[] = {10, 5, 3, 1}Input : arr[] = {1, 50, 100, 25}Output : arr[] = {100, 50, 25, 1}Prerequisite: Heap sort using min heap.Using Min Heap Implementation - O(n Log n) Time
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences