From bd5b5e48150c417e201e87837eef404362987772 Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 31 Oct 2021 18:55:46 -0700 Subject: [PATCH 1/5] implemented add method and getParentNode helper method --- lib/minheap.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/minheap.js b/lib/minheap.js index 4efceaf..c77f12c 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -14,7 +14,11 @@ class MinHeap { // Time Complexity: ? // Space Complexity: ? add(key, value = key) { - throw new Error("Method not implemented yet..."); + + node = HeapNode(key, value) + this.store.push(node) + this.heapUp(len(self.store) - 1 ) + } // This method removes and returns an element from the heap @@ -50,9 +54,13 @@ class MinHeap { // Time complexity: ? // Space complexity: ? heapUp(index) { - throw new Error("Method not implemented yet..."); + if (this.store[index]){ + + } + } + // This helper method takes an index and // moves it up the heap if it's smaller // than it's parent node. @@ -65,6 +73,9 @@ class MinHeap { const s = this.store; [s[index1], s[index2]] = [s[index2], s[index1]]; } + + getParentNode(index){ + return Math.floor((index-1)/2) } module.exports = { From 4f4a84d1dffe4d112e045235eafb60d8293735b3 Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 31 Oct 2021 20:21:43 -0700 Subject: [PATCH 2/5] passes 6 tests --- lib/minheap.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/minheap.js b/lib/minheap.js index c77f12c..c963452 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -15,10 +15,11 @@ class MinHeap { // Space Complexity: ? add(key, value = key) { - node = HeapNode(key, value) - this.store.push(node) - this.heapUp(len(self.store) - 1 ) - + const node = new HeapNode(key, value); + this.store.push(node); + if (this.store.length > 1 ){ + this.heapUp(this.store.length- 1 ); + } } // This method removes and returns an element from the heap @@ -26,7 +27,13 @@ class MinHeap { // Time Complexity: ? // Space Complexity: ? remove() { - throw new Error("Method not implemented yet..."); + pass + if (this.store.length == 0){ + return None + } + + + } @@ -54,10 +61,17 @@ class MinHeap { // Time complexity: ? // Space complexity: ? heapUp(index) { - if (this.store[index]){ - - } + const parentIndex = Math.floor((index - 1)/2); + const parentNode = this.store[Math.floor((index - 1)/2)]; + const childNode = this.store[index]; + + if ( childNode.key < parentNode.key ){ + this.swap(index, parentIndex); + if (parentIndex > 0){ + this.heapUp(parentIndex) + } + } } @@ -65,7 +79,7 @@ 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..."); + } // If you want a swap method... you're welcome @@ -74,10 +88,10 @@ class MinHeap { [s[index1], s[index2]] = [s[index2], s[index1]]; } - getParentNode(index){ + getParentIndex(index){ return Math.floor((index-1)/2) } - +} module.exports = { MinHeap }; From ea87984ba0d1b8a944a2c9708283745ddb249ec0 Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Sun, 31 Oct 2021 22:23:50 -0700 Subject: [PATCH 3/5] pass all minheap tests --- lib/minheap.js | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/minheap.js b/lib/minheap.js index c963452..bbc317c 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -17,8 +17,8 @@ class MinHeap { const node = new HeapNode(key, value); this.store.push(node); - if (this.store.length > 1 ){ - this.heapUp(this.store.length- 1 ); + if (this.store.length > 1) { + this.heapUp(this.store.length - 1); } } @@ -27,13 +27,16 @@ class MinHeap { // Time Complexity: ? // Space Complexity: ? remove() { - pass - if (this.store.length == 0){ - return None + if (this.store.length == 0) { + return; } + this.swap(0, (this.store.length - 1)) + const lastNode = this.store.pop(); + this.heapDown(0); - + console.log(this.store, lastNode.value , this.store.length) + return lastNode.value } @@ -62,14 +65,14 @@ class MinHeap { // Space complexity: ? heapUp(index) { - const parentIndex = Math.floor((index - 1)/2); - const parentNode = this.store[Math.floor((index - 1)/2)]; + const parentIndex = Math.floor((index - 1) / 2); + const parentNode = this.store[Math.floor((index - 1) / 2)]; const childNode = this.store[index]; - if ( childNode.key < parentNode.key ){ + if (childNode.key < parentNode.key) { this.swap(index, parentIndex); - if (parentIndex > 0){ - this.heapUp(parentIndex) + if (parentIndex > 0) { + this.heapUp(parentIndex) } } } @@ -79,7 +82,22 @@ class MinHeap { // moves it up the heap if it's smaller // than it's parent node. heapDown(index) { + const leftNode = 2 * index + 1 + const rightNode = 2 * index + 2 + let smallest = index; + + if(leftNode < this.store.length && this.store[leftNode].key < this.store[smallest].key) { + smallest = leftNode; + } + + if (rightNode < this.store.length && this.store[rightNode].key < this.store[smallest].key) { + smallest = rightNode; + } + if (index !== smallest){ + this.swap(index, smallest); + this.heapDown(smallest); + } } // If you want a swap method... you're welcome @@ -88,9 +106,9 @@ class MinHeap { [s[index1], s[index2]] = [s[index2], s[index1]]; } - getParentIndex(index){ - return Math.floor((index-1)/2) -} + getParentIndex(index) { + return Math.floor((index - 1) / 2) + } } module.exports = { MinHeap From 50002b9279c87d93da8a4ae4bd0c2e8a563885ce Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Mon, 1 Nov 2021 22:54:09 -0700 Subject: [PATCH 4/5] pass all test --- lib/heapsort.js | 15 ++++++++++++++- lib/minheap.js | 3 +-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/heapsort.js b/lib/heapsort.js index 69d5af2..5982a8c 100644 --- a/lib/heapsort.js +++ b/lib/heapsort.js @@ -1,8 +1,21 @@ +const { MinHeap } = require("./minheap"); + + // This method uses a heap to sort an array. // Time Complexity: ? // Space Complexity: ? function heapsort(list) { - throw new Error('Method not implemented yet...'); + const heap = new MinHeap(); + + while (list.length > 0) { + heap.add(list.pop()); + } + + while (!heap.isEmpty()) { + list.push(heap.remove()); + } + + return list; }; module.exports = heapsort; diff --git a/lib/minheap.js b/lib/minheap.js index bbc317c..4e078a0 100644 --- a/lib/minheap.js +++ b/lib/minheap.js @@ -35,7 +35,6 @@ class MinHeap { const lastNode = this.store.pop(); this.heapDown(0); - console.log(this.store, lastNode.value , this.store.length) return lastNode.value } @@ -55,7 +54,7 @@ class MinHeap { // Time complexity: ? // Space complexity: ? isEmpty() { - throw new Error("Method not implemented yet..."); + return this.store.length === 0; } // This helper method takes an index and From 78abf6be71a1367d3f06610aefc82635b791fd41 Mon Sep 17 00:00:00 2001 From: Daniela Sanchez Date: Tue, 2 Nov 2021 22:35:58 -0700 Subject: [PATCH 5/5] clean up code and add Time and Space Complexity comments --- lib/heapsort.js | 4 ++-- lib/minheap.js | 21 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/heapsort.js b/lib/heapsort.js index 5982a8c..9274e0f 100644 --- a/lib/heapsort.js +++ b/lib/heapsort.js @@ -2,8 +2,8 @@ const { MinHeap } = require("./minheap"); // This method uses a heap to sort an array. -// Time Complexity: ? -// Space Complexity: ? +// Time Complexity: O(n log n ) +// Space Complexity: O(n) function heapsort(list) { const heap = new MinHeap(); diff --git a/lib/minheap.js b/lib/minheap.js index 4e078a0..1f98b52 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(log n) + // Space Complexity: O(log n) add(key, value = key) { const node = new HeapNode(key, value); @@ -24,8 +24,8 @@ class MinHeap { // This method removes and returns an element from the heap // maintaining the heap structure - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(log n) + // Space Complexity: O(log n) remove() { if (this.store.length == 0) { return; @@ -51,8 +51,8 @@ class MinHeap { } // This method returns true if the heap is empty - // Time complexity: ? - // Space complexity: ? + // Time complexity: O(1) + // Space complexity: O(1) isEmpty() { return this.store.length === 0; } @@ -60,12 +60,12 @@ class MinHeap { // 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(log n) + // Space complexity: O(log n) heapUp(index) { const parentIndex = Math.floor((index - 1) / 2); - const parentNode = this.store[Math.floor((index - 1) / 2)]; + const parentNode = this.store[parentIndex]; const childNode = this.store[index]; if (childNode.key < parentNode.key) { @@ -105,9 +105,6 @@ class MinHeap { [s[index1], s[index2]] = [s[index2], s[index1]]; } - getParentIndex(index) { - return Math.floor((index - 1) / 2) - } } module.exports = { MinHeap