diff --git a/01-data-structures/05-hashes-part-2/separate_chaining/instance.rb b/01-data-structures/05-hashes-part-2/separate_chaining/instance.rb new file mode 100644 index 00000000..8a13b436 --- /dev/null +++ b/01-data-structures/05-hashes-part-2/separate_chaining/instance.rb @@ -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 \ No newline at end of file diff --git a/01-data-structures/06-trees/binary_tree/benchmark.rb b/01-data-structures/06-trees/binary_tree/benchmark.rb new file mode 100644 index 00000000..b732f0f9 --- /dev/null +++ b/01-data-structures/06-trees/binary_tree/benchmark.rb @@ -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 diff --git a/01-data-structures/06-trees/binary_tree/binary_search_tree.rb b/01-data-structures/06-trees/binary_tree/binary_search_tree.rb index 5cded8ea..20a3f296 100644 --- a/01-data-structures/06-trees/binary_tree/binary_search_tree.rb +++ b/01-data-structures/06-trees/binary_tree/binary_search_tree.rb @@ -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 diff --git a/01-data-structures/06-trees/binary_tree/min_binary_heap.rb b/01-data-structures/06-trees/binary_tree/min_binary_heap.rb new file mode 100644 index 00000000..ac04de1f --- /dev/null +++ b/01-data-structures/06-trees/binary_tree/min_binary_heap.rb @@ -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 diff --git a/01-data-structures/06-trees/binary_tree/min_binary_heap_spec.rb b/01-data-structures/06-trees/binary_tree/min_binary_heap_spec.rb new file mode 100644 index 00000000..c52598cf --- /dev/null +++ b/01-data-structures/06-trees/binary_tree/min_binary_heap_spec.rb @@ -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 \ No newline at end of file diff --git a/01-data-structures/06-trees/binary_tree/min_binary_tree_spec.rb b/01-data-structures/06-trees/binary_tree/min_binary_tree_spec.rb new file mode 100644 index 00000000..c52598cf --- /dev/null +++ b/01-data-structures/06-trees/binary_tree/min_binary_tree_spec.rb @@ -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 \ No newline at end of file diff --git a/01-data-structures/06-trees/binary_tree/node.rb b/01-data-structures/06-trees/binary_tree/node.rb index c0106c8c..70ad3a08 100644 --- a/01-data-structures/06-trees/binary_tree/node.rb +++ b/01-data-structures/06-trees/binary_tree/node.rb @@ -5,5 +5,7 @@ class Node attr_accessor :right def initialize(title, rating) + @title, @rating = title, rating end -end \ No newline at end of file +end +