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 - Georgina #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Leaves - Georgina #18

wants to merge 1 commit into from

Conversation

geomsb
Copy link

@geomsb geomsb commented Mar 16, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? In a Binary Search Tree all the nodes from the left are smaller than the root and all the nodes from the right are bigger. We can have max heap where the root is the biggest element and we can have a min heap where the root is the smallest element.
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? Because its going to traverse the height of the tree
Where the heap_up & heap_down methods useful? Why? heap_up is useful to add an element because it compares the new node with it's parent until it finds the correct place for the node. heap_down is useful for the remove method.

@geomsb geomsb changed the title heap methods: Leaves - Georgina Mar 16, 2020
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 well done, you hit the major learning goals here. Do take a look at my comments with regards to Space complexity. You do need to account for the system stack with recursion.

Comment on lines +3 to +5
# Time Complexity: O(nlogn)
# Space Complexity: O(n)
def heapsort(list)

Choose a reason for hiding this comment

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

👍

Comment on lines +17 to 19
# Time Complexity: O(logn)
# Space Complexity: 0(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 +55 to 57
# 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.

👍

Comment on lines +68 to 70
# Time complexity: O(logn)
# Space complexity: O(1)
def heap_up(index)

Choose a reason for hiding this comment

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

Since this method is recursive, you have O(log n) space complexity. You have to account for the system stack.

@store[index] = @store[parent]
@store[parent]= temp
heap_up(parent)
end
end

# 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