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
19 changes: 17 additions & 2 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
Comment on lines 4 to 5

Choose a reason for hiding this comment

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

No time or space complexity?

def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
def heapsort(list)
return list if list.length == 0 || list.length == 1

#create new instance of MinHeap
min_heap = MinHeap.new

#add all the elements from list
list.each do |element|
min_heap.add(element, value = element)
end

#remove and add to result
result = []
list.length.times do
result << min_heap.remove
end
return result
end
44 changes: 41 additions & 3 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ def initialize
# Time Complexity: ?
# Space Complexity: ?
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
new_node_index = @store.length

Choose a reason for hiding this comment

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

I don't think you really need this.

@store.push(new_node)
if new_node_index > 1
heap_up((@store.length - 1))
end
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..."
swap(0, @store.length - 1)
answer = @store.pop
heap_down(0)
return answer.value
end


Expand All @@ -48,24 +56,54 @@ def to_s
# Space complexity: ?
def empty?
raise NotImplementedError, "Method not implemented yet..."
return true if @store == []
end

private

# def find_parent(index)
# parent_index = (index - 1)/2
# return parent_index
# end

# 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)
return if index == 0
parent_index = (index - 1) / 2
return if @store[parent_index].key < @store[index].key
swap(index, parent_index)
heap_up(parent_index)

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..."
# raise NotImplementedError, "Method not implemented yet..."
left_child = index * 2 + 1
right_child = index * 2 + 2
if @store[left_child] == nil #at bottom of tree
return
end
if @store[right_child] == nil
if @store[index] > @store[left_child]

Choose a reason for hiding this comment

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

You should be comparing the keys here.

Suggested change
if @store[index] > @store[left_child]
if @store[index].key > @store[left_child].key

swap(index, left_child)
end
return
end

if @store[left_child].key < @store[right_child].key
swap(left_child, index)
heap_down(left_child)
else
swap(right_child, index)
heap_down(right_child)
end
end

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