Skip to content

Earth - Christina Minh#13

Open
christinaminh wants to merge 6 commits intoAda-C14:masterfrom
christinaminh:master
Open

Earth - Christina Minh#13
christinaminh wants to merge 6 commits intoAda-C14:masterfrom
christinaminh:master

Conversation

@christinaminh
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 heap, every node is either larger than (max heap) or smaller than (min heap) than its children. In a binary search tree, all nodes to the left of its parent is smaller and all nodes to the right of its parent are larger.
Could you build a heap with linked nodes? You could build a heap with linked nodes but a heap is easier to implement with an array. It's easy to determine the index of the children of any node.
Why is adding a node to a heap an O(log n) operation? To add a node, the value is added to the end of the array and the value is bubbled up (heap up) to maintain the heap structure. It could take 1 swap per level and there are log n levels in a heap.
Were the heap_up & heap_down methods useful? Why? The heap_up and heap_down methods maintain the heap structure after an node is added or removed from the heap

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.

This was outstanding work Christina. Well done. I especially like all the helper methods, that made following your, very clean code, especially easy.

The only note here as you maybe are doing Python now. Normally we indent with 2 spaces in Ruby.

Comment on lines 3 to 6
# # This method uses a heap to sort an array.
# # Time Complexity: O(n log n) to add elements to a heap and swap them in order at each level
# # Space Complexity: O(log n) for recursive heap_up in .add
def heapsort(list)

Choose a reason for hiding this comment

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

👍 , except that the space complexity is O(n) since you're adding all the elements to a heap here.

Comment on lines +35 to +37
# Time Complexity: O(n log n) to add elements to a heap and swap them in order at each level
# Space Complexity: O(1)
def heapsort_in_place(list)

Choose a reason for hiding this comment

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

👍 This is really nice Christina. Well done !

Comment on lines +27 to 29
# Time Complexity: O(log n) to swap for each level
# Space Complexity: O(log n) for the call stack in heap_down??
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 +57 to 59
# 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(log n) to swap for each level
# Space complexity: O(log n) for the call stack
def heap_up(index)

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(log n) to swap for each level
# Space Complexity: O(log n) for the call stack in heap_up??
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 +83 to 85
# moves it down the heap if it's larger
# than it's children 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.

👍 I love the helper methods here, super readable.

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