From f8b3ffa5544f9c044f4b4b4ba0c61ef015b21fd2 Mon Sep 17 00:00:00 2001 From: Angela Date: Mon, 2 Sep 2019 13:50:45 -0700 Subject: [PATCH] tests pass, still working of bfs --- lib/tree.rb | 127 +++++++++++++++++++++++++++++++++++++--------- test/tree_test.rb | 50 +++++++++++------- 2 files changed, 135 insertions(+), 42 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..a73c07d 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -2,61 +2,142 @@ class TreeNode attr_reader :key, :value attr_accessor :left, :right - def initialize(key, val) + def initialize(key, val) @key = key @value = val @left = nil @right = nil - end + end end class Tree attr_reader :root + def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(logn) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data + # Space Complexity: O(1) def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + @root = new_node if @root.nil? + current = @root + add_helper(current, new_node) end - # Time Complexity: - # Space Complexity: + def add_helper(current, new_node) + if current.nil? + return current = new_node + elsif new_node.key < current.key + current.left = add_helper(current.left, new_node) + elsif new_node.key > current.key + current.right = add_helper(current.right, new_node) + end + return current + end + + # Time Complexity: O(logn) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data + # Space Complexity: O(1) def find(key) - raise NotImplementedError + current = @root + return nil if current.nil? + return current.value if current.key == key + find_helper(current, key) end - # Time Complexity: - # Space Complexity: + def find_helper(current, key) + if key == current.key + return current.value + elsif key < current.key + current.left = find_helper(current.left, key) + else + current.right = find_helper(current.right, key) + end + end + + # Time Complexity: O(n) because each node will need to be visited and added to the final array + # Space Complexity: O(n) because the final array will contain the size of the tree + # left, root, right def inorder - raise NotImplementedError + array = [] + return array if @root.nil? + inorder_helper(array, @root) + return array + end + + def inorder_helper(array, node) + return if node.nil? + inorder_helper(array, node.left) + hash = { key: node.key, value: node.value } + array.push(hash) + inorder_helper(array, node.right) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) because each node will need to be visited and added to the final array + # Space Complexity: O(n) because the final array will contain the size of the tree + # root, left, right def preorder - raise NotImplementedError + return [] if @root.nil? + array = [] + preorder_helper(@root, array) + return array end - # Time Complexity: - # Space Complexity: + def preorder_helper(node, array) + return if node.nil? + array.push({ key: node.key, value: node.value }) + preorder_helper(node.left, array) + preorder_helper(node.right, array) + end + + # Time Complexity: O(n) because each node will need to be visited and added to the final array + # Space Complexity: O(n) because the final array will contain the size of the tree + # left, right, root def postorder - raise NotImplementedError + array = [] + return array if @root.nil? + postorder_helper(array, @root) + return array + end + + def postorder_helper(array, node) + return if node.nil? + postorder_helper(array, node.left) + postorder_helper(array, node.right) + # hash = { key: node.key, value: node.value } + array.push({ key: node.key, value: node.value }) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) because worst case each node will need to be visited to be counted + # Space Complexity: O(1) def height - raise NotImplementedError + return 0 if @root.nil? + node = @root + return height_helper(@root) + end + + def height_helper(node) + return 0 if node.nil? + left = height_helper(node.left) + right = height_helper(node.right) + + if left >= right + return left + 1 + else + return right + 1 + end end # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def bfs - raise NotImplementedError + array = [] + return array if @root.nil? + end + + def bfs_helper(node, level, array) end # Useful for printing diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..97d3576 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -1,10 +1,10 @@ -require_relative 'test_helper' - +require_relative "test_helper" +require "minitest/skip_dsl" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe Tree do - let (:tree) {Tree.new} + let (:tree) { Tree.new } let (:tree_with_nodes) { tree.add(5, "Peter") @@ -37,23 +37,21 @@ end it "will return the tree in order" do - - expect(tree_with_nodes.inorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"}, - {:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + expect(tree_with_nodes.inorder).must_equal [{ :key => 1, :value => "Mary" }, { :key => 3, :value => "Paul" }, + { :key => 5, :value => "Peter" }, { :key => 10, :value => "Karla" }, + { :key => 15, :value => "Ada" }, { :key => 25, :value => "Kari" }] end end - describe "preorder" do it "will give an empty array for an empty tree" do expect(tree.preorder).must_equal [] end it "will return the tree in preorder" do - expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, - {:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + expect(tree_with_nodes.preorder).must_equal [{ :key => 5, :value => "Peter" }, { :key => 3, :value => "Paul" }, + { :key => 1, :value => "Mary" }, { :key => 10, :value => "Karla" }, + { :key => 15, :value => "Ada" }, { :key => 25, :value => "Kari" }] end end @@ -63,21 +61,35 @@ end it "will return the tree in postorder" do - expect(tree_with_nodes.postorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"}, - {:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"}, - {:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}] + expect(tree_with_nodes.postorder).must_equal [{ :key => 1, :value => "Mary" }, { :key => 3, :value => "Paul" }, + { :key => 25, :value => "Kari" }, { :key => 15, :value => "Ada" }, + { :key => 10, :value => "Karla" }, { :key => 5, :value => "Peter" }] + end + end + + describe "height" do + it "will return 0 if tree is empty" do + expect(tree.height).must_equal 0 + end + + it "will return the height of the tree" do + expect(tree_with_nodes.height).must_equal 4 + tree_with_nodes.add(2, "bob") + expect(tree_with_nodes.height).must_equal 4 + tree_with_nodes.add(50, "angela") + expect(tree_with_nodes.height).must_equal 5 end end - describe "breadth first search" do + xdescribe "breadth first search" do it "will give an empty array for an empty tree" do expect(tree.bfs).must_equal [] end it "will return an array of a level-by-level output of the tree" do - expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, - {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + expect(tree_with_nodes.bfs).must_equal [{ :key => 5, :value => "Peter" }, { :key => 3, :value => "Paul" }, + { :key => 10, :value => "Karla" }, { :key => 1, :value => "Mary" }, + { :key => 15, :value => "Ada" }, { :key => 25, :value => "Kari" }] end end -end \ No newline at end of file +end