Gloria - Scissors#38
Open
ggrossvi wants to merge 6 commits into
Open
Conversation
CheezItMan
reviewed
Jan 24, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Gloria, you hit the learning goals here. Nice work on the iterative solutions.
Comment on lines
3
to
7
| 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) | ||
| """ |
Comment on lines
27
to
32
| 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 | ||
| """ |
Comment on lines
47
to
+51
| 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.
👍 Ditto you need to count heap_down
Comment on lines
79
to
83
| def empty(self): | ||
| """ This method returns true if the heap is empty | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(1) | ||
| Space complexity: O(1) | ||
| """ |
Comment on lines
+90
to
+114
| 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 |
Comment on lines
+132
to
134
| Time complexity: log(n) only takes one path | ||
| Space complexity: O(1) not creating anything | ||
| """ |
Comment on lines
143
to
151
| 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 | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
Were the
heap_up&heap_downmethods 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.