-
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
Gloria - Scissors #38
base: master
Are you sure you want to change the base?
Changes from all commits
da148ff
340230b
76f3303
abd823c
3dfa4e2
bbce3f7
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,19 @@ | ||
|
||
from heaps.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) -- n for loop log n for heap sort | ||
Space Complexity: O(n) | ||
""" | ||
pass | ||
sorted_list = [] | ||
heap = MinHeap() | ||
|
||
for element in list: | ||
heap.add(element) | ||
for i in range(len(list)): | ||
min = heap.remove() | ||
sorted_list.append(min) | ||
return sorted_list | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# from tests.test_min_heap import heap | ||
|
||
|
||
from lib2to3.pytree import Node | ||
|
||
|
||
class HeapNode: | ||
|
||
def __init__(self, key, value): | ||
|
@@ -10,32 +16,58 @@ def __str__(self): | |
def __repr__(self): | ||
return str(self.value) | ||
|
||
|
||
|
||
class MinHeap: | ||
|
||
def __init__(self): | ||
self.store = [] | ||
self.size = 0 | ||
self.FRONT = 0 | ||
|
||
|
||
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 | ||
""" | ||
Comment on lines
27
to
32
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. 👍 But you do need to count |
||
pass | ||
if value == None: | ||
value = key | ||
node = HeapNode(key, value) | ||
print(node) | ||
|
||
|
||
# Need to increment size | ||
self.store.insert(self.size, node) | ||
self.size +=1 | ||
|
||
# use self when referring to method or attribute of the class | ||
self.heap_up(self.size-1) | ||
|
||
|
||
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 | ||
Comment on lines
47
to
+51
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. 👍 Ditto you need to count |
||
""" | ||
pass | ||
|
||
# take copy first | ||
# copy last one to position 1 | ||
# modify size | ||
# heapify | ||
|
||
if self.empty() == True: | ||
return None | ||
|
||
min = self.store[self.FRONT] | ||
|
||
self.store[self.FRONT] = self.store[self.size-1] | ||
self.size -= 1 | ||
self.heap_down(self.FRONT) | ||
# min returns object we need it to return value min.value | ||
return min.value | ||
|
||
|
||
|
||
def __str__(self): | ||
""" This method lets you print the heap, when you're testing your app. | ||
""" | ||
|
@@ -46,11 +78,49 @@ def __str__(self): | |
|
||
def empty(self): | ||
""" This method returns true if the heap is empty | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(1) | ||
""" | ||
Comment on lines
79
to
83
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. 👍 |
||
pass | ||
if len(self.store)== 0: | ||
return True | ||
|
||
# Function to return the position of | ||
# parent for the node currently | ||
# at pos | ||
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 | ||
Comment on lines
+90
to
+114
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. Nice set of helper methods |
||
|
||
# Function that returns true if the passed | ||
# node is a leaf node if it is leaf node (no left and right children) then it does not have children, it is not a parent | ||
def isLeaf(self, pos): | ||
if pos >= (self.size//2) and pos < self.size: | ||
return True | ||
return False | ||
|
||
|
||
|
||
def heap_up(self, index): | ||
""" This helper method takes an index and | ||
|
@@ -59,25 +129,77 @@ def heap_up(self, index): | |
property is reestablished. | ||
|
||
This could be **very** helpful for the add method. | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: log(n) only takes one path | ||
Space complexity: O(1) not creating anything | ||
""" | ||
Comment on lines
+132
to
134
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. 👍 |
||
pass | ||
|
||
#self.store[index] value for the new node that we are adding | ||
#self.parent(index) is the value of the current parent/leaf - that may have its first child or second if child is smaller we need to swap | ||
current = index | ||
while self.store[current].key < self.store[self.parent(current)].key: | ||
self.swap(current, self.parent(current)) | ||
current = self.parent(current) | ||
# I don't think I am checking the whether the right key is bigger than the left. | ||
|
||
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 | ||
""" | ||
Comment on lines
143
to
151
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. 👍 Nice iterative solution |
||
pass | ||
# If the node is a non-leaf (not a leaf) node and greater | ||
# than any of its child -- if it is a parent then | ||
#check right child if exists and check it's index and see if it is greater than or = the store.size | ||
|
||
#keep looping while given index current = index > value of it's children so check both children | ||
current = index | ||
# check if current if a leaf node | ||
|
||
hasLeftChild = self.leftChild(current) is not None | ||
hasRightChild = self.rightChild(current) is not None | ||
|
||
|
||
#see if it has both children | ||
#compare and swap with the smaller | ||
|
||
while (hasLeftChild and (self.store[current].key > self.store[self.leftChild(current)].key)) or (hasRightChild and (self.store[current].key > self.store[self.rightChild(current)].key)): | ||
if hasLeftChild and hasRightChild: | ||
if self.store[self.leftChild(current)].key > self.store[self.rightChild(current)].key: | ||
self.swap(current, self.rightChild(current)) | ||
current = self.rightChild(current) | ||
else: | ||
self.swap(current, self.leftChild(current)) | ||
current = self.leftChild(current) | ||
|
||
else: | ||
self.swap(current, self.leftChild(current)) | ||
current = self.leftChild(current) | ||
print('left child:', current) | ||
|
||
hasLeftChild = self.leftChild(current) is not None | ||
hasRightChild = self.rightChild(current) is not None | ||
|
||
|
||
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 | ||
""" | ||
|
||
temp = self.store[index_1] | ||
self.store[index_1] = self.store[index_2] | ||
self.store[index_2] = temp | ||
""" | ||
|
||
self.store[index_1], self.store[index_2] = self.store[index_2], self.store[index_1] | ||
|
||
# Function to build the min heap using | ||
# the minHeapify function | ||
# range(start, stop[, step]) | ||
def my_minHeap(self): | ||
|
||
for pos in range(self.size // 2, 0, -1): | ||
self.heap_down(pos) | ||
|
||
|
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.
👍