Paper - Al Leonard Heap Assignment#29
Open
Alli-Oops wants to merge 2 commits into
Open
Conversation
CheezItMan
reviewed
Jan 11, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Al, all the methods here work well. However you didn't answer any of the time/space sections. It's good to try those even if you are not sure, as it's good practice.
Comment on lines
3
to
7
| def heap_sort(list): | ||
| """ This method uses a heap to sort an array. | ||
| Time Complexity: ? | ||
| Time Complexity: O(log n) | ||
| Space Complexity: ? | ||
| """ |
|
|
||
| self.store = [] # this is the list where we store stuff | ||
|
|
||
| def add(self, key, value = None): |
| # using the parent's index number. | ||
| self.heap_up(len(self.store) - 1) | ||
|
|
||
| def remove(self): |
|
|
||
| return remove_val.value | ||
|
|
||
| def __str__(self): |
| return f"[{', '.join([str(element) for element in self.store])}]" | ||
|
|
||
|
|
||
| def empty(self): |
| return len(self.store) == 0 | ||
|
|
||
|
|
||
| def heap_up(self, index): |
| index = parent_index | ||
| parent_index = (index-1)//2 | ||
|
|
||
| def heap_down(self, index): |
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
Could you build a heap with linked nodes? |
It wouldnt make sense to implement a heap as a linked list. You can store a heap in an array because it's easy to compute the array index of a node's children. It much more efficient to find a given element of an array vs in a linked list.
Why is adding a node to a heap an O(log n) operation? |
Binary heaps are implemented using "partially sorted" arrays. The order of elements in the array is not arbitrary, but it is also not completely determined like sorted array which is O(n) to add a node. We are only guaranteed that A[i] ≥ A[2i],A[2i+1]. This freedom allows us to trade-off the running time of Insert() its better than linear time.
Were the
heap_up&heap_downmethods useful? Why? |With heap_up... Because we add at the bottom and move up, we only make one comparison per iteration, between the current element and its parent element.
heap_down removes the top element from a heap by swapping the top element with the last element at the bottom of the tree, removing the last element, and then heap_up 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, heap_down was more complex to implement than heap_up.
In terms of usefulness, I am unsure if heap_up and heap_down are transferable concepts to other applications, beyond implementing and learning about a heap data structure.