From 1b007ef3e4b558916dd21bdac7c2256c76cb7a5b Mon Sep 17 00:00:00 2001 From: "kate.m" Date: Fri, 2 Oct 2020 13:17:17 -0700 Subject: [PATCH 1/4] heap sort --- lib/heap_sort.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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 From 8436bc96cdba740a6707d808981c7a7054c84abd Mon Sep 17 00:00:00 2001 From: "kate.m" Date: Sat, 3 Oct 2020 20:57:27 -0700 Subject: [PATCH 2/4] min heap --- lib/min_heap.rb | 58 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index 6eaa630..dfc23a3 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -14,18 +14,23 @@ 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 << HeapNode.new(key, value) + 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..." + root = @store[0] + swap(0, @store.length - 1) + @store.pop() + heap_down(0) + return root.value end @@ -44,10 +49,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 +60,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 From 840dbea1c3eaccd177310c52202b153ab90c3a03 Mon Sep 17 00:00:00 2001 From: "kate.m" Date: Sun, 4 Oct 2020 21:29:37 -0700 Subject: [PATCH 3/4] added notes --- lib/min_heap.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/min_heap.rb b/lib/min_heap.rb index dfc23a3..7c08842 100644 --- a/lib/min_heap.rb +++ b/lib/min_heap.rb @@ -17,7 +17,9 @@ def initialize # Time Complexity: ? O(logn) # Space Complexity: ? O(1) def add(key, value = key) + #pushing key value of node into the store array @store << HeapNode.new(key, value) + # add heap_up(@store.length - 1) end @@ -26,9 +28,13 @@ def add(key, value = key) # Time Complexity: O(logn) # Space Complexity: O(1) def remove() + #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 From 2ad71cd189f359488ce26b121b6927cf63839a46 Mon Sep 17 00:00:00 2001 From: "kate.m" Date: Sun, 4 Oct 2020 21:45:12 -0700 Subject: [PATCH 4/4] pr questions --- .github/PULL_REQUEST_TEMPLATE | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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. |