Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Yieni, you hit the main learning goals. Take a look on my feedback regarding time/space complexity and on heap_down.
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def heapsort(list) |
| # Time Complexity: 0(1)? | ||
| # Space Complexity: o(n)? | ||
| def add(key, value = key) |
There was a problem hiding this comment.
👍 The time complexity would be O(log n) because of how you heap_up going up through the levels of the tree.
Similarly because heap_up is recursive the time complexity is O(log n).
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def remove() |
| @@ -47,7 +59,7 @@ def to_s | |||
| # Time complexity: ? | |||
| # Space complexity: ? | |||
| def empty? | |||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def heap_up(index) |
| elsif left_child.nil? == true && right_child.nil? == true | ||
| return @store | ||
| elsif left_child.nil? == true && right_child.nil? == false |
There was a problem hiding this comment.
I don't think the right child can't be nil if the left child is.
similarly I don't think the left child can be nil and the right not nil.
| @store[right_child_index] = parent_node | ||
| index = right_child_index | ||
| return heap_down(index) | ||
| else left_child.nil? == false && right_child.nil? == true |
There was a problem hiding this comment.
Good catch checking if the right child is past the end of the array and the left isn't.
| # This helper method takes an index and | ||
| # moves it up the heap if it's smaller | ||
| # than it's parent node. | ||
| def heap_down(index) |
There was a problem hiding this comment.
This works, but you've overcomplicated some of the if statements.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?