From 142a34f4ef5ea158864d6afd2d704596a23b1350 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sun, 22 Mar 2020 22:31:53 -0700 Subject: [PATCH 1/4] completed min heap --- lib/heap_sort.rb | 2 - lib/min_heap.rb | 90 +++++++++++++++++++++++++++++-------------- test/min_heap_test.rb | 74 +++++++++++++++++------------------ 3 files changed, 98 insertions(+), 68 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..ca6c499 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,5 +1,3 @@ - - # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..bbbadc5 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -1,6 +1,6 @@ class HeapNode attr_reader :key, :value - + def initialize(key, value) @key = key @value = value @@ -8,66 +8,98 @@ def initialize(key, value) end class MinHeap - + def initialize @store = [] end - + # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? - def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + # Time Complexity: O(log n) where n is number of nodes + # Space Complexity: O(1) + def add(key, value) + @store << HeapNode.new(key, value) + + heap_up(@store.length - 1) end - + # This method removes and returns an element from the heap # maintaining the heap structure - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(log n) where n is number of nodes + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + # binding.pry + to_remove = @store[0] + swap(0, (@store.length - 1)) + @store.pop + + heap_down(0) + + return to_remove.value end - - + + # Used for Testing def to_s return "[]" if @store.empty? - + output = "[" (@store.length - 1).times do |index| output += @store[index].value + ", " end - + output += @store.last.value + "]" - + return output end - + # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end - + private - + # 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) where n is number of nodes + # Space complexity: O(1) def heap_up(index) - + if index != 0 + parent = ((index - 1) / 2) + if @store[index].key < @store[parent].key + swap(index, parent) + heap_up(parent) + else + return + end + end + return end - + # This helper method takes an index and - # moves it up the heap if it's smaller + # moves it down the heap if it's smaller # than it's parent node. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + left_child = (index * 2 + 1) + right_child = (index * 2 + 2) + + until @store[left_child] == nil || @store[right_child] == nil + if (@store[index].key > @store[left_child].key) || (@store[index].key > @store[right_child].key) + if @store[left_child].key < @store[right_child].key + swap(index, left_child) + heap_down(left_child) + else + swap(index, right_child) + heap_down(right_child) + end + end + return + end end - + # If you want a swap method... you're welcome def swap(index_1, index_2) temp = @store[index_1] diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..10c571a 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -7,33 +7,33 @@ # Assert expect(heap).must_be_instance_of MinHeap end - + it "can have nodes added" do # Arrange key = 5 value = "Pasta" - + # Assert expect(heap).must_respond_to :add - + # Act heap.add(key, value) end - + it "adds nodes in a proper order" do # Arrange heap.add(3, "Pasta") heap.add(6, "Soup") heap.add(1, "Pizza") - + # Act output = heap.to_s - + # Assert - + expect(output).must_equal "[Pizza, Soup, Pasta]" end - + it "adds nodes in a proper order with a lot of nodes" do # Arrange heap.add(3, "Pasta") @@ -42,40 +42,40 @@ heap.add(0, "Donuts") heap.add(16, "Cookies") heap.add(57, "Cake") - + # Act output = heap.to_s - + # Assert - + expect(output).must_equal "[Donuts, Pizza, Pasta, Soup, Cookies, Cake]" end - + it "can remove nodes in the proper order" do - # Arrange - heap.add(3, "Pasta") - heap.add(6, "Soup") - heap.add(1, "Pizza") - heap.add(0, "Donuts") - heap.add(16, "Cookies") - heap.add(57, "Cake") - - # Act - removed = heap.remove - - # Assert - expect(removed).must_equal "Donuts" - - # Another Act - removed = heap.remove - - # Another assert - expect(removed).must_equal "Pizza" - - # Another Act - removed = heap.remove - - # Another assert - expect(removed).must_equal "Pasta" + # Arrange + heap.add(3, "Pasta") + heap.add(6, "Soup") + heap.add(1, "Pizza") + heap.add(0, "Donuts") + heap.add(16, "Cookies") + heap.add(57, "Cake") + + # Act + removed = heap.remove + + # Assert + expect(removed).must_equal "Donuts" + + # Another Act + removed = heap.remove + + # Another assert + expect(removed).must_equal "Pizza" + + # Another Act + removed = heap.remove + + # Another assert + expect(removed).must_equal "Pasta" end end \ No newline at end of file From f621b45f7e32714d178057ae37f9aa81faad062a Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sat, 28 Mar 2020 21:34:31 -0700 Subject: [PATCH 2/4] attempted heapsort --- lib/heap_sort.rb | 16 +++++++++- test/{heapsort_test.rb => heap_sort_test.rb} | 33 +++++++++++++------- 2 files changed, 37 insertions(+), 12 deletions(-) rename test/{heapsort_test.rb => heap_sort_test.rb} (55%) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index ca6c499..6aaa45f 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,6 +1,20 @@ +require 'min_heap' + # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." + heap = MinHeap.new + + list.each do |num| + heap.add(num, num) + end + + counter = 0 + until counter > list.length - 1 + list[counter] = heap.remove + counter += 1 + end + + return list end \ No newline at end of file diff --git a/test/heapsort_test.rb b/test/heap_sort_test.rb similarity index 55% rename from test/heapsort_test.rb rename to test/heap_sort_test.rb index 34402ac..cbd3399 100644 --- a/test/heapsort_test.rb +++ b/test/heap_sort_test.rb @@ -1,24 +1,24 @@ require_relative "test_helper" -xdescribe "heapsort" do +describe "heapsort" do it "sorts an empty array" do # Arrange list = [] - + # Act - result = heapsort(list) - + result = heap_sort(list) + # Assert expect(result).must_equal [] end - + it "can sort a 1-element array" do # Arrange list = [5] - + # Act - result = heapsort(list) - + result = heap_sort(list) + # Assert expect(result).must_equal [5] end @@ -26,11 +26,22 @@ it "can sort a 5-element array" do # Arrange list = [5, 27, 3, 16, -50] - + # Act - result = heapsort(list) - + result = heap_sort(list) + # Assert expect(result).must_equal [-50, 3, 5, 16, 27] end + + it "can sort a different 5-element array" do + # Arrange + list = [-4, 49, 9, 31, 47] + + # Act + result = heap_sort(list) + + # Assert + expect(result).must_equal [-4, 9, 31, 47, 49] + end end \ No newline at end of file From 3d21a82e28385ce2d26029f81d179c7db0690192 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sat, 28 Mar 2020 21:34:55 -0700 Subject: [PATCH 3/4] added a comment on heapsort --- lib/heap_sort.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6aaa45f..f4ef758 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -17,4 +17,6 @@ def heap_sort(list) end return list -end \ No newline at end of file +end + +# I'm having trouble figuring out why the "can sort a 5-element array" test will not pass for this. I have created a new "can sort a different 5-element array" test and that passes. I am thinking it has to do with the order of numbers - some exact pattern of numbers is causing the heap to not sort properly, but all my tests from the min_heap are passing. Could I get some help? \ No newline at end of file From 1d16b67c1102d88d7709e6d3d5332209f3b26d0c Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 23 Apr 2020 22:26:56 -0700 Subject: [PATCH 4/4] fixed bug in min_heap --- lib/heap_sort.rb | 14 ++++++-------- lib/min_heap.rb | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index f4ef758..b7ee6f7 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,22 +1,20 @@ require 'min_heap' # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n log n) +# Space Complexity: O(n) def heap_sort(list) heap = MinHeap.new - list.each do |num| - heap.add(num, num) + list.each do |num| # T: O(n) + heap.add(num, num) # S: O(n) end counter = 0 until counter > list.length - 1 - list[counter] = heap.remove + list[counter] = heap.remove # T: O(log n) counter += 1 end return list -end - -# I'm having trouble figuring out why the "can sort a 5-element array" test will not pass for this. I have created a new "can sort a different 5-element array" test and that passes. I am thinking it has to do with the order of numbers - some exact pattern of numbers is causing the heap to not sort properly, but all my tests from the min_heap are passing. Could I get some help? \ No newline at end of file +end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index bbbadc5..166fc14 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,14 +8,13 @@ def initialize(key, value) end class MinHeap - def initialize @store = [] end # This method adds a HeapNode instance to the heap # Time Complexity: O(log n) where n is number of nodes - # Space Complexity: O(1) + # Space Complexity: O(log n) def add(key, value) @store << HeapNode.new(key, value) @@ -25,9 +24,11 @@ def add(key, value) # This method removes and returns an element from the heap # maintaining the heap structure # Time Complexity: O(log n) where n is number of nodes - # Space Complexity: O(1) + # Space Complexity: O(log n) def remove() # binding.pry + return ArgumentError if @store == nil + to_remove = @store[0] swap(0, (@store.length - 1)) @store.pop @@ -72,8 +73,6 @@ def heap_up(index) if @store[index].key < @store[parent].key swap(index, parent) heap_up(parent) - else - return end end return @@ -86,7 +85,7 @@ def heap_down(index) left_child = (index * 2 + 1) right_child = (index * 2 + 2) - until @store[left_child] == nil || @store[right_child] == nil + if @store[left_child] && @store[right_child] if (@store[index].key > @store[left_child].key) || (@store[index].key > @store[right_child].key) if @store[left_child].key < @store[right_child].key swap(index, left_child) @@ -96,8 +95,16 @@ def heap_down(index) heap_down(right_child) end end - return end + + if @store[left_child] && @store[right_child] == nil + if @store[left_child].key < @store[index].key + swap(index, left_child) + heap_down(left_child) + end + end + + return end # If you want a swap method... you're welcome