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 - Mackenzie #24

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

Water - Mackenzie #24

wants to merge 5 commits into from

Conversation

mvlofthus
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? heaps have the smallest or largest value at the root compared to the children versus the root value being in the middle.
Could you build a heap with linked nodes? i'm sure there is something possible with this, but these methods work with arrays and rely on being able to switch values at specific indexes. With a linked list it would be less space efficient.
Why is adding a node to a heap an O(log n) operation? adding a node is o(1) and sorting it back is a factor of it's height, I think some level of log base 2.
Were the heap_up & heap_down methods useful? Why? yes, they can be used to sort any array as a 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.

Overall not bad Mackenzie. I had some feedback on your heap_down function. Take a look at my comments and let me know if I'm not clear enough in my expanations. I'm happy to try to clarify better.

Comment on lines +4 to 6
# Time Complexity: O(m+n)
# Space Complexity: O(n) - will have a call stack equal to number of nodes, though we are make a constant number of data structures... maybe O(1)
def heap_sort(list)

Choose a reason for hiding this comment

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

👍 The time complexity will be O(n log n) because you are adding n elements to the heap and each addition will take Log n time.

Then you remove n elements (each taking log n time).

Comment on lines +17 to 19
# Time Complexity: O(m+1), where m is height - O(1) for adding, O(m) for heap up
# Space Complexity: O(1) - no new data structures made, but O(m) for heap up call stack (is it the number of calls in the calls stack or number of structures made?)
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.

👍 , However the time complexity is O(log n) because heap_up will go level-by-level in the tree. Since the tree is always balanced, m = log n.

Comment on lines +30 to 32
# Time Complexity: O(n+1)
# Space Complexity: O(1)
def remove()

Choose a reason for hiding this comment

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

👍 However the time complexity will be O(log n) time (because of the recursive swaps) and space complexity of O(log n) as well due to the call stack.

def heap_up(index)
return if index == 0

if @store[index].key < @store[((index - (index % 3)) / 2)].key

Choose a reason for hiding this comment

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

It might be easier just to do

parent_index = (index - 1) / 2

And use that variable

Comment on lines +72 to 74
# Time complexity: O(m) where m is height
# Space complexity: O(1), no persistent data structure is made beyond 1 temporary reassigned variable
def heap_up(index)

Choose a reason for hiding this comment

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

👍 , but I would give O(log n) for the time complexity because the tree is always balanced and the height is always log n. The space complexity would be O(log n) due to the call stack (recursion).

Comment on lines +95 to +104
if !@store[index * 2 + 1].nil? && @store[index].key > @store[index * 2 + 1].key
swap(index, (index * 2 + 1))
end

if !@store[index * 2 + 2].nil? && @store[index].key > @store[index * 2 + 2].key
swap(index, (index * 2 + 2))
end

heap_down(index * 2 + 1)
heap_down(index * 2 + 2)

Choose a reason for hiding this comment

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

You're doing a bunch of extra swaps here. Instead find the smallest child and then if it's smaller than the parent, swap those and continue. You should never need to swap both the right and left children, as well as heap down both children.

end

# If you want a swap method... you're welcome
#THANK YOUUUUUU

Choose a reason for hiding this comment

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

😻

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