-
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
Christian C15 - Scissors #44
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.
A little awkward, but I think it mostly works. I made a few comments and let me know what questions you have.
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) | ||
""" |
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.
👍
|
||
print(self.store) |
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.
print(self.store) |
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) | ||
""" |
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.
👍 However space complexity should be O(log n) due to the recursive call stack.
if index == 1 or index == 2: | ||
parent_index = 0 | ||
else: | ||
parent_index = int(math.ceil((index-2)/2)) |
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.
I don't think you need the if statement, just the arithmetic for the parent_index
would work.
print("Parent") | ||
print(parent_index) | ||
print(type(parent_index)) | ||
print("index") | ||
print(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.
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): |
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.
You can have a left child with no right child.
if left_index >= len(self.store) or right_index >= len(self.store): | |
if left_index >= len(self.store) and right_index >= len(self.store): |
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) |
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.
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.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?