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

Water - Christabel #15

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

Water - Christabel #15

wants to merge 4 commits into from

Conversation

cescarez
Copy link

@cescarez cescarez commented May 2, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps maintain an ascending or descending parent-child order. BSTs maintain a left to right order. That is, the root/parent in a heap will always be the min or max and the root/parent of a BST will be some value between the left and right children.
Could you build a heap with linked nodes? Sure, with some modification in that instead of a single instance variable with the memory address of the next node, there are two instance variables for the left and right children.
Why is adding a node to a heap an O(log n) operation? It's a log base 2 operation for the number of nodes already in the heap because when a node gets added to the heap, only the value of every parent node is checked against the value of the new node and thus we are only traveling/checking one side of the tree.
Were the heap_up & heap_down methods useful? Why? I didn't see them until the end so not really. It made the code look cleaner. And I guess they're helpful for abstraction in case I want to change implementation to a recursive call or something....

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.

It looks pretty good Christabel, I like the iterative methods, very efficient. However I think there are some hidden bugs in heap_down. Take a look at my comments and let me know if you have questions.

Consider:

    heap.add(3, 3)
    heap.add(6, 6)
    heap.add(1, 1)

Then if you try to remove things you'll get NoMethodError: undefined method key' for nil:NilClass`

Comment on lines +4 to 6
# Time Complexity: O(n log n) where n is the number of elements in the given array
# Space Complexity: O(n)
def heap_sort(list)

Choose a reason for hiding this comment

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

This turns the array into a heap, it doesn't "sort" the array using a heap. So it's not quite the exercise.

Comment on lines +17 to 19
# Time Complexity: O(log n) where n is the number of nodes in the heap
# Space Complexity: O(2) == O(1)
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.

👍 , nice iterative heap_up

Comment on lines +27 to 29
# Time Complexity: O(log n) where n is the number of nodes in the heap
# Space Complexity: O(2) == O(1)
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 +52 to 54
# Time complexity: O(1) ? I think the length of arrays is somehow cached or something in Ruby? If not, then O(n) where n is the number of elements in the array
# Space complexity: O(1)
def empty?

Choose a reason for hiding this comment

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

👍

Comment on lines +63 to +65
# Time complexity: O(log n) where n is the number of nodes in the heap
# Space complexity: O(1)
def heap_up(current)

Choose a reason for hiding this comment

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

👍 , very cleanly written

right_child = current * 2 + 2
child = @store[left_child].key < @store[right_child].key ? left_child : right_child

while @store[current].key > @store[child].key do

Choose a reason for hiding this comment

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

What happens if @store[left_child] is nil? I think this could cause an error.

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