From 746d70bc1a19a5d3c6605e0eb66d978abda73d62 Mon Sep 17 00:00:00 2001 From: Bitaman Date: Thu, 3 Oct 2019 12:57:13 -0700 Subject: [PATCH] min_heap and heap_sort done --- lib/heap_sort.rb | 19 +++++++++++++++++-- lib/min_heap.rb | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index c8a32a4..3582802 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -3,6 +3,21 @@ # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? -def heap_sort(list) - raise NotImplementedError, "Method not implemented yet..." +def heapsort(list) + return list if list.length == 0 || list.length == 1 + + #create new instance of MinHeap + min_heap = MinHeap.new + + #add all the elements from list + list.each do |element| + min_heap.add(element, value = element) + end + + #remove and add to result + result = [] + list.length.times do + result << min_heap.remove + end + return result end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..089fc74 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,12 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + new_node = HeapNode.new(key, value) + new_node_index = @store.length + @store.push(new_node) + if new_node_index > 1 + heap_up((@store.length - 1)) + end end # This method removes and returns an element from the heap @@ -25,7 +30,10 @@ def add(key, value = key) # Time Complexity: ? # Space Complexity: ? def remove() - raise NotImplementedError, "Method not implemented yet..." + swap(0, @store.length - 1) + answer = @store.pop + heap_down(0) + return answer.value end @@ -48,16 +56,27 @@ def to_s # Space complexity: ? def empty? raise NotImplementedError, "Method not implemented yet..." + return true if @store == [] end private + # def find_parent(index) + # parent_index = (index - 1)/2 + # return parent_index + # 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) + return if index == 0 + parent_index = (index - 1) / 2 + return if @store[parent_index].key < @store[index].key + swap(index, parent_index) + heap_up(parent_index) end @@ -65,7 +84,26 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + # raise NotImplementedError, "Method not implemented yet..." + left_child = index * 2 + 1 + right_child = index * 2 + 2 + if @store[left_child] == nil #at bottom of tree + return + end + if @store[right_child] == nil + if @store[index] > @store[left_child] + swap(index, left_child) + end + return + end + + if @store[left_child].key < @store[right_child].key + swap(left_child, index) + heap_down(left_child) + else + swap(right_child, index) + heap_down(right_child) + end end # If you want a swap method... you're welcome