Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -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. |


20 changes: 17 additions & 3 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +4 to 6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
64 changes: 51 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +17 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are doing recursion the space complexity is O(log n) due to the call stack.

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()
Comment on lines +28 to 30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are doing recursion the space complexity is O(log n) due to the call stack.

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


Expand All @@ -44,28 +55,55 @@ 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

# 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
Comment on lines +95 to +97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the left child index is less than the length and the value at the left child index is less than the current parent!

Suggested change
elsif right_child_index >= @store.length
return
end
elsif right_child_index >= @store.length && @store[left_child_index].key >= @store[index].key
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
Expand Down