diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..c972022 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,24 @@ 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 # 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 @@ -47,7 +53,7 @@ def to_s # Time complexity: ? # Space complexity: ? def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store[0].nil? end private @@ -55,17 +61,50 @@ 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 - # 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