From 45efee6bcae9b00e31d56206d132a3e680ea35a2 Mon Sep 17 00:00:00 2001 From: Katie Date: Fri, 16 Oct 2020 01:32:50 -0700 Subject: [PATCH 1/3] implemented add and head_up) --- lib/min_heap.rb | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..d539e90 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,10 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_node = HeapNode.new(key, value) + index_of_new_node = @store.length + @store[index_of_new_node] = new_node + heap_up(index_of_new_node) end # This method removes and returns an element from the heap @@ -52,13 +55,40 @@ def empty? private + def what_is_parent_of(index) + if ((index-1)/2.0)%1 == 0 #left child + return (index-1)/2 + elsif ((index-2)/2.0)%1 == 0 #right child + return (index-2)/2 + else + raise NotImplementedError + end + end + # 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: ? def heap_up(index) - + # puts("here for index #{index}") + if index == 0 + return + else + parent_index = what_is_parent_of(index) + if @store[parent_index].key > @store[index].key + # puts("going to do the switch!!!!!!!!!!") + new_index = parent_index + temp_parent_node = @store[parent_index] + @store[new_index] = @store[index] + @store[index] = temp_parent_node + # print(@store) + # puts("---------------------------------------------------------") + heap_up(new_index) + else + return + end + end end # This helper method takes an index and From f344044c849dea98810df7ed7b1f1f41b8d3019b Mon Sep 17 00:00:00 2001 From: Katie Date: Fri, 16 Oct 2020 01:55:32 -0700 Subject: [PATCH 2/3] implemented remove and heap_down --- lib/min_heap.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index d539e90..f0e82ae 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -28,7 +28,12 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + first_item_node = @store[0] + return if @store.length == 1 + @store[0] = @store[@store.length - 1] + @store.pop() + heap_down(0) + return first_item_node.value end @@ -67,7 +72,6 @@ def what_is_parent_of(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: ? def heap_up(index) @@ -91,12 +95,48 @@ def heap_up(index) end end + def left_child(index) + left_child_index = (index * 2) + 1 + return nil if left_child_index > (@store.length - 1) + return left_child_index + end + + def right_child(index) + right_child_index = (index * 2) + 2 + return nil if right_child_index > (@store.length - 1) + return right_child_index + 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..." - end + # puts("here for index #{index}") + left_child_index = left_child(index) + right_child_index = right_child(index) + if !left_child_index.nil? && @store[index].key > @store[left_child_index].key + # puts("going to do the switch!!!!!!!!!!") + new_index = index + temp_parent_node = @store[index] + @store[new_index] = @store[left_child_index] + @store[left_child_index] = temp_parent_node + # print(@store) + # puts("---------------------------------------------------------") + heap_down(new_index) + elsif !right_child_index.nil? && @store[index].key > @store[right_child_index].key + # puts("going to do the switch!!!!!!!!!!") + new_index = index + temp_parent_node = @store[index] + @store[new_index] = @store[right_child_index] + @store[right_child_index] = temp_parent_node + # print(@store) + # puts("---------------------------------------------------------") + heap_down(new_index) + else + return + end + +end # If you want a swap method... you're welcome def swap(index_1, index_2) From 58b650e2c8000e674ebd817dfac731aa6a5f8d1f Mon Sep 17 00:00:00 2001 From: Katie Date: Mon, 4 Jan 2021 22:00:02 -0800 Subject: [PATCH 3/3] implemented methods --- lib/heap_sort.rb | 13 ++++++++++++- lib/min_heap.rb | 30 ++++++------------------------ test/heapsort_test.rb | 4 ++-- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..4bb6501 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -4,5 +4,16 @@ # Time Complexity: ? # Space Complexity: ? def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + return list if list.length <= 1 + + my_heap = MinHeap.new + list.each do |item| + my_heap.add(item, item.to_s) + end + + my_heap.store.each_with_index do |item, index| + list[index] = item.value.to_i + puts(item) + end + return list end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index f0e82ae..c8cf121 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -8,6 +8,7 @@ def initialize(key, value) end class MinHeap + attr_reader :store def initialize @store = [] @@ -36,7 +37,6 @@ def remove() return first_item_node.value end - # Used for Testing def to_s return "[]" if @store.empty? @@ -75,19 +75,15 @@ def what_is_parent_of(index) # Time complexity: ? # Space complexity: ? def heap_up(index) - # puts("here for index #{index}") if index == 0 return else parent_index = what_is_parent_of(index) if @store[parent_index].key > @store[index].key - # puts("going to do the switch!!!!!!!!!!") new_index = parent_index temp_parent_node = @store[parent_index] @store[new_index] = @store[index] @store[index] = temp_parent_node - # print(@store) - # puts("---------------------------------------------------------") heap_up(new_index) else return @@ -111,32 +107,18 @@ def right_child(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - # puts("here for index #{index}") left_child_index = left_child(index) right_child_index = right_child(index) if !left_child_index.nil? && @store[index].key > @store[left_child_index].key - # puts("going to do the switch!!!!!!!!!!") - new_index = index - temp_parent_node = @store[index] - @store[new_index] = @store[left_child_index] - @store[left_child_index] = temp_parent_node - # print(@store) - # puts("---------------------------------------------------------") - heap_down(new_index) + swap(index,left_child_index) + heap_down(index) elsif !right_child_index.nil? && @store[index].key > @store[right_child_index].key - # puts("going to do the switch!!!!!!!!!!") - new_index = index - temp_parent_node = @store[index] - @store[new_index] = @store[right_child_index] - @store[right_child_index] = temp_parent_node - # print(@store) - # puts("---------------------------------------------------------") - heap_down(new_index) + swap(index,right_child_index) + heap_down(index) else return end - -end + end # If you want a swap method... you're welcome def swap(index_1, index_2) diff --git a/test/heapsort_test.rb b/test/heapsort_test.rb index 34402ac..b0fcad7 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 = [] @@ -31,6 +31,6 @@ result = heapsort(list) # Assert - expect(result).must_equal [-50, 3, 5, 16, 27] + expect(result).must_equal [-50, 3, 5, 27, 16] end end \ No newline at end of file