Skip to content

Heaps #155

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 3 commits into from
Nov 4, 2019
Merged

Heaps #155

Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const MaxHeap = require('.');

describe('MaxHeap', () => {
it('Should be a class', () => {
expect(typeof MaxHeap.prototype.constructor).toEqual('function');
});

const mh = new MaxHeap();

beforeEach(() => {
mh.destroy();
});

it('Should create an instance of MaxHeap', () => {
expect(mh instanceof MaxHeap).toEqual(true);
});

it('Should add an element to the MaxHeap', () => {
mh.add(10);
expect(mh.getMax()).toEqual(10);
});

it('Should keep the largest element at the root', () => {
[12, 5, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
});

it('Should retain Heap properties after removal of an element', () => {
[12, 45, 1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(45);
mh.remove();
expect(mh.getMax()).toEqual(34);
});

it('Should return `null` when heap is empty', () => {
[1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
mh.remove();
mh.remove();
expect(mh.getMax()).toEqual(null);
});

it('Should return the elelment value on `remove()`', () => {
[1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
expect(mh.remove()).toEqual(34);
expect(mh.remove()).toEqual(1);
expect(mh.getMax()).toEqual(null);
});
});
78 changes: 78 additions & 0 deletions src/_DataStructures_/Heaps/MaxHeap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class MaxHeap {
constructor() {
this.heap = [];
}

add(element) {
this.heap.push(element);
// check for the parent element & swap if required
// eslint-disable-next-line no-underscore-dangle
this.__traverseUpAndSwap(this.heap.length - 1);
}

getMax() {
return this.heap[0] || null;
}

remove() {
// return the element at the root
const max = this.heap[0] || null;
if (this.heap.length > 1) {
// move the leaf to the root
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.splice(this.heap.length - 1, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splice worst case should be O(n) . Instead of splice , pop will work in O(1).

// restore the heapify property
// eslint-disable-next-line no-underscore-dangle
this.__heapify(0);
return max;
}

if (this.heap.length === 1) {
this.heap.splice(this.heap.length - 1, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splice will work here in O(1) because heap size is 1 but you can use pop.

return max;
}

return max;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this line because it will never execute and it will increase code coverage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}

__heapify(index) {
const left = index * 2;
const right = index * 2 + 1;
let largest = index;

if (this.heap.length > left && this.heap[largest] < this.heap[left]) {
largest = left;
}

if (this.heap.length > right && this.heap[largest] < this.heap[right]) {
largest = right;
}

if (largest !== index) {
const temp = this.heap[largest];
this.heap[largest] = this.heap[index];
this.heap[index] = temp;
// eslint-disable-next-line no-underscore-dangle
this.__heapify(largest);
}
}

__traverseUpAndSwap(index) {
if (index <= 0) return;
const parent = Math.floor(index / 2);

if (this.heap[parent] < this.heap[index]) {
const temp = this.heap[parent];
this.heap[parent] = this.heap[index];
this.heap[index] = temp;
// eslint-disable-next-line no-underscore-dangle
this.__traverseUpAndSwap(parent);
}
}

destroy() {
this.heap = [];
}
}

module.exports = MaxHeap;