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

Leaves - Nicky C. #27

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

Leaves - Nicky C. #27

wants to merge 3 commits into from

Conversation

njch5
Copy link

@njch5 njch5 commented Mar 19, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps guarantee that the elements on the higher levels are greater (in a max heap) or smaller (in a min heap). BST guarantees order from left to right.
Could you build a heap with linked nodes? Yes, but arrays are usually easier to implement.
Why is adding a node to a heap an O(log n) operation? In a worst case scenario, adding a node will require 1 swap per level of the heap (each level is log n).
Were the heap_up & heap_down methods useful? Why? Yes because both help arrange the binary tree in the correct order when adding or removing an element.

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 Nicky, you hit the learning goals here. Well done.

Comment on lines +4 to 6
# Time Complexity: O(nlogn)
# 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.

👍

Comment on lines +16 to 18
# Time Complexity: O(logn)
# Space Complexity: 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.

👍

Comment on lines +26 to 28
# Time Complexity: O(logn)
# 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.

👍

Comment on lines +53 to 55
# Time complexity: O(1)
# 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.

👍

def empty?
raise NotImplementedError, "Method not implemented yet..."
return true if @store.empty?

Choose a reason for hiding this comment

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

Suggested change
return true if @store.empty?
return @store.empty?

Comment on lines +64 to 66
# Time complexity: O(logn)
# Space complexity: O(logn)
def heap_up(index)

Choose a reason for hiding this comment

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

👍 Just note how this impacts the space complexity of add.


# This helper method takes an index and

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
def heap_down(index)

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