Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Congratulations! You're submitting your assignment!

Question | Answer
:------------- | :-------------
How is a Heap different from a Binary Search Tree? |
Could you build a heap with linked nodes? |
Why is adding a node to a heap an O(log n) operation? |
Were the `heap_up` & `heap_down` methods useful? Why? |
How is a Heap different from a Binary Search Tree? | A BST is ordered; A Heap is not entirely ordered. And heaps allow duplicates, unlike BSTs
Could you build a heap with linked nodes? | uh, I guess you *could* but it would not be very efficient to search compared to a normal array
Why is adding a node to a heap an O(log n) operation? | Because it has to figure out where it truly belongs
Were the `heap_up` & `heap_down` methods useful? Why? | Because after big changes, they help every node and child find where to go. They restore balance to the heap.


21 changes: 16 additions & 5 deletions heaps/heap_sort.py
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)?
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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

68 changes: 54 additions & 14 deletions heaps/min_heap.py
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:

Expand All @@ -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)?
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct space complexity is O(log n)

Copy link
Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Caboose? 😆

Copy link
Author

Choose a reason for hiding this comment

The 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):
Expand All @@ -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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I wouldn't name a variable poncho here.

Copy link
Author

Choose a reason for hiding this comment

The 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):
Expand Down