-
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
scissors laurel O #48
base: master
Are you sure you want to change the base?
Changes from 9 commits
5878341
7164322
233b1cb
affd94a
260db52
aa261a2
3c41bdc
6ef6c29
42adbae
4899a71
bfbd290
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(n log n)? | ||
Space Complexitty: O(n)? | ||
""" | ||
pass | ||
listless = MinHeap() | ||
sorts= [] | ||
|
||
for node in list: | ||
listless.add(node) | ||
|
||
for i in range(len(list)): | ||
nod = listless.remove() | ||
sorts.append(nod) | ||
|
||
return sorts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
class HeapNode: | ||
|
||
def initialize(self, key, value): | ||
def __init__(self, key, value): | ||
self.key = key | ||
self.value = value | ||
|
||
def __str__(self): | ||
return str(self.value) | ||
|
||
def __repr__(self): | ||
return str(self.value) | ||
|
||
class MinHeap: | ||
|
||
|
@@ -14,19 +19,31 @@ def __init__(self): | |
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) | ||
Space Complexity: O(1)? O(log n)? | ||
""" | ||
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. 👍 Correct space complexity is O(log n) 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. thank you. |
||
pass | ||
if value == None: | ||
value = key | ||
|
||
self.store.append(HeapNode(key, value)) | ||
self.heap_up(len(self.store) - 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(log n) | ||
Space Complexity: O(log n) | ||
""" | ||
Comment on lines
32
to
37
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. 👍 Caboose? 😆 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. something like a tail? |
||
pass | ||
if len(self.store) == 0: | ||
return None | ||
|
||
caboose = len(self.store) - 1 | ||
self.swap(0, caboose) | ||
smol = self.store.pop() | ||
|
||
self.heap_down(0) | ||
return smol.value | ||
|
||
|
||
def __str__(self): | ||
|
@@ -39,27 +56,50 @@ 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) | ||
""" | ||
pass | ||
return len(self.store) == 0 | ||
|
||
|
||
def heap_up(self, index): | ||
|
||
""" This helper method takes an index and | ||
moves it up the heap, if it is less than it's parent node. | ||
It could be **very** helpful for the add method. | ||
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(log n) | ||
Space complexity: O(log n) | ||
""" | ||
Comment on lines
65
to
72
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 index == 0: | ||
return | ||
|
||
parent = (index - 1) // 2 | ||
store = self.store | ||
if store[parent].key > store[index].key: | ||
self.swap(parent, index) | ||
self.heap_up(parent) | ||
|
||
|
||
def heap_down(self, index): | ||
""" This helper method takes an index and | ||
moves it up the heap if it's smaller | ||
than it's parent node. | ||
""" | ||
Comment on lines
83
to
87
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 wouldn't name a variable 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. WOW, I agree. I am sorry about that. I think I had Willie Nelson's cover of TVZ stuck in my head and was pretty sleepy. (1) I have no business naming anything anything near that (2) I didn't even spell pancho right. |
||
pass | ||
children = index * 2 + 1 | ||
lefty = children | ||
poncho = children + 1 | ||
if lefty < len(self.store): | ||
if poncho < len(self.store): | ||
if self.store[lefty].key < self.store[poncho].key: | ||
smol = lefty | ||
else: | ||
smol = poncho | ||
else: | ||
smol = lefty | ||
|
||
if self.store[index].key > self.store[smol].key: | ||
self.swap(index, smol) | ||
self.heap_down(smol) | ||
|
||
|
||
def swap(self, index_1, index_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.
👍