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

finished binary search tree, min binary heap, specs all passing, and … #150

Open
wants to merge 1 commit 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
16 changes: 16 additions & 0 deletions 01-data-structures/05-hashes-part-2/separate_chaining/instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_relative 'separate_chaining'

def sep_chain
#creates new hash with size 6
star_wars_movies = SeparateChaining.new(6)

#adding entries into new hash
star_wars_movies["Star Wars: The Phantom Menace"] = "Number One"
star_wars_movies["Star Wars: Attack of the Clones"] = "Number Two"
star_wars_movies["Star Wars: Revenge of the Sith"] = "Number Three"
star_wars_movies["Star Wars: A New Hope"] = "Number Four"
star_wars_movies["Star Wars: The Empire Strikes Back"] = "Number Five"
star_wars_movies["Star Wars: Return of the Jedi"] = "Number Six"

puts star_wars_movies.status
end
47 changes: 47 additions & 0 deletions 01-data-structures/06-trees/binary_tree/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'benchmark'
require_relative 'binary_search_tree'
require_relative 'min_binary_heap'

tree_root = Node.new("tree root", 42)
heap_root = Node.new("heap root", 42)
tree = BinarySearchTree.new(tree_root)
heap = MinBinaryHeap.new(heap_root)

n = 100000

Benchmark.bm(100) do |x|
x.report("tree insert") do
for i in 1..n do
new_node = Node.new("tree", Random.rand(100000))
tree.insert(tree_root, new_node)
end
end
x.report("heap insert") do
for i in 1..n do
new_node = Node.new("heap", Random.rand(100000))
heap.insert(heap_root, new_node)
end
end
end

target = Node.new("target", 50000)
tree.insert(tree_root, target)
heap.insert(heap_root, target)

Benchmark.bm(100) do |x|
x.report("tree find") do
tree.find(tree_root, 50000)
end
x.report("heap find") do
heap.find(heap_root, 50000)
end
end

Benchmark.bm(100) do |x|
x.report("tree delete") do
tree.delete(tree_root, 50000)
end
x.report("heap delete") do
heap.delete(heap_root, 50000)
end
end
53 changes: 50 additions & 3 deletions 01-data-structures/06-trees/binary_tree/binary_search_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,66 @@
class BinarySearchTree

def initialize(root)
@root = root
end

def insert(root, node)
if node.rating < root.rating && root.left == nil
root.left = node
elsif node.rating < root.rating && root.left != nil
root = root.left
insert(root, node)
elsif node.rating > root.rating && root.right == nil
root.right = node
elsif node.rating > root.rating && root.right != nil
root = root.right
insert(root, node)
end
end

# Recursive Depth First Search
def find(root, data)
if root.nil? || data.nil?
return nil
else
if root.title == data
return root
elsif root.left != nil
find(root.left, data)
elsif root.right != nil
find(root.right, data)
end
end
end

def delete(root, data)
if root.nil? || data.nil?
return nil
else
target_node = find(root, data)
target_node.nil? ? nil : (target_node.title = nil && target_node.rating = nil)
end
end

# Recursive Breadth First Search
def printf(children=nil)
def printf
queue = []
output = [@root]
if @root.left != nil
queue.push(@root.left)
end
if @root.right != nil
queue.push(@root.right)
end
queue.each do |q|
output.push(q)
if q.left != nil
queue.push(q.left)
end
if q.right != nil
queue.push(q.right)
end
end
output.each do |o|
puts "#{o.title}: #{o.rating}"
end
end
end
74 changes: 74 additions & 0 deletions 01-data-structures/06-trees/binary_tree/min_binary_heap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require_relative 'node'

class MinBinaryHeap
attr_reader :root

def initialize(root)
@root = root
end

def insert(root, node)
if root.rating > node.rating
temp = root
@root = node
node = temp
insert(@root, node)
else
if root.left.nil?
root.left = node
elsif root.right.nil? && root.left != nil
root.right = node
elsif root.left != nil && root.right != nil && root.left.left != nil && root.left.right != nil
insert(root.right, node)
elsif root.left != nil && root.right != nil
insert(root.left, node)
end
end
end

def find(root, data)
if root.nil? || data.nil?
return nil
else
if root.title == data
return root
elsif root.left != nil
find(root.left, data)
elsif root.right != nil
find(root.right, data)
end
end
end

def delete(root, data)
if root.nil? || data.nil?
return nil
else
target_node = find(root, data)
target_node.nil? ? nil : (target_node.title = nil && target_node.rating = nil)
end
end

def printf
queue = []
output = [@root]
if @root.left != nil
queue.push(@root.left)
end
if @root.right != nil
queue.push(@root.right)
end
queue.each do |q|
output.push(q)
if q.left != nil
queue.push(q.left)
end
if q.right != nil
queue.push(q.right)
end
end
output.each do |o|
puts "#{o.title}: #{o.rating}"
end
end
end
81 changes: 81 additions & 0 deletions 01-data-structures/06-trees/binary_tree/min_binary_heap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
include RSpec

