Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Cedar-Bailey #3

wants to merge 4 commits into from

Conversation

beaniebaye
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? BSTs are ordered, heaps are more similar to queues
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? Because worst case you have traverse every layer when you heap up.
Were the heap_up & heap_down methods useful? Why? Yes, helper functions are nice and make code more readable

Copy link

@anselrognlie anselrognlie left a 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.

🟢

Comment on lines +14 to +15
// Time Complexity: O(log n)
// Space Complexity: O(log n) because of recursion

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.

Comment on lines +24 to +25
// Time Complexity: O(log n)
// Space Complexity: O(log n) because of recursion

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.

Comment on lines +49 to +50
// Time complexity: O(1)
// Space complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +58 to +59
// Time complexity: O(log n)
// Space complexity: O(1)

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);

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);

Comment on lines +64 to +68
while (parentIndex >= 0 && this.store[parentIndex].key > this.store[index1].key) {
this.swap(index1, parentIndex);
index1 = parentIndex;
parentIndex = Math.floor((index1 - 1)/2);
}

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;
    }

Comment on lines +75 to +76
let leftChildIndex = (2 * index) + 1;
let rightChildIndex = (2 * index) + 2;

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

Comment on lines +77 to +87
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;
}

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;
      }
    }

Comment on lines +3 to +4
// 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)

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).

Comment on lines +15 to 22
while (!minHeap.isEmpty()) {
currentMin = minHeap.remove();
list[i] = currentMin;
i++;
}

return list;
};

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants