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 - Samantha #33

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

Leaves - Samantha #33

wants to merge 2 commits into from

Conversation

iamsammysam
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heap is an almost complete tree, where every level of the tree if full except from the last level, that must be filled left to right.
Could you build a heap with linked nodes? Yes, but it is way easier to implement it with an array.
Why is adding a node to a heap an O(log n) operation? Performing the heap-up operation will at worst-case perform one swap per level of the heap and there are log n levels to the heap.
Where the heap_up & heap_down methods useful? Why? Heap_up => to add an element to the heap. You place it into the end of the array, then you do a "heap-up" operation comparing the new node to its parent and swapping them if they are out of order. Heap_down => to remove an element from the heap. You swap the last leaf with the root, then compare the new root with its children and swap to maintain the heap order using. the operation heap-down. The heap down operation is repeated until a leaf node is reached or no swaps are made.

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.

I apologize for taking so long to look at this. You did a fantastic job here. The code is clean and readable and everything works well. A few small things on space complexity, otherwise great!

Comment on lines +4 to 6
# Time Complexity: On(log n)
# Space Complexity: 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.

You are building a heap here... so you DO have some space complexity even if that heap is empty by the end.

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(log n)
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 +28 to 30
# Time Complexity: O(log n)
# Space Complexity: O(log n)
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 +66 to 68
# Time complexity: O(log n)
# 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.

You do have space complexity here brought upon by the recursion involved.

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.

Very clean code here I like it!

@iamsammysam
Copy link
Author

iamsammysam commented Apr 21, 2020 via email

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