-
Notifications
You must be signed in to change notification settings - Fork 56
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
Gloria - Scissors #38
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work Gloria, you hit the learning goals here. Nice work on the iterative solutions.
def heap_sort(list): | ||
""" This method uses a heap to sort an array. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(nlogn) -- n for loop log n for heap sort | ||
Space Complexity: O(n) | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
def add(self, key, value = None): | ||
""" This method adds a HeapNode instance to the heap | ||
If value == None the new node's value should be set to key | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(1) not counting heap_up | ||
Space Complexity: O(1) not counting heap_up | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 But you do need to count heap_up
def remove(self): | ||
""" This method removes and returns an element from the heap | ||
maintaining the heap structure | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(1) not counting heap_down | ||
Space Complexity: O(1) not counting heap_down |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Ditto you need to count heap_down
def empty(self): | ||
""" This method returns true if the heap is empty | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(1) | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
def parent(self, pos): | ||
if pos == 0: | ||
return 0 | ||
|
||
return (pos-1)//2 | ||
|
||
# Function to return the position of | ||
# the left child for the node currently | ||
# at pos | ||
def leftChild(self, pos): | ||
child_position = 2 * pos + 1 | ||
if child_position < self.size: | ||
return child_position | ||
return None | ||
|
||
# Function to return the position of | ||
# the right child for the node currently | ||
# at pos | ||
|
||
def rightChild(self, pos): | ||
child_position = 2 * pos + 2 | ||
#maybe add to left child | ||
if child_position < self.size: | ||
return child_position | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice set of helper methods
Time complexity: log(n) only takes one path | ||
Space complexity: O(1) not creating anything | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
def heap_down(self, index): | ||
""" This helper method takes an index and | ||
moves the corresponding element down the heap if it's | ||
larger than either of its children and continues until | ||
the heap property is reestablished. | ||
|
||
Time complexity: log(n) only takes one path | ||
Space complexity: O(1) not creating anything | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice iterative solution
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
Were the
heap_up
&heap_down
methods useful? Why? | Heapify up is used when we insert a new element to a heap. When inserting a new element, we add it at the bottom of the heap tree, and move up the tree while comparing to the current parent element and swapping if needed. Because we move up for heapify up, we only make one comparison per iteration, between the current element and its parent element.Heapify down is used when we remove the top element from a heap. Removal of an element is done by swapping the top element with the last element at the bottom of the tree, removing the last element, and then heapfying the new top element down to maintain the heap property. Because this moves down the heap tree, it must perform two comparisons per iteration, with the left child and the right child elements, then swap with the smaller one. Because of this, heapify down is usually more complex to implement than heapify up.