Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Alli-Oops
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree?
  • The heap can either be min-heap (root is smallest) or max-heap (root is largest)
  • BST doesnt allow duplicates whereas Heap does
  • The BST is an ordered data structure where as the Heap is not.
  • If order matters use BST.
  • If order is irrelevant Heap is nice because it guarantees O(log(n)) time for insert and remove

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.

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.

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: ?
"""

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):

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):

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):

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):

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):

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):

Choose a reason for hiding this comment

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

👍

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