Skip to content

BST and Mergesort Code for Sept 14th #2

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

Open
wants to merge 2 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
65 changes: 65 additions & 0 deletions BST.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# pg 398 of Algo 4th Edition


class BST
def initialize
@root = nil
end

class Node
attr_reader :key
attr_accessor :value, :left, :right, :node_count

def initialize(key, value, node_count)
@key = key
@value = value
@left = nil
@right = nil
@node_count = node_count
end
end

def tree_size
size(@root)
end

def size(node)
return 0 unless node
node.node_count
end

def get(key)
get_helper(@root, key)
end

def put(key, value)
@root = put_helper(@root, key, value)
end

private

def get_helper(node, key)
return nil unless node
if key < node.key
get_helper(node.left, key)
elsif key > node.key
get_helper(node.right, key)
else
node.value
end
end

def put_helper(node, key, value)
return Node.new(key, value, 1) unless node
if key < node.key
node.left = put_helper(node.left, key, value)
elsif key > node.key
node.right = put_helper(node.right, key, value)
else
node.value = value
end
node.node_count = size(node.left) + size(node.right) + 1;
p "Hello"
return node
end
end
55 changes: 55 additions & 0 deletions MergeSort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# pg 271 of Algo 4th Edition

def merge!(arr, low, mid, high, aux_arr=[])
(0...arr.length).each do |i| # Copies the arr into the aux_arr
aux_arr[i] = arr[i]
end

i = low
j = mid + 1

(low..high).each do |k|
if (i > mid)
arr[k] = aux_arr[j]
j += 1 # Wish there was a clean way to do j++/i++
elsif (j > high)
arr[k] = aux_arr[i]
i += 1
elsif (aux_arr[j] < aux_arr[i])
arr[k] = aux_arr[j]
j += 1
else
arr[k] = aux_arr[i]
i += 1
end
end
# p aux_arr.object_id # Shows that no new aux_arrs are created
arr # Line needed for base case, won't work for <=2 element arrays without it
end

# pg 273 of Algo 4th Edition

def mergesort!(arr, low=0, high = arr.length-1, aux_arr=arr.dup) # Did not know about the arr.length as a param
# aux_arr = arr.dup # Don't need this line if I create the aux_arr in mergesort! params
# p aux_arr.object_id # Shows that no new aux_arrs are created

mid = low + (high - low)/2

return merge!(arr,low,mid,high, aux_arr) if low+1 >= high # Base case

mergesort!(arr, low, mid, aux_arr)
mergesort!(arr, mid+1, high, aux_arr)
merge!(arr,low, mid, high, aux_arr)
arr
end


1000.times do |x|
arr = []
rand(300).times do |y|
arr << rand
end
if (arr.sort != mergesort!(arr))
puts "Problem!"
end
end