Skip to content
Open

Niv #10

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: 0 additions & 2 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
Expand Down
49 changes: 43 additions & 6 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HeapNode
attr_reader :key, :value

# compare by key, priority field
def initialize(key, value)
@key = key
@value = value
Expand All @@ -17,16 +17,25 @@ def initialize
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 17 to 18

Choose a reason for hiding this comment

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

Since you're doing recursion it should be O(log n) for both.

def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
new_node_index = @store.length
@store.push(new_node)
heap_up(@store.length - 1)

return new_node
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
def remove()
raise NotImplementedError, "Method not implemented yet..."
end
removed = @store[0].value
swap(0, @store.length - 1)
@store.pop
heap_down(0)
return removed
end


# Used for Testing
Expand All @@ -52,20 +61,48 @@ def empty?

private

def find_parent(index)
parent_index = (index - 1)/2
return parent_index
end
Comment on lines +64 to +67

Choose a reason for hiding this comment

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

I like this helper method.


# 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: ?
def heap_up(index)

puts to_s
parent_index = find_parent(index)
if @store[parent_index].key <= @store[index].key || index == 0
return
end
if @store[parent_index].key > @store[index].key

Choose a reason for hiding this comment

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

Since your prior if statement returns you don't need an if statement here.

# puts @store[index].value
swap(parent_index, index)
index = parent_index
heap_up(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..."
puts 'heap down'
puts to_s
child_index = 2*index + 1

Choose a reason for hiding this comment

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

You are only finding the left child here. You are not finding the right child.

if index >= @store.length - 1 || child_index >= @store.length - 1
return
end
if @store[child_index].key >= @store[index].key
return
end
if @store[child_index].key < @store[index].key
swap(child_index, index)
index = child_index
heap_down(index)
end
end

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