Skip to content
Open

KS #9

Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.vscode/settings.json
18 changes: 13 additions & 5 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Time Complexity: O(nlog(n))
# Space Complexity: Constant.
def heapsort(vault)
hoard = MinHeap.new

hoard.each do |treasure|
vault.add(treasure, treasure)
end

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
the_precious = []
until hoard.empty?
the_precious << treasure.remove
end

return the_precious
end
74 changes: 46 additions & 28 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: ? O(log n)
# Space Complexity: ? O(nm)
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
node = HeapNode.new(key, value)
@store << 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: Already have all the space we need.
def remove()
raise NotImplementedError, "Method not implemented yet..."
return nil if !@store

swap(0, @store.length - 1)
nope = @store.pop

heap_down(0)
return nope.value
end


Expand All @@ -39,39 +44,52 @@ def to_s
end

output += @store.last.value + "]"

return output
end

# This method returns true if the heap is empty
# Time complexity: ?
# Space complexity: ?
# Time complexity: Exceedingly fast.
# Space complexity: Literally one bit.
def empty?
raise NotImplementedError, "Method not implemented yet..."
!@store
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: ?
def heap_up(index)

# Time complexity: O(log n)
# Space complexity: O(nm)
def heap_up(i)
return if i == 0

pnode = (i-1) / 2
if @store[i].key < @store[pnode].key
swap(i, pnode)
heap_up(pnode)
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..."
def heap_down(i)
li = i*2+1
ri = i*2+2

if ri < @store.length

min = @store[li].key < @store[ri].key ? li : ri

if @store[i].key > @store[min].key
swap(i, min)
heap_down(min)
end
elsif li < @store.length
if @store[i].key > @store[li].key
swap(i, li)
end
end
end

# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
@store[index_1] = @store[index_2]
@store[index_2] = temp
end
end
end