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

Tatiana - sorry it's so late! #26

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

Conversation

tatsqui
Copy link

@tatsqui tatsqui commented Oct 17, 2019

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap's parent-child node relationship is slightly different than a bst. For example in a min-heap the parent node is always smaller. It is a left-right balancing tree. Whereas with binary search you are always guaranteed that the left values are always smaller than the right node values... which isn't always the case for heaps.
Could you build a heap with linked nodes? Yes it is possible, I've never done it and I have been told by a great instructor named Chris that it's quite difficult.
Why is adding a node to a heap an O(log n) operation? Because of the tree traversal required.
Were the heap_up & heap_down methods useful? Why? Yes, they are what enforce the parent-child relationship and make it possible to easily implement the data structure using an array.

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.

Not bad, take a look at my comments and let me know what questions you have. In particular when you heap up and down you need to compare the values involved to make sure if you need to swap or not.

@@ -1,8 +1,24 @@

require 'pry'

Choose a reason for hiding this comment

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

You can take out pry when submitting things.


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation

Choose a reason for hiding this comment

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

It's actually O(n log n) which is because you're adding n elements to the heap which each take log n time. Then you remove n elements which takes log n time each.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) followed by O(log n) beacause going through entire array for first operation
# Space Complexity: I am attempting to do it in O(1)

Choose a reason for hiding this comment

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

Since you're creating a heap and building it up. This is O(n) space complexity.

Comment on lines +91 to +92
elsif @store[child_index_right].nil?
swap(index, child_index_left)

Choose a reason for hiding this comment

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

You should always compare the element at index with child_index_left. This applies every place in the heap.

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