Skip to content

Paper - Al Leonard Heap Assignment#29

Open
Alli-Oops wants to merge 2 commits into
Ada-C15:masterfrom
Alli-Oops:master
Open

Paper - Al Leonard Heap Assignment#29
Alli-Oops wants to merge 2 commits into
Ada-C15:masterfrom
Alli-Oops:master

Conversation

@Alli-Oops
Copy link
Copy Markdown

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
Copy Markdown

@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 thread heaps/heap_sort.py
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: ?
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 What about space complexity?

Comment thread heaps/min_heap.py

self.store = [] # this is the list where we store stuff

def add(self, key, value = None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread heaps/min_heap.py
# using the parent's index number.
self.heap_up(len(self.store) - 1)

def remove(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread heaps/min_heap.py

return remove_val.value

def __str__(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread heaps/min_heap.py
return f"[{', '.join([str(element) for element in self.store])}]"


def empty(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread heaps/min_heap.py
return len(self.store) == 0


def heap_up(self, index):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread heaps/min_heap.py
index = parent_index
parent_index = (index-1)//2

def heap_down(self, index):
Copy link
Copy Markdown

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