-
Notifications
You must be signed in to change notification settings - Fork 40
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
Janice H. #20
base: master
Are you sure you want to change the base?
Janice H. #20
Changes from 5 commits
7c48840
16637fc
2483566
5d627da
5095f40
b09e26e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,68 @@ | ||
|
||
|
||
# This method uses a heap to sort an array. | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def heap_sort(list) | ||
raise NotImplementedError, "Method not implemented yet..." | ||
# Time Complexity: O(2nlogn)? | ||
# Space Complexity: O(1) | ||
# GAH I cannot get those last two numbers | ||
# sorted properly for some reason and it's | ||
# time to turn this in. I know they are in | ||
# the correct order at the end but then flip | ||
def heapsort(list) | ||
return [] if list == [] | ||
return list if list.length == 1 | ||
index = 1 | ||
until index == list.length | ||
heap_up(list, index) | ||
index += 1 | ||
end | ||
find_min(list) | ||
return list | ||
end | ||
|
||
def heap_up(list, index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To do this in-place, I would suggest making a Max_heap instead of a min-heap and then as you remove a node adding it to the rear of the array working your way from the last node until index 0. That's a lot easier than using a min-heap like you are doing. |
||
# for right children | ||
if index % 2 == 0 | ||
parent = (index - 2) / 2 | ||
unless parent < 0 | ||
if list[index] < list[parent] | ||
swap(list, index, parent) | ||
print list | ||
heap_up(list, parent) | ||
end | ||
end | ||
end | ||
# for left children | ||
if index % 2 == 1 | ||
parent = (index - 1) / 2 | ||
unless parent < 0 | ||
if list[index] < list[parent] | ||
swap(list, index, parent) | ||
print list | ||
heap_up(list, parent) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def swap(list, index_1, index_2) | ||
temp = list[index_1] | ||
list[index_1] = list[index_2] | ||
list[index_2] = temp | ||
end | ||
|
||
def find_min(list) | ||
return list[0..1] if list.length == 2 | ||
heap_down(list, 1) | ||
print list | ||
return find_min(list[1...list.length]) | ||
end | ||
|
||
def heap_down(list, index) | ||
if !list[index+1].nil? && list[index] > list[index+1] | ||
swap(list, index, index+1) | ||
heap_down(list, index+1) | ||
elsif !list[index+2].nil? && list[index] > list[index+2] | ||
swap(list, index, index+2) | ||
heap_down(list, index+2) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,110 @@ | ||
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: ? | ||
# Time Complexity: O(nlogn) | ||
# Space Complexity: O(n) | ||
def add(key, value = key) | ||
Comment on lines
+17
to
19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but why would adding one value to the heap be O(n log n) time complexity and why would it take O(n) space complexity? I suggest it's O(log n) for space (the recursive stack) and O(log n) for time. |
||
raise NotImplementedError, "Method not implemented yet..." | ||
new_node = HeapNode.new(key, value) | ||
@store << new_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(nlogn) | ||
# Space Complexity: O(1) | ||
def remove() | ||
Comment on lines
+27
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my notes above about space/time complexity. |
||
raise NotImplementedError, "Method not implemented yet..." | ||
if @store.empty? | ||
return nil | ||
end | ||
swap(0, @store.length-1) | ||
min = @store.pop | ||
heap_down(0) unless @store.empty? | ||
return min.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? | ||
raise NotImplementedError, "Method not implemented yet..." | ||
if @store == [] | ||
return true | ||
else | ||
return false | ||
end | ||
jaitch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(nlogn) | ||
# Space complexity: O(1) | ||
def heap_up(index) | ||
Comment on lines
+66
to
68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is the time complexity to heap up 1 element O(n log n), you don't, or shouldn't look at every node in the list for this method. |
||
|
||
# for right children | ||
if index % 2 == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These if statements could be condensed a bit. |
||
parent = (index - 2) / 2 | ||
unless parent < 0 | ||
if @store[index].key < @store[parent].key | ||
swap(index, parent) | ||
heap_up(parent) | ||
end | ||
end | ||
end | ||
# for left children | ||
if index % 2 == 1 | ||
parent = (index - 1) / 2 | ||
unless parent < 0 | ||
if @store[index].key < @store[parent].key | ||
swap(index, parent) | ||
heap_up(parent) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# This helper method takes an index and | ||
# 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..." | ||
if !@store[index+1].nil? && @store[index].key > @store[index+1].key | ||
swap(index, index+1) | ||
heap_down(index+1) | ||
elsif !@store[index+2].nil? && @store[index].key > @store[index+2].key | ||
swap(index, index+2) | ||
heap_down(index+2) | ||
end | ||
end | ||
Comment on lines
94
to
102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is NOT doing head_down. Instead it's comparing a node to the nodes next to it. Remember the formula to find a node's left child is:
Because you're not doing the correct heap_down method this method is just using a sorted array which is O(n) |
||
|
||
# If you want a swap method... you're welcome | ||
def swap(index_1, index_2) | ||
temp = @store[index_1] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's neat that you were trying to do the in-place heapsort. I'd suggest doing it the simple way first, using the minheap you created in this assignment and adding all the elements of the list to the heap and then extracting them one by one and shoveling them into the resulting list. It's O(n) space complexity but it works. Then try for this one if/when you have time.