diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index 37dfdde..7c2785e 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -6,9 +6,9 @@ Congratulations! You're submitting your assignment! Question | Answer :------------- | :------------- -How is a Heap different from a Binary Search Tree? | -Could you build a heap with linked nodes? | -Why is adding a node to a heap an O(log n) operation? | -Were the `heap_up` & `heap_down` methods useful? Why? | +How is a Heap different from a Binary Search Tree? | Heaps guarantee that the elements on higher levels are greater for max heaps or lower for min heaps. But, the BST guarantees that there is order from left to right, this is useful for searching elements that are sorted. | +Could you build a heap with linked nodes? | No because heaps are binary trees. You can store the heap into an array since it's easier to manage memory. | +Why is adding a node to a heap an O(log n) operation? | Because the smallest possible height for a heap with N nodes is o log n. | +Were the `heap_up` & `heap_down` methods useful? Why? | Heap up methods are useful in adding elements to a heap, by comparing the new node to its parent and swapping them if they are out of order, and updating a lcoation. The heap down is useful to remove an element by swapping the last leaf with the root and done until there are more swaps left. | diff --git a/lib/heap_sort.rb b/lib/heap_sort.rb index 6b692e4..9a3e9e4 100644 --- a/lib/heap_sort.rb +++ b/lib/heap_sort.rb @@ -1,8 +1,22 @@ # This method uses a heap to sort an array. -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: ? n log(n) +# Space Complexity: ? O(n) def heapsort(list) - raise NotImplementedError, "Method not implemented yet..." + # raise NotImplementedError, "Method not implemented yet..." + #initialize + heap_sort = MinHeap.new() + #loop through list + list.each do |number| + # add values into new heap + heap_sort.add(number, number) + end + #create a new array that will be sorted + sorted_array = Array.new + #until the heap is empty + until heap_sort.empty? + #push values into sorted array while removing from heap + sorted_array << heap_sort.remove + end end \ No newline at end of file diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..7c08842 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,29 @@ 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..." + #pushing key value of node into the store array + @store << HeapNode.new(key, value) + # add + heap_up(@store.length - 1) 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..." + #set root to first element of the store array + root = @store[0] + #swpap + swap(0, @store.length - 1) + # take it out + @store.pop() + #eventually remove + heap_down(0) + return root.value end @@ -44,10 +55,10 @@ def to_s end # This method returns true if the heap is empty - # Time complexity: ? - # Space complexity: ? + # Time complexity: O(1) + # Space complexity: O(1) def empty? - raise NotImplementedError, "Method not implemented yet..." + return @store.length == 0 end private @@ -55,17 +66,44 @@ 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) - + if index == 0 + return + end + parent_index = ((index -1 )/2).floor() + if @store[index].key < @store[parent_index].key + swap(index, parent_index) + heap_up(parent_index) + end 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..." + if index >= @store.length + return + end + + left_child_index = (2 * index + 1) + right_child_index = (2 * index + 2) + + if left_child_index >= @store.length + return + elsif right_child_index >= @store.length + return + end + + if @store[index].key > @store[left_child_index].key + swap(index, left_child_index) + heap_down(left_child_index) + end + if @store[index].key > @store[right_child_index].key + swap(index, right_child_index) + heap_down(right_child_index) + end end # If you want a swap method... you're welcome