-
Notifications
You must be signed in to change notification settings - Fork 5
Aimee 👾 - Fire 🔥 #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
619e859
6511233
d9822a1
85f6ae8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,20 @@ | ||
| const { MinHeap } = require('./minheap'); | ||
|
|
||
| // This method uses a heap to sort an array. | ||
| // Time Complexity: ? | ||
| // Space Complexity: ? | ||
| // Time Complexity: O(nlog(n))- both for loops are O(n), heap.add and heap.remove are O(log(n)) | ||
| // Space Complexity: O(n) - the store array of minHeap has a space complexity of O(n) | ||
| function heapsort(list) { | ||
| throw new Error('Method not implemented yet...'); | ||
| let heap = new MinHeap(); | ||
| const len = list.length; | ||
| for (let i = 0; i < len; i++) { | ||
| heap.add(list[i]); | ||
| } | ||
|
|
||
| for (let i = 0; i < len; i++) { | ||
| list[i] = heap.remove(); | ||
| } | ||
|
|
||
| return list; | ||
| }; | ||
|
|
||
| module.exports = heapsort; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,18 +11,30 @@ class MinHeap { | |
| } | ||
|
|
||
| // This method adds a HeapNode instance to the heap | ||
| // Time Complexity: ? | ||
| // Space Complexity: ? | ||
| // Time Complexity: O(log(n)) - Recursive calls (uses heapUp) | ||
| // Space Complexity: O(log(n)) - Recursive calls, log(n) calls in memory (because of heapUp) | ||
| add(key, value = key) { | ||
|
Comment on lines
+14
to
16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| throw new Error("Method not implemented yet..."); | ||
| const node = new HeapNode(key, value); | ||
| const len = this.store.length; | ||
| this.store[len] = node; | ||
|
|
||
| return this.heapUp(len); | ||
| } | ||
|
|
||
| // This method removes and returns an element from the heap | ||
| // maintaining the heap structure | ||
| // Time Complexity: ? | ||
| // Space Complexity: ? | ||
| // Time Complexity: O(log(n)), recursive (uses heapDown, similar to heapUp) | ||
| // Space Complexity: O(log(n)), log(n) recursion calls (uses heapDown) | ||
| remove() { | ||
|
Comment on lines
+26
to
28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| throw new Error("Method not implemented yet..."); | ||
| const len = this.store.length; | ||
| if (this.isEmpty()) { | ||
| return undefined; | ||
| } | ||
| this.swap(0, len - 1); | ||
| const removed = this.store.pop(); | ||
| this.heapDown(0); | ||
|
|
||
| return removed.value; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -38,26 +50,77 @@ class MinHeap { | |
| } | ||
|
|
||
| // This method returns true if the heap is empty | ||
| // Time complexity: ? | ||
| // Space complexity: ? | ||
| // Time complexity: O(1) - the length property is a quick look up | ||
| // Space complexity: O(1) - saving the length of the array will be constant memory | ||
| isEmpty() { | ||
|
Comment on lines
+53
to
55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| throw new Error("Method not implemented yet..."); | ||
| const len = this.store.length; | ||
| if (len === 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // 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)), index is divided by two each time this method is called (n-2)/(2^t) = c (where t is the number of times the method is called and c is a constant (the parent index)). To solve for the number of times this is called, we can simplify this to n = 2^t. Solving for t: t = log2(n). | ||
| // Space complexity: O(log(n)), the space complexity here depends on the number of recursion steps, which will be log(n) steps. | ||
| heapUp(index) { | ||
| throw new Error("Method not implemented yet..."); | ||
|
|
||
| if (index === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const childNode = this.store[index]; | ||
| let parentIndex; | ||
| if (index % 2 === 0) { | ||
| parentIndex = (index - 2) / 2; | ||
| } else { | ||
| parentIndex = (index - 1) / 2; | ||
| } | ||
|
Comment on lines
+76
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use floor to simplify this a bit? |
||
|
|
||
| const parentNode = this.store[parentIndex]; | ||
|
|
||
| if (childNode.key >= parentNode.key) { | ||
| return; | ||
| } else { | ||
| this.swap(index, parentIndex); | ||
| this.heapUp(parentIndex); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // This helper method takes an index and | ||
| // moves it up the heap if it's smaller | ||
| // than it's parent node. | ||
| heapDown(index) { | ||
|
Comment on lines
93
to
96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| throw new Error("Method not implemented yet..."); | ||
| const leftIndex = (2 * index) + 1; | ||
| const len = this.store.length; | ||
| const rightIndex = (2 * index) + 2; | ||
| const leftNode = this.store[leftIndex]; | ||
| const rightNode = this.store[rightIndex]; | ||
| const parentNode = this.store[index]; | ||
|
|
||
| if (leftIndex >= len) { | ||
| return; | ||
| } else if (leftIndex === len - 1) { | ||
| if (parentNode.key <= leftNode.key) { | ||
| return; | ||
| } else { | ||
| this.swap(index, leftIndex); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (parentNode.key <= leftNode.key && parentNode.key <= rightNode.key) { | ||
| return; | ||
| } else if (leftNode.key < rightNode.key) { | ||
| this.swap(index, leftIndex); | ||
| this.heapDown(leftIndex); | ||
| } else { | ||
| this.swap(index, rightIndex); | ||
| this.heapDown(rightIndex); | ||
| } | ||
| } | ||
|
|
||
| // If you want a swap method... you're welcome | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