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

Branches -- Paige #2

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

Conversation

minipaige02
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap is different in that the nodes on the right side do not have to be greater than the nodes on the left, it just must satisfy the heap property. It must also be an almost complete tree, meaning that every level must be full except for the last level, which must be filled from left to right.
Could you build a heap with linked nodes? You could use a linked list, but it is easier to implement using an array.
Why is adding a node to a heap an O(log n) operation? Because any time you add a new node you may have to traverse the tree to make sure it is in order, but you will only ever need to do that log n times.
Were the heap_up & heap_down methods useful? Why? Yes, because it helped to separate out the pieces of the algorithm that were being called over and over again, in my case, recursively.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

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

Nice work, you hit the basic learning goals here. I do think you missed one edge-case on heapDown, but otherwise very well done.

Comment on lines +4 to 7
// Time Complexity: O(n log n) - first iterate over list (O(n))
// and run .add() for each item (O(log n))
// Space Complexity: O(1) - uses original list param to store sorted list
function heapsort(list) {

Choose a reason for hiding this comment

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

👍 , except that you're building a heap, so the space complexity is O(n).

Comment on lines +14 to 16
// Time Complexity: O(log n)
// Space Complexity: O(log n)
add(key, value = key) {

Choose a reason for hiding this comment

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

The method works, and you have the space complexity right because you're doing recursion.

Comment on lines +54 to 56
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {

Choose a reason for hiding this comment

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

👍

const leftChild = 2 * index + 1;
const rightChild = 2 * index + 2;

if (leftChild >= this.store.length || rightChild >= this.store.length) return;

Choose a reason for hiding this comment

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

What if the left child is smaller than the length and the left child is smaller than it's parent, and at the same time the right child is equal to the length?

Copy link
Author

Choose a reason for hiding this comment

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

@CheezItMan can you give me an example? I'm having a hard time thinking of how a child index would be smaller than its parent index.

if (this.store[parent].key > this.store[index].key) {
this.swap(parent, index);
return this.heapUp(parent);
}
}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
heapDown(index) {

Choose a reason for hiding this comment

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

This method works in most cases, but I think you're missing an edge-case here.

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