From 58783413e68773db2e02bf4ac8c82a5166b0efc2 Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 11 Oct 2021 00:00:34 -0600 Subject: [PATCH 01/11] puttering around --- heaps/heap_sort.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..07cfa83 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,8 +1,35 @@ def heap_sort(list): - """ This method uses a heap to sort an array. - Time Complexity: ? - Space Complexity: ? + """ This method was supposed to use a heap instance to sort an array. But here we are. + sources: + https://towardsdatascience.com/data-structure-heap-23d4c78a6962 + Time Complexity: O(n log n)? + Space Complexity: O(n log n) bc not in place? O(1) if I had figured out how to do it correctly """ - pass \ No newline at end of file + i = 0 + sorted = [] + + def min_heap_sort(list, i): + smol = i + lefty = 2 * i + 1 + poncho = 2 * i + 2 + length = len(list) - 1 + + if lefty <= length and list[i] > list[lefty]: + smol = lefty + if poncho <= length and list[smol] > list[poncho]: + smol = poncho + if smol != i: + list[i], list[smol] = list[smol], list[i] + min_heap_sort(list, smol) + + for i in reversed(range(len(list)//2)): + min_heap_sort(list, i) + + for i in range(len(list)): + list[0], list[-1] = list[-1], list[0] + sorted.append(list.pop()) + min_heap_sort(list, 0) + + return sorted \ No newline at end of file From 7164322ef4d9fef556f508d038ed3a3a9399edbf Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 11 Oct 2021 00:37:21 -0600 Subject: [PATCH 02/11] putter --- heaps/min_heap.py | 59 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index a1340e3..293d115 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -14,19 +14,30 @@ 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)? """ - pass + if value == None: + value = key + + self.store.append(HeapNode(key, value)) + 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) """ - 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 +50,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) """ - 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) """ - 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. """ - pass + children = index * 2 + 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): From 233b1cb1832871c7d067187eb15fa464cbb8bb4b Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 11 Oct 2021 00:40:06 -0600 Subject: [PATCH 03/11] initialize looks weird --- heaps/min_heap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 293d115..4d69a65 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -1,6 +1,6 @@ class HeapNode: - def initialize(self, key, value): + def __init__(self, key, value): self.key = key self.value = value From affd94aca1b9a383e57b648841af9958d60f679c Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 11 Oct 2021 00:41:54 -0600 Subject: [PATCH 04/11] whaaat --- heaps/min_heap.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 4d69a65..662d3bc 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -4,6 +4,11 @@ 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: From 260db52d8338058f952be755b5945560826ee021 Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 11 Oct 2021 00:44:53 -0600 Subject: [PATCH 05/11] heap up less errors --- heaps/min_heap.py | 1 + 1 file changed, 1 insertion(+) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 662d3bc..ee883fd 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -26,6 +26,7 @@ def add(self, key, value = None): value = key self.store.append(HeapNode(key, value)) + self.heap_up(len(self.store) - 1) def remove(self): From aa261a2419b3640bca8bb068bf9d037111a03b43 Mon Sep 17 00:00:00 2001 From: Laurel Date: Wed, 20 Oct 2021 22:12:05 -0600 Subject: [PATCH 06/11] +1 on 89 --- heaps/min_heap.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index ee883fd..8dc210f 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -63,6 +63,7 @@ def empty(self): 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. @@ -80,11 +81,12 @@ def heap_up(self, index): def heap_down(self, index): + #if we are passed zero it may be causing an error """ This helper method takes an index and moves it up the heap if it's smaller than it's parent node. """ - children = index * 2 + children = index * 2 +1 lefty = children poncho = children + 1 if lefty < len(self.store): From 3c41bdc3c8b5d613e2f42f91b6f99133accad6b2 Mon Sep 17 00:00:00 2001 From: Laurel Date: Wed, 20 Oct 2021 23:11:23 -0600 Subject: [PATCH 07/11] answers for PR --- .github/PULL_REQUEST_TEMPLATE | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index 37dfdde..43a1c57 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -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. From 6ef6c29d92301b2fc97cf32c307a64fdd591e2c2 Mon Sep 17 00:00:00 2001 From: Laurel Date: Thu, 21 Oct 2021 17:56:59 -0600 Subject: [PATCH 08/11] woof do the sort now --- heaps/heap_sort.py | 2 +- heaps/min_heap.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 07cfa83..086a4e6 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -5,7 +5,7 @@ def heap_sort(list): sources: https://towardsdatascience.com/data-structure-heap-23d4c78a6962 Time Complexity: O(n log n)? - Space Complexity: O(n log n) bc not in place? O(1) if I had figured out how to do it correctly + """ i = 0 sorted = [] diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 8dc210f..00dd649 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -81,12 +81,11 @@ def heap_up(self, index): def heap_down(self, index): - #if we are passed zero it may be causing an error """ This helper method takes an index and moves it up the heap if it's smaller than it's parent node. """ - children = index * 2 +1 + children = index * 2 + 1 lefty = children poncho = children + 1 if lefty < len(self.store): From 42adbae249bcc89d338d30697ea60472619d2f24 Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 24 Jan 2022 12:39:22 -0700 Subject: [PATCH 09/11] not as listless --- heaps/heap_sort.py | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 086a4e6..32dd7bf 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,35 +1,19 @@ - +from heaps.min_heap import MinHeap def heap_sort(list): - """ This method was supposed to use a heap instance to sort an array. But here we are. - sources: - https://towardsdatascience.com/data-structure-heap-23d4c78a6962 + """ Time Complexity: O(n log n)? - + Space Complexitty: O(n)? """ - i = 0 - sorted = [] - - def min_heap_sort(list, i): - smol = i - lefty = 2 * i + 1 - poncho = 2 * i + 2 - length = len(list) - 1 - - if lefty <= length and list[i] > list[lefty]: - smol = lefty - if poncho <= length and list[smol] > list[poncho]: - smol = poncho - if smol != i: - list[i], list[smol] = list[smol], list[i] - min_heap_sort(list, smol) - - for i in reversed(range(len(list)//2)): - min_heap_sort(list, i) + listless = MinHeap() + sorts= [] + for node in list: + listless.add(node) + for i in range(len(list)): - list[0], list[-1] = list[-1], list[0] - sorted.append(list.pop()) - min_heap_sort(list, 0) - - return sorted \ No newline at end of file + nod = listless.remove() + sorts.append(nod) + + return sorts + \ No newline at end of file From 4899a713b3ba5617ee12afff6df0bbc00b4223cc Mon Sep 17 00:00:00 2001 From: lolkinetzky <81205315+lolkinetzky@users.noreply.github.com> Date: Fri, 4 Feb 2022 05:38:08 -0700 Subject: [PATCH 10/11] Update heap_sort.py --- heaps/heap_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 32dd7bf..fb819c4 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -2,8 +2,8 @@ def heap_sort(list): """ - Time Complexity: O(n log n)? - Space Complexitty: O(n)? + Time Complexity: O(n log n) + Space Complexitty: O(n) """ listless = MinHeap() sorts= [] @@ -16,4 +16,4 @@ def heap_sort(list): sorts.append(nod) return sorts - \ No newline at end of file + From bfbd290ae85bc21bacf573bd076efd444d763d20 Mon Sep 17 00:00:00 2001 From: lolkinetzky <81205315+lolkinetzky@users.noreply.github.com> Date: Fri, 4 Feb 2022 05:42:24 -0700 Subject: [PATCH 11/11] Update min_heap.py --- heaps/min_heap.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 00dd649..b884d57 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -20,7 +20,7 @@ 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: O(log n) - Space Complexity: O(1)? O(log n)? + Space Complexity: O(log n) """ if value == None: value = key @@ -86,16 +86,16 @@ def heap_down(self, index): than it's parent node. """ 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 + left = children + right = children + 1 + if left < len(self.store): + if right < len(self.store): + if self.store[left].key < self.store[right].key: + smol = left else: - smol = poncho + smol = right else: - smol = lefty + smol = left if self.store[index].key > self.store[smol].key: self.swap(index, smol)