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

Christian C15 - Scissors #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions heaps/heap_sort.py
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)
"""
Comment on lines 3 to 7

Choose a reason for hiding this comment

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

👍

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


101 changes: 77 additions & 24 deletions heaps/min_heap.py
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
Expand All @@ -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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Suggested change
print(self.store)

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

Choose a reason for hiding this comment

The 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 parent_index would work.

# 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

Choose a reason for hiding this comment

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

Suggested change
print("Parent")
print(parent_index)
print(type(parent_index))
print("index")
print(index)


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):

Choose a reason for hiding this comment

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

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):

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

Choose a reason for hiding this comment

The 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