Binary heap data structure is a special binary tree. It can find the maximum and minimum efficiently and quickly. It is often used in priority queue and well-known heap sorting algorithm.

## Two fork pile

Binary heap has the following two characteristics:

1. It is a complete binary tree. Each layer of the number has left and right sub-nodes (except the leaf nodes of the last layer), and the leaf nodes of the last layer are left sub-nodes as far as possible.
2. The binary heap is either the smallest heap or the largest heap. All nodes are greater than or equal to (the largest heap) or less than or equal to (the smallest heap) each of its children.

### Create the smallest heap class

``````class MinHeap {
constructor(compareFn = defaultCompare) {
this.compareFn = compareFn;
this.heap = [];
}
}``````

#### Array Representation of Binary Heap

``````
static getLeftIndex(index) {
return (2 * index) + 1;
}

static getRightIndex(index) {
return (2 * index) + 2;
}

static getParentIndex(index) {
if (index === 0) {
return undefined;
}
return Math.floor((index - 1) / 2);
}

size() {
return this.heap.length;
}

isEmpty() {
return this.size() <= 0;
}

clear() {
this.heap = [];
}
``````

#### Find the minimum or maximum binary heap

`````` findMinimum() {
return this.isEmpty() ? undefined : this.heap[0];
}``````

#### Implementation of Exchange Function

``````function swap(array, a, b) {
/* const temp = array[a];
array[a] = array[b];
array[b] = temp; */
[array[a], array[b]] = [array[b], array[a]];
}``````

#### Insert new values into the heap

``````insert(value) {
if (value != null) {
const index = this.heap.length;
this.heap.push(value);
this.siftUp(index);
return true;
}
return false;
};
//Up shift operation
siftUp(index) {
let parent = this.getParentIndex(index);
while (
index > 0
&& this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN
) {
swap(this.heap, parent, index);
index = parent;
parent = this.getParentIndex(index);
}
}``````

#### Derived maximum or minimum in binary heap

``````extract() {
if (this.isEmpty()) {
return undefined;
}
if (this.size() === 1) {
return this.heap.shift();
}
const removedValue = this.heap[0];
this.heap[0] = this.heap.pop();
this.siftDown(0);
return removedValue;
};
//Downshift operation
siftDown(index) {
let element = index;
const left = MinHeap.getLeftIndex(index);
const right = this.getRightIndex(index);
const size = this.size();
if (
left < size
&& this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN
) {
element = left;
}
if (
right < size
&& this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN
) {
element = right;
}
if (index !== element) {
swap(this.heap, index, element);
this.siftDown(element);
}
}``````

### Create the maximum heap class

``````class MaxHeap extends MinHeap {
constructor(compareFn = defaultCompare) {
super(compareFn);
this.compareFn = compareFn;
this.compareFn = reverseCompare(compareFn);
}
}``````

Other operations are the same as the smallest heap classes, so I won't go into much detail here.

### Heap sorting algorithm

``````heapify(array) {
if (array) {
this.heap = array;
}
const maxIndex = Math.floor(this.size() / 2) - 1;
for (let i = 0; i <= maxIndex; i++) {
this.siftDown(i);
}
return this.heap;
};
getAsArray() {
return this.heap;
};
//Constructing the Maximum Heap Function
function buildMaxHeap(array, compareFn) {
for (let i = Math.floor(array.length / 2);i >= 0; i -= 1){
heapify(array, i, array.length, compareFn);
return array;
}
}
//Implementation of Heap Sorting Algorithms
function heapSort(array, compareFn = defaultCompare) {
let heapSize = array.length;
//Using arrays to create a maximum heap for source data
buildMaxHeap(array, compareFn);
while(heapSize > 1){
//After creating the maximum heap, the maximum value is stored at the first location of the heap. We replace it with the last value of the heap, and the heap size is -1.
swap(array, 0, --heapSize);
//Move down the root node of the heap and repeat step 2 until the heap size is 1
heapify(array, 0, heapSize, compareFn);
}
return array;
}``````