-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
|
||
from .min_heap import MinHeap | ||
|
||
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) | ||
""" | ||
pass | ||
|
||
heap = MinHeap() | ||
|
||
for item in list: | ||
heap.add(item) | ||
|
||
# Iterate through list by index and remove heap items to build sorted list in the original array | ||
for i in range(0, len(list)): | ||
list[i] = heap.remove() | ||
|
||
return list | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,8 @@ | ||||||||||||
import math | ||||||||||||
|
||||||||||||
|
||||||||||||
class HeapNode: | ||||||||||||
|
||||||||||||
def __init__(self, key, value): | ||||||||||||
self.key = key | ||||||||||||
self.value = value | ||||||||||||
|
@@ -11,73 +14,123 @@ def __repr__(self): | |||||||||||
return str(self.value) | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
class MinHeap: | ||||||||||||
|
||||||||||||
def __init__(self): | ||||||||||||
self.store = [] | ||||||||||||
|
||||||||||||
|
||||||||||||
def add(self, key, value = None): | ||||||||||||
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) | ||||||||||||
""" | ||||||||||||
Comment on lines
+22
to
27
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 value is not None: | ||||||||||||
self.store.append((key, value)) | ||||||||||||
else: | ||||||||||||
self.store.append((key, key)) | ||||||||||||
# Bubble up to re balance tree | ||||||||||||
|
||||||||||||
self.heap_up(len(self.store)-1) # list -1 index is last in list (python) | ||||||||||||
|
||||||||||||
print(self.store) | ||||||||||||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
pass | ||||||||||||
|
||||||||||||
def remove(self): | ||||||||||||
""" This method removes and returns an element from the heap | ||||||||||||
maintaining the heap structure | ||||||||||||
Time Complexity: ? | ||||||||||||
Space Complexity: ? | ||||||||||||
Time Complexity: O(log(n)) : remove head and replace with tail is constant. re balancing is log(n) | ||||||||||||
Space Complexity: O(1) : | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
if self.empty(): | ||||||||||||
return | ||||||||||||
min = self.store[0] # set aside root value | ||||||||||||
self.store[0] = self.store[-1] # Delete root value and replace with last node | ||||||||||||
self.store.pop(-1) | ||||||||||||
self.heap_down(0) # re balance the tree from the root position | ||||||||||||
return min[1] # return value removed from heap | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
def __str__(self): | ||||||||||||
""" This method lets you print the heap, when you're testing your app. | ||||||||||||
""" | ||||||||||||
if len(self.store) == 0: | ||||||||||||
return "[]" | ||||||||||||
return f"[{', '.join([str(element) for element in self.store])}]" | ||||||||||||
return f"[{', '.join([str(element[1]) for element in self.store])}]" | ||||||||||||
|
||||||||||||
|
||||||||||||
def empty(self): | ||||||||||||
""" This method returns true if the heap is empty | ||||||||||||
Time complexity: ? | ||||||||||||
Space complexity: ? | ||||||||||||
Time complexity: O(nlogn) | ||||||||||||
Space complexity: O(n) | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
if len(self.store) == 0: | ||||||||||||
return True | ||||||||||||
|
||||||||||||
return False # ?? implicit? | ||||||||||||
|
||||||||||||
|
||||||||||||
# "Bubble up" to root | ||||||||||||
def heap_up(self, index): | ||||||||||||
""" This helper method takes an index and | ||||||||||||
moves the corresponding element up the heap, if | ||||||||||||
moves the corresponding element up the heap, if | ||||||||||||
it is less than it's parent node until the Heap | ||||||||||||
property is reestablished. | ||||||||||||
|
||||||||||||
This could be **very** helpful for the add method. | ||||||||||||
Time complexity: ? | ||||||||||||
Space complexity: ? | ||||||||||||
Time complexity: O(logn) | ||||||||||||
Space complexity: O(n +1) | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
if index == 0: | ||||||||||||
return | ||||||||||||
|
||||||||||||
if index == 1 or index == 2: | ||||||||||||
parent_index = 0 | ||||||||||||
else: | ||||||||||||
parent_index = int(math.ceil((index-2)/2)) | ||||||||||||
Comment on lines
+87
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||
# if parent_index <= 1: | ||||||||||||
# parent_index = int(1) | ||||||||||||
|
||||||||||||
print("Parent") | ||||||||||||
print(parent_index) | ||||||||||||
print(type(parent_index)) | ||||||||||||
print("index") | ||||||||||||
print(index) | ||||||||||||
|
||||||||||||
Comment on lines
+94
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
if self.store[int(parent_index)][0] > self.store[index][0]: | ||||||||||||
self.swap(index, parent_index) | ||||||||||||
self.heap_up(parent_index) | ||||||||||||
|
||||||||||||
return | ||||||||||||
|
||||||||||||
# "filter down" to child position. | ||||||||||||
def heap_down(self, index): | ||||||||||||
""" This helper method takes an index and | ||||||||||||
moves the corresponding element down the heap if it's | ||||||||||||
""" 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. | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. You can have a left child with no right child.
Suggested change
|
||||||||||||
return | ||||||||||||
|
||||||||||||
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) | ||||||||||||
Comment on lines
+120
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||
|
||||||||||||
|
||||||||||||
def swap(self, index_1, index_2): | ||||||||||||
""" Swaps two elements in self.store | ||||||||||||
at index_1 and index_2 | ||||||||||||
used for heap_up & heap_down | ||||||||||||
""" | ||||||||||||
print("Swapping ", index_1, " ", index_2) | ||||||||||||
temp = self.store[index_1] | ||||||||||||
self.store[index_1] = self.store[index_2] | ||||||||||||
self.store[index_2] = temp | ||||||||||||
self.store[index_2] = temp |
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.
👍