-
Notifications
You must be signed in to change notification settings - Fork 40
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
Julia Kingrey #26
base: master
Are you sure you want to change the base?
Julia Kingrey #26
Conversation
There was a problem hiding this 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 most of the learning goals here. Some issues with heap_down, which is a tough method. Otherwise things look pretty good. Take a look at my comments and let me know any questions you have.
# Time Complexity: n log n -- n to put elements in heap, log n to sort, | ||
# and n log n put them back in the array | ||
# Space Complexity: O(n) | ||
def heapsort(list) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Well done.
# Time Complexity: usually O(1) because unlike adding to the front, appending / | ||
# adding to the end doesn't require reordering the whole array | ||
# Space Complexity: O(1) | ||
def add(key, value = key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The time complexity is O(log n) because the heap_up method is O(log n). Because it's recursive, the space complexity is also O(log n)
Otherwise this works.
# Time Complexity: O(1) because we are doing a swap and deleting at the end | ||
# (would be O of n if we were deleting from the front) | ||
# Space Complexity: O(1) because we're not creating any new spaces in memory | ||
|
||
def remove |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works but has similar time/space complexity issues to add.
def empty? | ||
raise NotImplementedError, "Method not implemented yet..." | ||
@store.length == 0 ? true : false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@store.length == 0 ? true : false | |
return @store.length == 0 |
# Time complexity: O(1) -- I *think* this code will stop running once | ||
# count gets higher than 0 but I'm not certain | ||
# Space complexity: O(1) | ||
def empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time complexity: O(log n) because we're swapping max once per tree level | ||
# Space complexity: O(log n) | ||
def heap_up(index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# stop if we're at the last leaf in the tree | ||
return if @store[left_child_index].nil? && @store[right_child_index].nil? | ||
# stop if both children are greater than or equal to parent | ||
return if @store[left_child_index].key >= @store[index].key && @store |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the right index child is not nil and it's less than @store[index]
??? Looks like you missed an edge-case.
# if parent is greater than left child, swap and look at next level -- | ||
# need @store[index] conditional to stop code running after we've reached | ||
# the bottom of the heap | ||
if @store[index] && @store[index].key > @store[left_child_index].key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to figure out which is smaller the left child or right child. Otherwise you'll do some unnecessary heap_downs.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?add
method just swapped the first and last elements and removed the last, while theheap_up
helper took care of the heap reordering. Separation of concerns is good design!