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

Ngoc L #25

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

Ngoc L #25

wants to merge 3 commits into from

Conversation

lebaongoc
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? In a BST, all nodes to the left of a parent are smaller in value than nodes to the right, in Heap, left child can be greater than the right child. The following properties only apply to a Heap, but not a Binary Search Tree: In a min-heap, every parent nodes are smaller than child nodes. In a max-heap, every parent nodes are greater than child nodes. Heap is left-complete, meaning the last level of the tree can be incomplete and it is filled from left to right
Could you build a heap with linked nodes? yes :)
Why is adding a node to a heap an O(log n) operation? because you only compare the number added to the parents and the number of parents is log n
Were the heap_up & heap_down methods useful? Why? yes, these methods were usefull as they help with the add and remove methods

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.

Nice work. Thanks for getting this in. Note my comments on heap_down and on your space complexity overall. You need to remember that recursion incurs some space complexity.

You hit the learning goals here, well done!

while @store[index].key < @store[parent_index].key && parent_index > -1
swap(index,parent_index)
index = parent_index
if index.odd?

Choose a reason for hiding this comment

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

Actually due to truncation it doesn't matter if the index is even or odd.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(logn) adding element to end of array is O(1) while heap-up worst case is O(logn)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Remember since your heap_up and heap_down methods are recursive, you have some space complexity from use of the system stack. So space complexity is O(log n)

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