require_relative 'min_binary_heap'

RSpec.describe MinBinaryHeap, type: Class do
let (:root) { Node.new("The Matrix", 87) }

let (:tree) { MinBinaryHeap.new(root) }
let (:pacific_rim) { Node.new("Pacific Rim", 72) }
let (:braveheart) { Node.new("Braveheart", 78) }
let (:jedi) { Node.new("Star Wars: Return of the Jedi", 80) }
let (:donnie) { Node.new("Donnie Darko", 85) }
let (:inception) { Node.new("Inception", 86) }
let (:district) { Node.new("District 9", 90) }
let (:shawshank) { Node.new("The Shawshank Redemption", 91) }
let (:martian) { Node.new("The Martian", 92) }
let (:hope) { Node.new("Star Wars: A New Hope", 93) }
let (:empire) { Node.new("Star Wars: The Empire Strikes Back", 94) }
let (:mad_max_2) { Node.new("Mad Max 2: The Road Warrior", 98) }

describe "#insert(data)" do
it "properly inserts a new node and it swaps with the root" do
tree.insert(root, pacific_rim)
expect(tree.root.title).to eq "Pacific Rim"
end

it "properly inserts a new node and it swaps with the root" do
tree.insert(root, pacific_rim)
expect(tree.root.left.title).to eq "The Matrix"
end

it "properly inserts a new node as a left-left child" do
tree.insert(tree.root, braveheart)
tree.insert(tree.root, pacific_rim)
expect(tree.root.left.left.title).to eq "The Matrix"
end

it "properly inserts a new node as a left-right child" do
tree.insert(tree.root, donnie)
tree.insert(tree.root, inception)
expect(tree.root.right.title).to eq "Inception"
end

it "properly inserts a new node as a right child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
expect(tree.root.right.title).to eq "District 9"
end

it "properly inserts a new node as a right-left child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
expect(tree.root.right.left.title).to eq "Mad Max 2: The Road Warrior"
end

it "properly inserts a new node as a right-right child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
tree.insert(tree.root, empire)
expect(tree.root.right.right.title).to eq "Star Wars: The Empire Strikes Back"
end
end

describe "#print" do
it "should print" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
tree.insert(tree.root, empire)
tree.printf()
end
end
end
81 changes: 81 additions & 0 deletions 01-data-structures/06-trees/binary_tree/min_binary_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
include RSpec

require_relative 'min_binary_heap'

RSpec.describe MinBinaryHeap, type: Class do
let (:root) { Node.new("The Matrix", 87) }

let (:tree) { MinBinaryHeap.new(root) }
let (:pacific_rim) { Node.new("Pacific Rim", 72) }
let (:braveheart) { Node.new("Braveheart", 78) }
let (:jedi) { Node.new("Star Wars: Return of the Jedi", 80) }
let (:donnie) { Node.new("Donnie Darko", 85) }
let (:inception) { Node.new("Inception", 86) }
let (:district) { Node.new("District 9", 90) }
let (:shawshank) { Node.new("The Shawshank Redemption", 91) }
let (:martian) { Node.new("The Martian", 92) }
let (:hope) { Node.new("Star Wars: A New Hope", 93) }
let (:empire) { Node.new("Star Wars: The Empire Strikes Back", 94) }
let (:mad_max_2) { Node.new("Mad Max 2: The Road Warrior", 98) }

describe "#insert(data)" do
it "properly inserts a new node and it swaps with the root" do
tree.insert(root, pacific_rim)
expect(tree.root.title).to eq "Pacific Rim"
end

it "properly inserts a new node and it swaps with the root" do
tree.insert(root, pacific_rim)
expect(tree.root.left.title).to eq "The Matrix"
end

it "properly inserts a new node as a left-left child" do
tree.insert(tree.root, braveheart)
tree.insert(tree.root, pacific_rim)
expect(tree.root.left.left.title).to eq "The Matrix"
end

it "properly inserts a new node as a left-right child" do
tree.insert(tree.root, donnie)
tree.insert(tree.root, inception)
expect(tree.root.right.title).to eq "Inception"
end

it "properly inserts a new node as a right child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
expect(tree.root.right.title).to eq "District 9"
end

it "properly inserts a new node as a right-left child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
expect(tree.root.right.left.title).to eq "Mad Max 2: The Road Warrior"
end

it "properly inserts a new node as a right-right child" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
tree.insert(tree.root, empire)
expect(tree.root.right.right.title).to eq "Star Wars: The Empire Strikes Back"
end
end

describe "#print" do
it "should print" do
tree.insert(tree.root, pacific_rim)
tree.insert(tree.root, district)
tree.insert(tree.root, hope)
tree.insert(tree.root, martian)
tree.insert(tree.root, mad_max_2)
tree.insert(tree.root, empire)
tree.printf()
end
end
end
4 changes: 3 additions & 1 deletion 01-data-structures/06-trees/binary_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ class Node
attr_accessor :right

def initialize(title, rating)
@title, @rating = title, rating
end
end
end