From e9dc0972d84629ee1be9fe1e74532a9c2a74398e Mon Sep 17 00:00:00 2001 From: Jetmire Bajrami Date: Sat, 26 Sep 2020 09:43:33 -0700 Subject: [PATCH 1/2] added heap_up and add method --- lib/min_heap.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..0cb3507 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,10 +14,12 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(logn) + # Space Complexity: O(1) def add(key, value = key) - raise NotImplementedError, "Method not implemented yet..." + @store.push(HeapNode.new(key, value)) + new_node_index = @store.length - 1 + heap_up(new_node_index) end # This method removes and returns an element from the heap @@ -55,10 +57,20 @@ 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(1) def heap_up(index) - + parent_i = (index - 1) / 2 + return if parent_i < 0 + parent = @store[parent_i] + current = @store[index] + + while parent_i >= 0 && parent.key > current.key + swap(index, parent_i) + index = parent_i + parent_i = (index - 1) / 2 + parent = @store[parent_i] + end end # This helper method takes an index and From 101710bb20d0916fe70b245b6c055fda58f90f40 Mon Sep 17 00:00:00 2001 From: Jetmire Bajrami Date: Sun, 27 Sep 2020 20:10:00 -0700 Subject: [PATCH 2/2] added Heap_Down empty and remove method --- lib/min_heap.rb | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 0cb3507..c972022 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,7 +14,7 @@ def initialize end # This method adds a HeapNode instance to the heap - # Time Complexity: O(logn) + # Time Complexity: O(logn) # Space Complexity: O(1) def add(key, value = key) @store.push(HeapNode.new(key, value)) @@ -24,10 +24,14 @@ 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) + # Space Complexity: O(1) def remove() - raise NotImplementedError, "Method not implemented yet..." + return if empty? + swap(0, @store.length - 1) + removed_element = @store.pop + heap_down(0) + return removed_element.value end @@ -49,7 +53,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store[0].nil? end private @@ -57,7 +61,7 @@ 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: O(logn) + # Time complexity: O(logn) # Space complexity: O(1) def heap_up(index) parent_i = (index - 1) / 2 @@ -74,10 +78,33 @@ def 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. + # moves it up the heap if it's smaller + # than it's parent node. def heap_down(index) - raise NotImplementedError, "Method not implemented yet..." + while index < @store.length + left_i = index * 2 + 1 + right_i = index * 2 + 2 + child_index = nil + left = @store[left_i] + right = @store[right_i] + + if left && right + child_index = left.key < right.key ? left_i : right_i + if @store[child_index].key < @store[index].key + swap(index, child_index) + index = child_index + else + return + end + elsif left && !right + return unless left.key < @store[index].key + child_index = left_i + swap(index, child_index) + index = child_index + else + return + end + end end # If you want a swap method... you're welcome