Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaves_Ga-Young #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
20 changes: 16 additions & 4 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

require 'min_heap'

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n log n)
# Space Complexity: O(n)
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
heap = MinHeap.new

list.each do |num| # T: O(n)
heap.add(num, num) # S: O(n)
end

counter = 0
until counter > list.length - 1
list[counter] = heap.remove # T: O(log n)
counter += 1
end

return list
end
97 changes: 68 additions & 29 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
@@ -1,73 +1,112 @@
class HeapNode
attr_reader :key, :value

def initialize(key, value)
@key = key
@value = value
end
end

class MinHeap

def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
# Time Complexity: O(log n) where n is number of nodes
# Space Complexity: O(log n)
def add(key, value)
@store << HeapNode.new(key, value)

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) where n is number of nodes
# Space Complexity: O(log n)
def remove()
raise NotImplementedError, "Method not implemented yet..."
# binding.pry
return ArgumentError if @store == nil

to_remove = @store[0]
swap(0, (@store.length - 1))
@store.pop

heap_down(0)

return to_remove.value
end


# Used for Testing
def to_s
return "[]" if @store.empty?

output = "["
(@store.length - 1).times do |index|
output += @store[index].value + ", "
end

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

return output
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 +57 to 59

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 @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) where n is number of nodes
# Space complexity: O(1)
def heap_up(index)

if index != 0
parent = ((index - 1) / 2)
if @store[index].key < @store[parent].key
swap(index, parent)
heap_up(parent)
end
end
return
end

# This helper method takes an index and
# moves it up the heap if it's smaller
# moves it down the heap if it's smaller
# than it's parent node.
def heap_down(index)
Comment on lines -65 to 84

Choose a reason for hiding this comment

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

Ok this one has a small bug that heapsort caught. Take a look at it, you can get into the circumstance that the left child is not nil and smaller than the parent and the right child is nil.

raise NotImplementedError, "Method not implemented yet..."
left_child = (index * 2 + 1)
right_child = (index * 2 + 2)

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

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

return
end

# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
Expand Down
33 changes: 22 additions & 11 deletions test/heapsort_test.rb → test/heap_sort_test.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heapsort" do
it "sorts an empty array" do
# Arrange
list = []

# Act
result = heapsort(list)

result = heap_sort(list)
# Assert
expect(result).must_equal []
end

it "can sort a 1-element array" do
# Arrange
list = [5]

# Act
result = heapsort(list)

result = heap_sort(list)
# Assert
expect(result).must_equal [5]
end

it "can sort a 5-element array" do
# Arrange
list = [5, 27, 3, 16, -50]

# Act
result = heapsort(list)

result = heap_sort(list)
# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
end

it "can sort a different 5-element array" do
# Arrange
list = [-4, 49, 9, 31, 47]

# Act
result = heap_sort(list)

# Assert
expect(result).must_equal [-4, 9, 31, 47, 49]
end
end
74 changes: 37 additions & 37 deletions test/min_heap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
# Assert
expect(heap).must_be_instance_of MinHeap
end

it "can have nodes added" do
# Arrange
key = 5
value = "Pasta"

# Assert
expect(heap).must_respond_to :add

# Act
heap.add(key, value)
end

it "adds nodes in a proper order" do
# Arrange
heap.add(3, "Pasta")
heap.add(6, "Soup")
heap.add(1, "Pizza")

# Act
output = heap.to_s

# Assert

expect(output).must_equal "[Pizza, Soup, Pasta]"
end

it "adds nodes in a proper order with a lot of nodes" do
# Arrange
heap.add(3, "Pasta")
Expand All @@ -42,40 +42,40 @@
heap.add(0, "Donuts")
heap.add(16, "Cookies")
heap.add(57, "Cake")

# Act
output = heap.to_s

# Assert

expect(output).must_equal "[Donuts, Pizza, Pasta, Soup, Cookies, Cake]"
end

it "can remove nodes in the proper order" do
# Arrange
heap.add(3, "Pasta")
heap.add(6, "Soup")
heap.add(1, "Pizza")
heap.add(0, "Donuts")
heap.add(16, "Cookies")
heap.add(57, "Cake")

# Act
removed = heap.remove

# Assert
expect(removed).must_equal "Donuts"

# Another Act
removed = heap.remove

# Another assert
expect(removed).must_equal "Pizza"

# Another Act
removed = heap.remove

# Another assert
expect(removed).must_equal "Pasta"
# Arrange
heap.add(3, "Pasta")
heap.add(6, "Soup")
heap.add(1, "Pizza")
heap.add(0, "Donuts")
heap.add(16, "Cookies")
heap.add(57, "Cake")
# Act
removed = heap.remove
# Assert
expect(removed).must_equal "Donuts"
# Another Act
removed = heap.remove
# Another assert
expect(removed).must_equal "Pizza"
# Another Act
removed = heap.remove
# Another assert
expect(removed).must_equal "Pasta"
end
end