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

Kate P - Time #4

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

Kate P - Time #4

wants to merge 2 commits into from

Conversation

Catherina87
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? 1. Heap ensures that every parent node is greater (max heap) or smaller (min heap) its child nodes, whereas in BST every node to the left is smaller and every node to the right is bigger than the root of the tree/subtree. 2. Heap is a binary tree which is complete, whereas to be a BST the tree need not be complete.
Could you build a heap with linked nodes? I think it would be possible, but it is much easier to do it with array.
Why is adding a node to a heap an O(log n) operation? Because we not only need to add the node to the end of the array, but we also need to heap this node up to preserve the heap structure. Heaping up the node takes O(log n).
Were the heap_up & heap_down methods useful? Why? Yes, both methods were useful when implementing add and remove methods.

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.

Really nice work Kate, you hit everything on this project. Well done.

Comment on lines +4 to 7
// Time Complexity: O(n log n)
// Space Complexity: O(n)

function heapsort(list) {

Choose a reason for hiding this comment

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

👍

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.

👍

Comment on lines +25 to 27
// Time Complexity: O(log n)
// Space Complexity: O(log n)
remove() {

Choose a reason for hiding this comment

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

👍

// Time complexity: ?
// Space complexity: ?
// 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.

👍

Comment on lines +62 to 64
// Time complexity: O(log n)
// Space complexity: O(log n)
heapUp(index) {

Choose a reason for hiding this comment

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

👍

this.store[parent] = this.store[index];
this.store[index] = temp;

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.

👍

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