Christian C15 - Scissors#44
Open
Crintion wants to merge 1 commit into
Open
Conversation
CheezItMan
reviewed
Jan 26, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
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) | ||
| """ |
Comment on lines
+35
to
+36
|
|
||
| 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) | ||
| """ |
There was a problem hiding this comment.
👍 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)) |
There was a problem hiding this comment.
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) | ||
|
|
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?