From 0ca88dbc8b136b651f79b9dc30806e65d92ab823 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 3 Nov 2019 11:09:02 +0530 Subject: [PATCH 1/3] update: added MaxHeap --- src/_DataStructures_/Heaps/MaxHeap/index.js | 74 +++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/_DataStructures_/Heaps/MaxHeap/index.js diff --git a/src/_DataStructures_/Heaps/MaxHeap/index.js b/src/_DataStructures_/Heaps/MaxHeap/index.js new file mode 100644 index 00000000..18119c6b --- /dev/null +++ b/src/_DataStructures_/Heaps/MaxHeap/index.js @@ -0,0 +1,74 @@ +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); + // 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); + return max; + } + + return max; + } + + __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); + } + } +} + +module.exports = MaxHeap; From 27544fc2abe9f2027ace94c97919709bd4839573 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 3 Nov 2019 11:33:25 +0530 Subject: [PATCH 2/3] update: added unit tests --- .../Heaps/MaxHeap/MaxHeap.test.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js diff --git a/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js new file mode 100644 index 00000000..e207b564 --- /dev/null +++ b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js @@ -0,0 +1,50 @@ +const MaxHeap = require('.'); + +describe('MaxHeap', () => { + it('Should be a class', () => { + expect(typeof MaxHeap.prototype.constructor).toEqual('function'); + }); + + it('Should create an instance of MaxHeap', () => { + const mh = new MaxHeap(); + expect(mh instanceof MaxHeap).toEqual(true); + }); + + it('Should add an element to the MaxHeap', () => { + const mh = new MaxHeap(); + mh.add(10); + expect(mh.getMax()).toEqual(10); + }); + + it('Should keep the largest element at the root', () => { + const mh = new MaxHeap(); + [12, 5, 34].forEach(el => mh.add(el)); + expect(mh.getMax()).toEqual(34); + }); + + it('Should retain Heap properties after removal of an element', () => { + const mh = new MaxHeap(); + [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', () => { + const mh = new MaxHeap(); + [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()`', () => { + const mh = new MaxHeap(); + [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); + }); +}); From 6a5e13aa537ba04ed5c88f2bb6ad6d82dcc9dc55 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 3 Nov 2019 11:37:44 +0530 Subject: [PATCH 3/3] refactor: added destroy(), used beforeEach() --- src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js | 12 ++++++------ src/_DataStructures_/Heaps/MaxHeap/index.js | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js index e207b564..181390ed 100644 --- a/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js +++ b/src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js @@ -5,25 +5,27 @@ describe('MaxHeap', () => { expect(typeof MaxHeap.prototype.constructor).toEqual('function'); }); + const mh = new MaxHeap(); + + beforeEach(() => { + mh.destroy(); + }); + it('Should create an instance of MaxHeap', () => { - const mh = new MaxHeap(); expect(mh instanceof MaxHeap).toEqual(true); }); it('Should add an element to the MaxHeap', () => { - const mh = new MaxHeap(); mh.add(10); expect(mh.getMax()).toEqual(10); }); it('Should keep the largest element at the root', () => { - const mh = new MaxHeap(); [12, 5, 34].forEach(el => mh.add(el)); expect(mh.getMax()).toEqual(34); }); it('Should retain Heap properties after removal of an element', () => { - const mh = new MaxHeap(); [12, 45, 1, 34].forEach(el => mh.add(el)); expect(mh.getMax()).toEqual(45); mh.remove(); @@ -31,7 +33,6 @@ describe('MaxHeap', () => { }); it('Should return `null` when heap is empty', () => { - const mh = new MaxHeap(); [1, 34].forEach(el => mh.add(el)); expect(mh.getMax()).toEqual(34); mh.remove(); @@ -40,7 +41,6 @@ describe('MaxHeap', () => { }); it('Should return the elelment value on `remove()`', () => { - const mh = new MaxHeap(); [1, 34].forEach(el => mh.add(el)); expect(mh.getMax()).toEqual(34); expect(mh.remove()).toEqual(34); diff --git a/src/_DataStructures_/Heaps/MaxHeap/index.js b/src/_DataStructures_/Heaps/MaxHeap/index.js index 18119c6b..bf94e9d6 100644 --- a/src/_DataStructures_/Heaps/MaxHeap/index.js +++ b/src/_DataStructures_/Heaps/MaxHeap/index.js @@ -69,6 +69,10 @@ class MaxHeap { this.__traverseUpAndSwap(parent); } } + + destroy() { + this.heap = []; + } } module.exports = MaxHeap;