From 32bc6f7f950445a27cfb776e3818749c94d6423c Mon Sep 17 00:00:00 2001 From: Yieni Date: Mon, 28 Sep 2020 01:14:56 -0700 Subject: [PATCH] finished code --- lib/heap_sort.rb | 17 +++++++++-- lib/min_heap.rb | 68 ++++++++++++++++++++++++++++++++++++++----- test/heapsort_test.rb | 2 +- test/min_heap_test.rb | 1 - 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..1f798b9 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -4,5 +4,18 @@ # Time Complexity: ? # Space Complexity: ? def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." -end \ No newline at end of file + return list if list.empty? || list.length == 1 + + heap = MinHeap.new + + list.each do |num| + heap.add(num) + end + list = [] + until heap.empty? + removed = heap.remove + list << removed + end + return list +end + diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..c6e8f7a 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,10 +14,18 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(1)? + # Space Complexity: o(n)? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + node = HeapNode.new(key, value) + if @store.empty? + @store << node + else + @store << node + current_index = @store.length - 1 + @store = heap_up(current_index) + end + end # This method removes and returns an element from the heap @@ -25,10 +33,14 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + removed = @store[0].value + @store[0] = @store[@store.length - 1] + parent_node = @store[0] + @store.pop + @store = heap_down(0) + return removed end - # Used for Testing def to_s return "[]" if @store.empty? @@ -47,7 +59,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.empty? end private @@ -58,14 +70,56 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) + node = @store[index] + parent_index = (index - 1)/2 + parent_node = @store[parent_index] + return @store if parent_node.key <= node.key || index == 0 + + @store[index] = parent_node + @store[parent_index] = node + index = parent_index + return heap_up(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..." + parent_node = @store[index] + left_child_index = index * 2 + 1 + right_child_index = index * 2 + 2 + left_child = @store[left_child_index] + right_child = @store[right_child_index] + if left_child.nil? == false && right_child.nil? == false + return @store if (parent_node.key <= left_child.key && parent_node.key <= right_child.key) + + if left_child.key < right_child.key + @store[index] = left_child + @store[left_child_index] = parent_node + index = left_child_index + return heap_down(index) + else + @store[index] = right_child + @store[right_child_index] = parent_node + index = right_child_index + return heap_down(index) + end + elsif left_child.nil? == true && right_child.nil? == true + return @store + elsif left_child.nil? == true && right_child.nil? == false + return @store if parent_node.key <= right_child.key + @store[index] = right_child + @store[right_child_index] = parent_node + index = right_child_index + return heap_down(index) + else left_child.nil? == false && right_child.nil? == true + return @store if parent_node.key <= left_child.key + @store[index] = left_child + @store[left_child_index] = parent_node + index = left_child_index + return heap_down(index) + end end # If you want a swap method... you're welcome 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..476a51e 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -47,7 +47,6 @@ output = heap.to_s # Assert - expect(output).must_equal "[Donuts, Pizza, Pasta, Soup, Cookies, Cake]" end