Data Structure: Binary Heap[Max & Min]

A binary Heap is a special kind of tree. For a Binary Tree to be a Binary Heap the rule is simple-

u003cstrongu003eMax Binary Heapu003c/strongu003e- the parent node is always larger than the child nodes.
u003cstrongu003eMin Binary Heapu003c/strongu003e- The parent node is always smaller than the children.

As we can see, there are 2 types of Binary Heap- min and max.

NOTE

There is no rule for the nodes. There are no ordering guarantees between the siblings.
Binary Heap is very compact, as all nodes are as full as they can be.
The left node is filled first.

In this article, we are discussing the implementation of Binary Heap(both Min and Max) in JavaScript and TypeScript.

Check the link below to check the details of Binary Heap-

Max Binary Heap

Max Binary Heap
Max Binary Heap

If we represent this Heap(binary tree) in an array, it will look like below-

Binary Max Heap to array
Binary Max Heap to an array

Notice the indexes of the elements. Here can find the index of child elements from the index of parent, can find parent from the index of children, using the following formula-

  • If the parent element is in N th index, then-
    • Left child index: 2N + 1
    • Right child index: 2N + 2
  • If the child element is in N th index, then-
    • Parent element index: floor((N – 1) / 2)

Step #1: MaxBinaryHeap Class

Step #2: Insert Method

Step #3: Heapify Utility Method

Step #2: Extract Max Method

Full Source Code

Demo

Output:

----------- Max Binary Heap - Insert example -----------

Insert - 190| Result:  [ 190 ]
Insert - 100| Result:  [ 190, 100 ]
Insert - 120| Result:  [ 190, 100, 120 ]
Insert - 150| Result:  [ 190, 150, 120, 100 ]
Insert - 80| Result:  [ 190, 150, 120, 100, 80 ]
Insert - 110| Result:  [ 190, 150, 120, 100, 80, 110 ]
Insert - 40| Result:  [
  190, 150, 120,
  100,  80, 110,
   40
]
Insert - 10| Result:  [
  190, 150, 120, 100,
   80, 110,  40,  10
]
Insert - 55| Result:  [
  190, 150, 120, 100,
   80, 110,  40,  10,
   55
]
Insert - 60| Result:  [
  190, 150, 120, 100,
   80, 110,  40,  10,
   55,  60
]


----------- Max Binary Heap - Extract max example example -----------

Extract root | Result:  190
Extract root | Result:  150
Extract root | Result:  120
Extract root | Result:  110
Extract root | Result:  100
Extract root | Result:  80
Extract root | Result:  60
Extract root | Result:  55
Extract root | Result:  40
Extract root | Result:  10
Extract root | Result:  undefined
Extract root | Result:  undefined

Testing

Time Complexity

OperationComplexity
InsertO(log N)
Extract MaxO(log N)

Min Binary Heap

Min Binary Heap
Min Binary Heap

If we represent this Heap(binary tree) in an array, it will look like below-

Min Binary Heap to Array
Min Binary Heap to Array

Notice the indexes of the elements. Here can find the index of child elements from the index of parent, can find parent from the index of children, using the following formula-

  • If parent element is in N th index, then-
    • Left child index: 2N + 1
    • Right child index: 2N + 2
  • If child element is in N th index, then-
    • Parent element index: floor((N – 1) / 2)

Step #1: MinBinaryHeap Class

Step #2: Insert Method

Step #3: Heapify Utility Method

Step #2: Extract Min Method

Full Source Code

Demo

Output:

----------- Min Binary Heap - Insert example -----------

Insert - 10| Result:  [ 10 ]
Insert - 40| Result:  [ 10, 40 ]
Insert - 60| Result:  [ 10, 40, 60 ]
Insert - 55| Result:  [ 10, 40, 60, 55 ]
Insert - 80| Result:  [ 10, 40, 60, 55, 80 ]
Insert - 110| Result:  [ 10, 40, 60, 55, 80, 110 ]
Insert - 150| Result:  [
  10,  40,  60, 55,
  80, 110, 150
]
Insert - 100| Result:  [
  10,  40,  60,  55,
  80, 110, 150, 100
]
Insert - 120| Result:  [
   10,  40,  60,  55,
   80, 110, 150, 100,
  120
]
Insert - 190| Result:  [
   10,  40,  60,  55,
   80, 110, 150, 100,
  120, 190
]


----------- Min Binary Heap - Extract max example -----------

Extract root | Result:  10
Extract root | Result:  40
Extract root | Result:  55
Extract root | Result:  60
Extract root | Result:  80
Extract root | Result:  100
Extract root | Result:  110
Extract root | Result:  120
Extract root | Result:  150
Extract root | Result:  190
Extract root | Result:  undefined
Extract root | Result:  undefined

Testing

Time Complexity

OperationComplexity
InsertO(log N)
Extract MinO(log N)

Source Code

Use the following links to get the source code used in this article-

Related Data Structures

CommandDetails
Singly Linked List Singly Linked List Detail

Other Code Implementations

Use the following links to check the BFS implementation in other programming languages-

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.