Skip to content

Max heap implementation #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Data-Structures/Heap/MaxHeap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Author: Samarth Jain
* Max Heap implementation in Javascript
*/

class BinaryHeap {
constructor () {
this.heap = []
}

insert (value) {
this.heap.push(value)
this.heapify()
}

size () {
return this.heap.length
}

empty () {
return this.size() === 0
}

// using iterative approach to reorder the heap after insertion
heapify () {
let index = this.size() - 1

while (index > 0) {
const element = this.heap[index]
const parentIndex = Math.floor((index - 1) / 2)
const parent = this.heap[parentIndex]

if (parent[0] >= element[0]) break
this.heap[index] = parent
this.heap[parentIndex] = element
index = parentIndex
}
}

// Extracting the maximum element from the Heap
extractMax () {
const max = this.heap[0]
const tmp = this.heap.pop()
if (!this.empty()) {
this.heap[0] = tmp
this.sinkDown(0)
}
return max
}

// To restore the balance of the heap after extraction.
sinkDown (index) {
const left = 2 * index + 1
const right = 2 * index + 2
let largest = index
const length = this.size()

if (left < length && this.heap[left][0] > this.heap[largest][0]) {
largest = left
}
if (right < length && this.heap[right][0] > this.heap[largest][0]) {
largest = right
}
// swap
if (largest !== index) {
const tmp = this.heap[largest]
this.heap[largest] = this.heap[index]
this.heap[index] = tmp
this.sinkDown(largest)
}
}
}

const maxHeap = new BinaryHeap()
maxHeap.insert([4])
maxHeap.insert([3])
maxHeap.insert([6])
maxHeap.insert([1])
maxHeap.insert([8])
maxHeap.insert([2])

while (!maxHeap.empty()) {
const mx = maxHeap.extractMax()
console.log(mx)
}