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
2 changes: 1 addition & 1 deletion lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
def heapsort(list)

Choose a reason for hiding this comment

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

Why the rename?

raise NotImplementedError, "Method not implemented yet..."
end
68 changes: 55 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n)
# Space Complexity: O(log n)
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.

👍 Nice work with the time/space complexity

raise NotImplementedError, "Method not implemented yet..."
# raise NotImplementedError, "Method not implemented yet..."
heap_node = HeapNode.new(key, value)
@store << heap_node
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(log n)
# Space Complexity: O(log n)
def remove()
Comment on lines +29 to 31

Choose a reason for hiding this comment

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

👍 Nice work with the time/space complexity

raise NotImplementedError, "Method not implemented yet..."
# raise NotImplementedError, "Method not implemented yet..."
if @store.empty?
return nil
end

swap(0, @store.length - 1)

removed_node = @store.pop
heap_down(0) unless @store.empty?

return removed_node.value

end


Expand All @@ -44,28 +59,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?
Comment on lines +62 to 64

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..."
return @store.empty?
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(log n)
# Space complexity: O(log n)
def heap_up(index)
Comment on lines +74 to 76

Choose a reason for hiding this comment

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

👍


parent_node = (index - 1 ) / 2

if @store[parent_node].key > @store[index].key
swap(parent_node, index)

if parent_node > 0
return heap_up(parent_node)
end
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)

Choose a reason for hiding this comment

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

👍 Nice work with the time/space complexity

raise NotImplementedError, "Method not implemented yet..."
# raise NotImplementedError, "Method not implemented yet..."
left = index * 2 + 1
right = index * 2 + 2
min = left

if left >= @store.length
return
end

if right < @store.length && @store[right].key < @store[left].key
min = right
end

if @store[index].key < @store[min].key
return
end

swap(index, min)
heap_down(min)
end

# If you want a swap method... you're welcome
Expand Down