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

Gloria - Scissors #38

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

Gloria - Scissors #38

wants to merge 6 commits into from

Conversation

ggrossvi
Copy link

@ggrossvi ggrossvi commented Jan 23, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? The Heap is a Complete Binary Tree. The Heap is not the same as a Binary Search Tree. The Heap, on the other hand, is not an ordered data structure.
Could you build a heap with linked nodes? A priority queue using a heap ordered binary tree can be implemented using a triply linked list structure instead of an array. you will need three links per node:two to traverse down and one to traverse up.
Why is adding a node to a heap an O(log n) operation? . Constructing a heap from an array is O(n) because it uses the build_heap algorithm to rearrange the array in place. That's fundamentally different from doing n insert operations that are O(log n) each in the worst case. Building a heap by repeated insertion is O(n log n).

Were the heap_up & heap_down methods 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.

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

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

👍 But you do need to count 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

Choose a reason for hiding this comment

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

👍 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)
"""

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

Nice set of helper methods

Comment on lines +132 to 134
Time complexity: log(n) only takes one path
Space complexity: O(1) not creating anything
"""

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

👍 Nice iterative solution

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