From 9b843cc4159616db2ab6e49b235f47e5e26ca3d8 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Sun, 27 Sep 2020 21:02:15 -0700 Subject: [PATCH 1/5] implement add and heap_up methods --- lib/min_heap.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..c4c3449 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -13,11 +13,18 @@ def initialize @store = [] end + # helper method to find parent + def find_parent(index) + parent_index = (index - 1) / 2 + return parent_index + 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..." + @store << HeapNode.new(key, value) + heap_up(@store.length-1) end # This method removes and returns an element from the heap @@ -58,7 +65,15 @@ def empty? # Time complexity: ? # Space complexity: ? def heap_up(index) + return if index == 0 + parent = find_parent(index) + if @store[parent].key > @store[index].key + temp = @store[parent] + @store[parent] = @store[index] + @store[index] = temp + heap_up(parent) + end end # This helper method takes an index and From a02d29dcfb57c00d6b7bae7234bc5ec2a53fb660 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Mon, 28 Sep 2020 18:24:52 -0700 Subject: [PATCH 2/5] add remove method and update tests --- lib/min_heap.rb | 54 ++++++++++++++++++++++++++++++++++++++----- test/min_heap_test.rb | 6 ++--- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index c4c3449..0b7404d 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -15,8 +15,15 @@ def initialize # helper method to find parent def find_parent(index) - parent_index = (index - 1) / 2 - return parent_index + return (index - 1) / 2 + end + + def left_child(index) + return 2 * index + 1 + end + + def right_child(index) + return 2 * index + 2 end # This method adds a HeapNode instance to the heap @@ -31,11 +38,18 @@ def add(key, value = key) # maintaining the heap structure # Time Complexity: ? # Space Complexity: ? - def remove() - raise NotImplementedError, "Method not implemented yet..." + def remove + if @store.empty? + return nil + end + + swap(0, @store.length - 1) + result = @store.pop + + heap_down(0) unless @store.empty? + return result end - # Used for Testing def to_s return "[]" if @store.empty? @@ -80,7 +94,35 @@ 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..." + # compare the new root with its children and swap to maintain the heap order in an operation called heap-down. The heap down operation is repeated until a leaf node is reached or no swaps are made. + return if index >= @store.length-1 + + left = left_child(index) + right = right_child(index) + smallest = nil + + if !@store[left] && !@store[right] + return + elsif !@store[left] + smallest = right + elsif !@store[right] + smallest = left + else + smallest = (@store[left].key < @store[right].key) ? left : right + end + + if @store[index].key > @store[smallest].key + swap(index, smallest) + heap_down(smallest) + end + + # if (right <= @store.length-1) &&@store[index].key > @store[right].key + # swap(index, right) + # heap_down(right) + # end + + # heap_down(left) + # heap_down(right) end # If you want a swap method... you're welcome diff --git a/test/min_heap_test.rb b/test/min_heap_test.rb index 186d4c2..fa1e9a1 100644 --- a/test/min_heap_test.rb +++ b/test/min_heap_test.rb @@ -61,19 +61,19 @@ heap.add(57, "Cake") # Act - removed = heap.remove + removed = heap.remove.value # Assert expect(removed).must_equal "Donuts" # Another Act - removed = heap.remove + removed = heap.remove.value # Another assert expect(removed).must_equal "Pizza" # Another Act - removed = heap.remove + removed = heap.remove.value # Another assert expect(removed).must_equal "Pasta" From b46b13e93598bdef3bc6b4bbb5da89aa01241c4f Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Mon, 28 Sep 2020 18:45:09 -0700 Subject: [PATCH 3/5] clean up code and add big O notations --- lib/min_heap.rb | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 0b7404d..daae38e 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -18,17 +18,19 @@ def find_parent(index) return (index - 1) / 2 end + # helper method to find left child def left_child(index) return 2 * index + 1 end + # helper method to find right child def right_child(index) return 2 * index + 2 end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) depending on the height of the heap + # Space Complexity: O(logn) to hold the recursive stack def add(key, value = key) @store << HeapNode.new(key, value) heap_up(@store.length-1) @@ -36,8 +38,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(logn) depending on the height of the heap + # Space Complexity: O(logn) to hold the recursive stack def remove if @store.empty? return nil @@ -76,16 +78,14 @@ 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(logn) + # Space complexity: O(logn) def heap_up(index) return if index == 0 parent = find_parent(index) if @store[parent].key > @store[index].key - temp = @store[parent] - @store[parent] = @store[index] - @store[index] = temp + swap(parent, index) heap_up(parent) end end @@ -94,16 +94,15 @@ def heap_up(index) # moves it up the heap if it's smaller # than it's parent node. def heap_down(index) - # compare the new root with its children and swap to maintain the heap order in an operation called heap-down. The heap down operation is repeated until a leaf node is reached or no swaps are made. return if index >= @store.length-1 left = left_child(index) right = right_child(index) - smallest = nil - if !@store[left] && !@store[right] - return - elsif !@store[left] + return if !@store[left] && !@store[right] + + smallest = nil + if !@store[left] smallest = right elsif !@store[right] smallest = left @@ -115,14 +114,6 @@ def heap_down(index) swap(index, smallest) heap_down(smallest) end - - # if (right <= @store.length-1) &&@store[index].key > @store[right].key - # swap(index, right) - # heap_down(right) - # end - - # heap_down(left) - # heap_down(right) end # If you want a swap method... you're welcome From c93c2974c1d6d6770fea06f9790f83f8425cd86b Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Mon, 28 Sep 2020 18:59:31 -0700 Subject: [PATCH 4/5] implement heap sort --- lib/heap_sort.rb | 19 +++++++++++++++++-- test/heapsort_test.rb | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..3281d93 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,23 @@ - +require_relative "min_heap" # This method uses a heap to sort an array. # Time Complexity: ? # Space Complexity: ? +# With an array of n elements, to perform Heapsort you can add the elements to the heap, an O(nlog n) operation. Then you can remove the elements from the heap one-by-one placing them in the proper place in an array, also an O(nlog n) operation. + def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + return list if list.length <= 1 + + heap = MinHeap.new + + list.each do |i| + heap.add(i) + end + + ordered_list = [] + list.length.times do |i| + ordered_list << heap.remove.value + end + + return ordered_list end \ No newline at end of file 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 = [] From d0700a486001ed696d6dbe027eb0880ff04bdcb8 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Mon, 28 Sep 2020 19:03:36 -0700 Subject: [PATCH 5/5] remove comment and add Big O notation --- lib/heap_sort.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 3281d93..82f9812 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,10 +1,8 @@ require_relative "min_heap" # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? -# With an array of n elements, to perform Heapsort you can add the elements to the heap, an O(nlog n) operation. Then you can remove the elements from the heap one-by-one placing them in the proper place in an array, also an O(nlog n) operation. - +# Time Complexity: O(nlogn) - loops n times based on length of list, and then logn adding or removing +# Space Complexity: O(nlogn) def heapsort(list) return list if list.length <= 1