A Binary Heap is a Binary Tree with following properties.

  1. It’s a Complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible).
  2. This property of Binary Heap makes them suitable to be stored in an array.
  3. A Binary Heap is either Min Heap or Max Heap.
    • Min Binary Heap -
      • ​​The key at root/parent must be minimum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree.
    • Max Binary Heap -
      • It is similar to Min Heap, where root/parent must be maximum accross all level


Heap is a balanced binary tree.

The Order property:

For every node n, the value in n is greater (Max Heap) / lesser (Min Heap) than or equal to the values in its children (and thus is also greater/lesser than or equal to all of the values in its subtrees).

The Shape property:

  • All leaves are either at depth d or d-1 (for some value d).
  • All of the leaves at depth d-1 are to the right of the leaves at depth d.
    • There is at most 1 node with just 1 child.
    • That child is the left child of its parent, and it is the rightmost leaf at depth d.


  • Get Minimum in Min Heap or Get Maximum in Max Heap: O(1) 
  • Extract Minimum Min Heap or Extract Maximum in Max Heap: O(Log n)
  • Decrease Key in Min Heap or Increase Key in Max Heap: O(Log n)
  • Insert: O(Log n) 
  • Delete: O(Log n)


  • Priority Queues are also used in Dijkstra's shortest path algorithm and Prim’s Minimum Spanning Tree algorithms.
    • Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time
  • Scheduling processes in operating systems
  • Heap Sort uses Binary Heap to sort an array in O(nLogn) time
  • Graph Algorithms
    •  Dijkstra’s Shortest Path
    • Prim’s Minimum Spanning Tree.
  • Useful to solve
    • K’th Largest Element in an array.
    • Sort an almost sorted array
    • Merge K Sorted Arrays.


The Heap data structure can be used to efficiently find the k’th smallest (or largest) element in an array.
Heap is a special data structure and it cannot be used for searching of a particular element.

Operations on Min/Max Heap:

  1. getMini()/getMax():
    • It returns the root element of Min/Max Heap. Time Complexity of this operation is O(1).

  2. extractMin()/extractMax():

    • Removes the minimum/maximum element from Min/Max Heap. Time Complexity of this Operation is O(Logn) as this operation needs to maintain the heap property (by calling heapify()) after removing root.

  3. decreaseKey()/increaseKey():

    • Decreases/Increases value of the key. The time complexity of this operation is O(Logn). If the decreases/increases a key value of a node is greater/lesser than a parent of the node, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.

  4. insert():
    • Inserting a new key takes O(Logn) time. We add a new key at the end of the tree. If new key is greater/lesser than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.

  5. delete():
    • Deleting a key also takes O(Logn) time. We replace the key to be deleted with minimum/maximum infinite by calling decreaseKey()/increaseKey(). After decreaseKey()/increaseKey(), the minus/max.



Time Complexity

Space Complexity





Data Structure 


Binary Heap

Θ(1) Θ(log(n)) Θ(log(n)) Θ(log(n)) O(1) O(log(n)) O(log(n)) O(log(n)) O(n)