From fb5972e21bd7fb3ef1d2f08f138064a342620c8f Mon Sep 17 00:00:00 2001 From: Mira Gross Date: Thu, 12 Mar 2020 16:44:59 -0700 Subject: [PATCH 1/3] create and add --- lib/minheap.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/minheap.js b/lib/minheap.js index 4efceaf..61656b9 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -14,7 +14,9 @@ class MinHeap { // Time Complexity: ? // Space Complexity: ? add(key, value = key) { - throw new Error("Method not implemented yet..."); + const node = new HeapNode(key, value); + const i = this.store.push(node) - 1; + this.heapUp(i); } // This method removes and returns an element from the heap @@ -50,7 +52,13 @@ class MinHeap { // Time complexity: ? // Space complexity: ? heapUp(index) { - throw new Error("Method not implemented yet..."); + if (index <= 0) return; + + const parentIndex = this.parentIndex(index); + if (this.store[index].key < this.store[parentIndex].key) { + this.swap(index, parentIndex); + this.heapUp(parentIndex); + } } // This helper method takes an index and @@ -60,6 +68,11 @@ class MinHeap { throw new Error("Method not implemented yet..."); } + parentIndex(index) { + const i = Math.floor((index - 1) / 2); + return i < 0 ? 0 : i; + } + // If you want a swap method... you're welcome swap(index1, index2) { const s = this.store; From f27d064b4eee0012a518eac9bdfbee68bd810e11 Mon Sep 17 00:00:00 2001 From: Mira Gross Date: Fri, 13 Mar 2020 16:50:23 -0700 Subject: [PATCH 2/3] remove and empty --- lib/minheap.js | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/minheap.js b/lib/minheap.js index 61656b9..b4344b9 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -11,8 +11,8 @@ class MinHeap { } // This method adds a HeapNode instance to the heap - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(logn) + // Space Complexity: O(1) add(key, value = key) { const node = new HeapNode(key, value); const i = this.store.push(node) - 1; @@ -21,10 +21,14 @@ class MinHeap { // This method removes and returns an element from the heap // maintaining the heap structure - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(logn) + // Space Complexity: O(1) remove() { - throw new Error("Method not implemented yet..."); + if (this.isEmpty()) return; + this.swap(0, this.store.length - 1); + const { value } = this.store.pop(); + if (!this.isEmpty()) this.heapDown(0); + return value; } @@ -40,17 +44,17 @@ class MinHeap { } // This method returns true if the heap is empty - // Time complexity: ? - // Space complexity: ? + // Time complexity: O(1) + // Space complexity: O(1) isEmpty() { - throw new Error("Method not implemented yet..."); + return this.store.length === 0; } // This helper method takes an index and // moves it up the heap, if it is less than it's parent node. // It could be **very** helpful for the add method. - // Time complexity: ? - // Space complexity: ? + // Time complexity: O(logn) + // Space complexity: O(1) heapUp(index) { if (index <= 0) return; @@ -65,7 +69,26 @@ class MinHeap { // moves it up the heap if it's smaller // than it's parent node. heapDown(index) { - throw new Error("Method not implemented yet..."); + const [leftIndex, rightIndex] = this.children(index); + if (!leftIndex && !rightIndex) return; + + let min; + rightIndex ? min = rightIndex : min = leftIndex; + if (this.store[leftIndex].key < this.store[rightIndex].key) { + min = leftIndex; + } + + if (this.store[index].key > this.store[min].key) { + this.swap(index, min); + this.heapDown(min); + } + } + + children(index) { + let i = index * 2 + 1, j; + if (i >= this.store.length) i = null; + if (i) j = i + 1; + return [i, j]; } parentIndex(index) { From 1a1b0ac54da963510d62ab438718825f5e23536f Mon Sep 17 00:00:00 2001 From: Mira Gross Date: Thu, 19 Mar 2020 14:35:15 -0700 Subject: [PATCH 3/3] sort --- lib/heapsort.js | 18 +++++++++++++++--- lib/minheap.js | 9 ++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/heapsort.js b/lib/heapsort.js index 69d5af2..11230c8 100644 --- a/lib/heapsort.js +++ b/lib/heapsort.js @@ -1,8 +1,20 @@ +const { MinHeap } = require('./minheap'); + // This method uses a heap to sort an array. -// Time Complexity: ? -// Space Complexity: ? +// Time Complexity: O(logn) +// Space Complexity: O(n) function heapsort(list) { - throw new Error('Method not implemented yet...'); + const heap = new MinHeap(); + const out = []; + let i = 0; + + list.forEach(x => heap.add(x)); + + while (i < list.length) { + out[i] = heap.remove(); + i++; + } + return out; }; module.exports = heapsort; diff --git a/lib/minheap.js b/lib/minheap.js index b4344b9..3a9c275 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -17,6 +17,7 @@ class MinHeap { const node = new HeapNode(key, value); const i = this.store.push(node) - 1; this.heapUp(i); + // console.log(this.store); } // This method removes and returns an element from the heap @@ -70,12 +71,14 @@ class MinHeap { // than it's parent node. heapDown(index) { const [leftIndex, rightIndex] = this.children(index); + if (!leftIndex && !rightIndex) return; let min; rightIndex ? min = rightIndex : min = leftIndex; - if (this.store[leftIndex].key < this.store[rightIndex].key) { - min = leftIndex; + if ((leftIndex && rightIndex) && + this.store[leftIndex].key < this.store[rightIndex].key) { + min = leftIndex; } if (this.store[index].key > this.store[min].key) { @@ -87,7 +90,7 @@ class MinHeap { children(index) { let i = index * 2 + 1, j; if (i >= this.store.length) i = null; - if (i) j = i + 1; + if (i && i < this.store.length - 1) j = i + 1; return [i, j]; }