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

Angele Z. #29

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

Angele Z. #29

wants to merge 9 commits into from

Conversation

geli-gel
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 binary search tree's root needs to be somewhere in the middle of the range of values it contains so that it can remain balanced, with one child being less than its value and the other being greater than its value. With a heap, the root is either max or min, and the children just need to be lesser values than the root.
Could you build a heap with linked nodes? Yes, a heap with linked nodes could be made, instead of determining the parent or child nodes with a formula, we could determine where they are, what their values, are, and if they exist by checking the links (I think it would have to be doubly linked)
Why is adding a node to a heap an O(log n) operation? Adding a node, in the worst case scenario, would have to swap with subsequent parent nodes a number of times equal to the height of the heap, which is a logarithmic relationship to the number of nodes in the heap.
Were the heap_up & heap_down methods useful? Why? Yes, because when implementing a heap using an array, calculations need to be done to determine the parent and child indexes, and different things need to happen depending on the values of those nodes.

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.

Well you didn't get heapsort implemented, but otherwise this works. Take a look at my comments and let me know what questions you have.

lib/min_heap.rb Outdated
Comment on lines 17 to 19
# Time Complexity: O(h) - height of the heap
# Space Complexity: O(h)
def add(key, value = key)

Choose a reason for hiding this comment

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

👍 note h = log n

lib/min_heap.rb Outdated
Comment on lines 26 to 28
# Time Complexity: O(h)
# Space Complexity: O(h)
def remove()

Choose a reason for hiding this comment

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

👍

lib/min_heap.rb Outdated
Comment on lines 64 to 66
# Time complexity: o(1)
# Space complexity: o(1)
def heap_up(index)

Choose a reason for hiding this comment

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

O(1)?? How? This is a recursive method so it's going to have time/space complexity.

lib/min_heap.rb Outdated
end
swap(index, index_with_smaller_key)
end
heap_down(index_with_smaller_key)

Choose a reason for hiding this comment

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

This might lead you to heaping down, even when you didn't do a swap... It won't cause bugs, but might lead to extra recursive calls.

lib/min_heap.rb Show resolved Hide resolved
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 well done, this hits all the learning goals. The code is well-written and executed. Awesome!

Comment on lines +4 to +6
# Time Complexity: o((n * log n) + (n * log n)) => o(n log n) | it depends on the length of the list, but adding and removing from the list takes log(n), and the removal of each takes log(n) as well, and has to happen n times (the length of the list)
# Space Complexity: also o(n log n)
def heapsort(list)

Choose a reason for hiding this comment

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

The method here works, great! However the space complexity is O(n) because you're making a heap of n elements, not n log n elements.

Comment on lines +17 to 19
# Time Complexity: O(log n) - height of the heap is a logarithmic relationship to the number of nodes
# Space Complexity: O(log n)
def 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 +26 to 28
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def remove()

Choose a reason for hiding this comment

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

👍

Comment on lines +53 to 55
# Time complexity: o(1) - the access time complexity of an array
# Space complexity: o(1) - the access space complexity of an array
def empty?

Choose a reason for hiding this comment

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

👍

def heap_up(index)

return if index == 0
parent_index = (index % 2 != 0) ? (index / 2) : (index / 2 - 1)

Choose a reason for hiding this comment

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

Could this just be:

Suggested change
parent_index = (index % 2 != 0) ? (index / 2) : (index / 2 - 1)
parent_index =(index - 1) / 2

Comment on lines +64 to 66
# Time complexity: o(h) - the time it takes to run depends on the height of the heap; in the worst case scenario it will touch each level of the heap
# Space complexity: o(h) - the space complexity of heap_up depends on the number of recursive calls that will be held in memory, up to the height of the heap.
def heap_up(index)

Choose a reason for hiding this comment

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

👍 And h = log n, where n is the number of nodes.

Comment on lines +76 to 78
# moves it down the heap if it's bigger
# than its child node.
def heap_down(index)

Choose a reason for hiding this comment

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

👍 Nicely done

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