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

Kim <3 <3 <3 #5

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

Conversation

Kimberly-Fasbender
Copy link

@Kimberly-Fasbender Kimberly-Fasbender commented Sep 20, 2019

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? It isn't! J/K, gotcha! In a BST the left child of a parent node always has a value less than the parent, and the right child always has a value greater than the parent. In a Heap both children of a parent are either less than the parent (maxHeap) or greater than the parent (minHeap). Also, in a BST a parent could have a right child node, and no left child node (not the case in a Heap where each level is filled in left to right).
Could you build a heap with linked nodes? Yes, if you hated yourself. It would be much more difficult without being able to access the indices like you can with an array.
Why is adding a node to a heap an O(log n) operation? Because when you add a node to the Heap you compare it to its parent's value, swap if necessary, and then repeat until you reach the root of the tree. This means the depth of the tree is the max number of swaps you will ever need to preform. The depth is also 1/2 of the nodes in any given subtree of the heap, hence O(log n) time complexity.
Were the heap_up & heap_down methods useful? Why? Yes, they made the add and remove methods much more readable, and are also essential parts of maintaining the heap.

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 all the learning goals here. Well done.

Comment on lines +9 to +15
minHeap.add(list.pop)
end

until minHeap.empty?
list << minHeap.remove
end

Choose a reason for hiding this comment

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

Very clear and elegant.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n) - where n is the number of heap nodes
# Space Complexity: O(1) - constant

Choose a reason for hiding this comment

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

Since heap_up is recursive... You do have some space complexity here. The system stack.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n) - where n is the number of heap nodes
# Space Complexity: O(1) - constant

Choose a reason for hiding this comment

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

Ditto

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(log n) - where n is the number of heap nodes
# Space complexity: O(mn) - where m is the space the recursive call is taking up on the stack, and n is the max depth of the tree

Choose a reason for hiding this comment

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

Since m is likely to be constant in size, this is O(n)

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