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

Christian C15 - Scissors #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Crintion
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree?
Could you build a heap with linked nodes?
Why is adding a node to a heap an O(log n) operation?
Were the heap_up & heap_down methods useful? Why?

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.

A little awkward, but I think it mostly works. I made a few comments and let me know what questions you have.

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) (Each add is log(n), n nodes must be added)
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 +35 to +36

print(self.store)

Choose a reason for hiding this comment

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

Suggested change
print(self.store)

Comment on lines +22 to 27
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(log(n)) : add tail is constant rebalancing is log(n)
Space Complexity: O(N+1)
"""

Choose a reason for hiding this comment

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

👍 However space complexity should be O(log n) due to the recursive call stack.

Comment on lines +87 to +90
if index == 1 or index == 2:
parent_index = 0
else:
parent_index = int(math.ceil((index-2)/2))

Choose a reason for hiding this comment

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

I don't think you need the if statement, just the arithmetic for the parent_index would work.

Comment on lines +94 to +99
print("Parent")
print(parent_index)
print(type(parent_index))
print("index")
print(index)

Choose a reason for hiding this comment

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

Suggested change
print("Parent")
print(parent_index)
print(type(parent_index))
print("index")
print(index)

left_index = (index*2)+1
right_index = (index*2)+2

if left_index >= len(self.store) or right_index >= len(self.store):

Choose a reason for hiding this comment

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

You can have a left child with no right child.

Suggested change
if left_index >= len(self.store) or right_index >= len(self.store):
if left_index >= len(self.store) and right_index >= len(self.store):

Comment on lines +120 to +126
if self.store[left_index][0] < self.store[index][0]:
self.swap(left_index, index)
self.heap_down(index)

elif self.store[right_index][0] < self.store[index][0]:
self.swap(right_index, index)
self.heap_down(index)

Choose a reason for hiding this comment

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

You shouldn't need to do two swaps here. I think you should compare the left and right children and swap the current index with the min child.

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