From 0aa80b42c260fb3cec9cdba33b0fee69e63dbc3c Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Thu, 1 Oct 2020 20:07:19 -0700 Subject: [PATCH 1/3] min heap passing --- lib/min_heap.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..fe5ed32 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,3 +1,5 @@ +require 'pry' + class HeapNode attr_reader :key, :value @@ -17,7 +19,11 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + + @store.push(HeapNode.new(key,value)) + child = @store.length - 1 + heap_up(child) if child != 0 + end # This method removes and returns an element from the heap @@ -25,7 +31,13 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + + swap(0, @store.length - 1) + deleted = @store.pop + heap_down(0) unless self.empty? + + return deleted.value + end @@ -47,7 +59,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return false unless @store[0].nil? end private @@ -58,14 +70,42 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) - + + parent = (index - 2) / 2 if index.even? + parent = (index - 1) / 2 if index.odd? + + if @store[parent].key > @store[index].key + swap(parent, index) + heap_up(parent) if parent != 0 + end + end # This helper method takes an index and # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + + left = (2 * index) + 1 + right = (2 * index) + 2 + + + if !@store[right].nil? + min_child = left if @store[left].key < @store[right].key + min_child = right if @store[left].key > @store[right].key + elsif @store[left].nil? + return + else + min_child = left + end + + if @store[index].key > @store[min_child].key + swap(index, min_child) + index = min_child + heap_down(index) + end + + end # If you want a swap method... you're welcome From 6e017dff80bb7ef0de57d75b191d97d24546ee48 Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Fri, 2 Oct 2020 12:29:25 -0700 Subject: [PATCH 2/3] Implemeneted heap sort --- lib/heap_sort.rb | 21 +++++++++++++++++++-- lib/min_heap.rb | 20 ++++++++++++-------- test/heapsort_test.rb | 2 +- test/min_heap_test.rb | 1 + 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..2492db5 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,25 @@ - +require_relative 'min_heap' +require 'pry' # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + + return [] if list.empty? + # create heap + heap = MinHeap.new + + list.each do |element| + heap.add(element) + end + + until heap.empty? + current = heap.remove + list.push(current) + list.shift unless list.length == 1 + end + + return list + end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index fe5ed32..a905e91 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,4 +1,3 @@ -require 'pry' class HeapNode attr_reader :key, :value @@ -16,8 +15,8 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def add(key, value = key) @store.push(HeapNode.new(key,value)) @@ -28,8 +27,8 @@ def add(key, value = key) # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def remove() swap(0, @store.length - 1) @@ -59,7 +58,12 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - return false unless @store[0].nil? + if @store[0].nil? || @store.empty? + return true + end + + return false + end private @@ -67,8 +71,8 @@ def empty? # 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(n) + # Space complexity: O(1) def heap_up(index) parent = (index - 2) / 2 if index.even? diff --git a/test/heapsort_test.rb b/test/heapsort_test.rb index 34402ac..7ce79b7 100644 --- a/test/heapsort_test.rb +++ b/test/heapsort_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heapsort" do it "sorts an empty array" do # Arrange list = [] diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..d3193fc 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -59,6 +59,7 @@ heap.add(0, "Donuts") heap.add(16, "Cookies") heap.add(57, "Cake") + # Act removed = heap.remove From 68fde336966e95b71b1b154bf40a652fe2ee5c41 Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Fri, 2 Oct 2020 12:32:16 -0700 Subject: [PATCH 3/3] Added time complexities --- lib/heap_sort.rb | 4 ++-- lib/min_heap.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 2492db5..e762cb4 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -2,8 +2,8 @@ require 'pry' # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n log n) +# Space Complexity: O(n) def heapsort(list) return [] if list.empty? diff --git a/lib/min_heap.rb b/lib/min_heap.rb index a905e91..e2f51e2 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -55,15 +55,15 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) + # Space complexity: O(1) def empty? if @store[0].nil? || @store.empty? return true end return false - + end private