Skip to content
Open
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
65 changes: 52 additions & 13 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ def initialize
end

Choose a reason for hiding this comment

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

No heapsort method?


# 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.

👍 , nice work with a non-recursive heap_up method.

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()
Comment on lines +27 to 29

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..."
return if empty?
swap(0, @store.length - 1)
removed_element = @store.pop
heap_down(0)
return removed_element.value
end


Expand All @@ -47,25 +53,58 @@ def to_s
# Time complexity: ?
# Space complexity: ?
def empty?
raise NotImplementedError, "Method not implemented yet..."
return @store[0].nil?
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)
Comment on lines +64 to 66

Choose a reason for hiding this comment

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

👍


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)
Comment on lines +81 to 83

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..."
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

Choose a reason for hiding this comment

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

Nice work catching the scenario where the right is not in the array, but left is.

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
Expand Down