diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..39707d7 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -2,59 +2,127 @@ 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(n) is worst case if tree is not balanced where n is the height of the tree + # Space Complexity: O(1) def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + + if @root == nil + @root = new_node + return + end + + current = @root + + while current != nil + if current.key >= key + break if current.left == nil + current = current.left + else + break if current.right == nil + current = current.right + end + end + + current.key >= key ? current.left = new_node : current.right = new_node end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) is worst case if tree is not balanced where n is the height of the tree + # Space Complexity: O(1) def find(key) - raise NotImplementedError + current = @root + + while current != nil + if current.key > key + current = current.left + elsif current.key < key + current = current.right + else + return current.value + end + end + return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree def inorder - raise NotImplementedError + return inorder_recursion(current = @root, tree = []) end - # Time Complexity: - # Space Complexity: + def inorder_recursion(current, tree) + if current != nil + inorder_recursion(current.left, tree) + tree << { key: current.key, value: current.value } + inorder_recursion(current.right, tree) + else + return tree + end + end + + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree def preorder - raise NotImplementedError + return preorder_recursion(current = @root, tree = []) end - # Time Complexity: - # Space Complexity: + def preorder_recursion(current, tree) + if current != nil + tree << { key: current.key, value: current.value } + preorder_recursion(current.left, tree) + preorder_recursion(current.right, tree) + else + return tree + end + end + + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree xs4 def postorder - raise NotImplementedError + return postorder_recursion(current = @root, tree = []) end - # Time Complexity: - # Space Complexity: - def height - raise NotImplementedError + def postorder_recursion(current, tree) + if current != nil + postorder_recursion(current.left, tree) + postorder_recursion(current.right, tree) + tree << { key: current.key, value: current.value } + else + return tree + end + end + + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(1) + def height(current = @root, current_height = 0, max_height = 0) + return max_height if current == nil + + max_height = current_height if current_height > max_height + max_height = height(current.left, current_height += 1, max_height) + current_height -= 1 + max_height = height(current.right, current_height += 1, max_height) + + return max_height end # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def bfs raise NotImplementedError end diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..7bae9cb 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -1,10 +1,9 @@ -require_relative 'test_helper' - +require_relative "test_helper" 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 +36,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 +60,31 @@ 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 "breadth first search" do - it "will give an empty array for an empty tree" do - expect(tree.bfs).must_equal [] + describe "height" do + it "will find the height of a tree" do + expect(tree_with_nodes.height()).must_equal 3 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"}] + it "will return 0 for an empty tree" do + expect(tree.height()).must_equal 0 end end -end \ No newline at end of file + + # describe "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" }] + # end + # end +end