-
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
Paper - Al Leonard Heap Assignment #29
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 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.
def heap_sort(list): | ||
""" This method uses a heap to sort an array. | ||
Time Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: ? | ||
""" |
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.
👍 What about space complexity?
def __init__(self): | ||
self.store = [] | ||
|
||
self.store = [] # this is the list where we store stuff | ||
|
||
def add(self, key, value = 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.
👍
# Compare the new node with it's parent | ||
# If they are out of order swap and heap-up | ||
# using the parent's index number. | ||
self.heap_up(len(self.store) - 1) | ||
|
||
def remove(self): |
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.
👍
|
||
return remove_val.value | ||
|
||
def __str__(self): |
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 __str__(self): | ||
""" This method lets you print the heap, when you're testing your app. | ||
""" | ||
This method lets you print the heap, when you're testing your app. | ||
""" | ||
if len(self.store) == 0: | ||
return "[]" | ||
return f"[{', '.join([str(element) for element in self.store])}]" | ||
|
||
|
||
def empty(self): |
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.
👍
""" | ||
pass | ||
return len(self.store) == 0 | ||
|
||
|
||
def heap_up(self, index): |
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.
👍
while index>0 and self.store[index].key < self.store[parent_index].key: | ||
self.swap(index,parent_index) | ||
index = parent_index | ||
parent_index = (index-1)//2 | ||
|
||
def heap_down(self, index): |
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.
👍
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_down
methods 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.