-
Notifications
You must be signed in to change notification settings - Fork 7
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
Cedar-Bailey #3
base: master
Are you sure you want to change the base?
Cedar-Bailey #3
Conversation
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.
✨💫 Nice job, Bailey. I left some comments on your implementation below.
A few things about the comprehension questions. We can say that heaps are weakly, or partially ordered due the relationship between a node and it's two children (not left-right less-more, just strictly less or more than the children depending on whether this is a MinHeap or MaxHeap). And while we could use a nodes to build a heap, we benefit from the convenience of index calculations and data being located close together when using an array.
🟢
// Time Complexity: O(log n) | ||
// Space Complexity: O(log n) because of recursion |
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.
✨ You're exactly right that the time complexity is O(log n), which is due to the maximum number of swaps we might need to perform to get the node to the proper location, and the height of a heap is log n.
👀 You would be correct that the space complexity is also O(log n) when using a recursive approach due to stack growth. However your heapUp
uses an iterative approach, avoiding the height-dependent stack growth. Instead, the space complexity is simply O(1), constant.
// Time Complexity: O(log n) | ||
// Space Complexity: O(log n) because of recursion |
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.
✨ Nice. You're right that the log space complexity for remove
is due to the recursive heapDown
implementation. We could achieve O(1) space complexity if we used an iterative approach.
// Time complexity: O(1) | ||
// Space complexity: O(1) |
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.
✨
// Time complexity: O(log n) | ||
// Space complexity: O(1) |
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.
✨ Exactly. This is where the main cost of add
comes from, which is why add
actually has a space complexity of O(1), constant.
add(key, value = key) { | ||
throw new Error("Method not implemented yet..."); | ||
let newNode = new HeapNode(key, value); |
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.
Prefer const
unless we need to reassign the variable.
const newNode = new HeapNode(key, value);
while (parentIndex >= 0 && this.store[parentIndex].key > this.store[index1].key) { | ||
this.swap(index1, parentIndex); | ||
index1 = parentIndex; | ||
parentIndex = Math.floor((index1 - 1)/2); | ||
} |
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.
Here's another approach that would let us perform the parentIndex
calculation only once (we can get rid of the once before the start of the loop). It also allows us to simplify the while
condition a bit.
while (index > 0) {
const parentIndex = Math.floor((index - 1)/2);
if (this.store[parentIndex].key < this.store[index].key) { break; }
this.swap(index, parentIndex);
index = parentIndex;
}
let leftChildIndex = (2 * index) + 1; | ||
let rightChildIndex = (2 * index) + 2; |
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.
👀 These could both be const
let nextRootIndex; | ||
|
||
if (leftChildIndex < this.store.length && rightChildIndex < this.store.length) { | ||
if (this.store[leftChildIndex].key > this.store[rightChildIndex].key) { | ||
nextRootIndex = rightChildIndex; | ||
} else { | ||
nextRootIndex = leftChildIndex; | ||
} | ||
} else { | ||
nextRootIndex = leftChildIndex; | ||
} |
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.
If we initialize next to be the more common outcome of the conditions, we can reduce the complexity of the conditional clauses. Further, we can observe that the right child being in bounds implies the left must be as well and omit that from the condition.
let nextRootIndex = leftChildIndex;
if (rightChildIndex < this.store.length) {
if (this.store[leftChildIndex].key > this.store[rightChildIndex].key) {
nextRootIndex = rightChildIndex;
}
}
// Time Complexity: O(n log n) | ||
// Space Complexity: O(n + log n) because of the length of the array and the heap storage-> O(n) |
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.
✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the MinHeap
does grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.
Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).
while (!minHeap.isEmpty()) { | ||
currentMin = minHeap.remove(); | ||
list[i] = currentMin; | ||
i++; | ||
} | ||
|
||
return list; | ||
}; |
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.
Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input.
const result = [];
while (!minHeap.isEmpty()) {
result.push(minHeap.remove())
}
return result;
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?