diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..1877d38 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -2,12 +2,12 @@ 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 @@ -16,47 +16,153 @@ def initialize @root = nil end - # Time Complexity: - # Space Complexity: - def add(key, value) - raise NotImplementedError + # Time Complexity: O(log n) + # Space Complexity: [O(1)-if it's a loop] - why do I wanna say O(n)?? help me. + # Try with a while loop + def add(key, value = nil) + if @root.nil? + @root = TreeNode.new(key, value) + else + add_helper(@root, key, value) + end + end + + def add_helper(current, key, value) + # if current = return TreeNode.new + if key < current.key + # if current.left is nil place the new node + # otherwise make current.left the new current and run through the same checks + if current.left.nil? + current.left = TreeNode.new(key, value) + return + else + add_helper(current.left, key, value) + end + else + if current.right.nil? + current.right = TreeNode.new(key, value) + return + else + add_helper(current.right, key, value) + end + end end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) + # Space Complexity: O(1) def find(key) - raise NotImplementedError + return find_helper(@root, key) + end + + def find_helper(current, key) + if current.nil? + return nil + end + if current.key == key + return current.value + end + if key > current.key + return find_helper(current.right, key) + else + return find_helper(current.left, key) + end end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + # left-root-right def inorder - raise NotImplementedError + # current = @root + inorder_helper(@root, []) + end + + def inorder_helper(current, values) # Values is an array + if current.nil? + return values + end + inorder_helper(current.left, values) + values.push({:key=>current.key, :value=>current.value}) + inorder_helper(current.right, values) + return values end - # Time Complexity: - # Space Complexity: + + # Time Complexity: O(n) + # Space Complexity: O(n) + # root-left-right def preorder - raise NotImplementedError + preorder_helper(@root, []) end - # Time Complexity: - # Space Complexity: + def preorder_helper(current, values) + if current.nil? + return values + end + values.push({:key=>current.key, :value=>current.value}) + preorder_helper(current.left, values) + preorder_helper(current.right, values) + return values + end + + # Time Complexity: O(n) + # Space Complexity: O(n) + # left-right-root def postorder - raise NotImplementedError + postorder_helper(@root, []) + end + + def postorder_helper(current, values) + if current.nil? + return values + end + postorder_helper(current.left, values) + postorder_helper(current.right, values) + values.push({:key=>current.key, :value=>current.value}) + return values end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n)^2 + # Space Complexity: O(n) def height - raise NotImplementedError + # raise NotImplementedError + height_helper(@root) end + def height_helper(current) + if current.nil? + return 0 + end + left_height = height_helper(current.left) + right_height = height_helper(current.right) + if left_height > right_height + return left_height + 1 + else + return right_height + 1 + end + end + +# ********************************************************************** # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) Does shift add time? O(n)^2? + # Space Complexity: O(n) def bfs - raise NotImplementedError + return [] if @root.nil? + + to_be_chkd = [@root] + output = [] + + while to_be_chkd.length > 0 + current = to_be_chkd.shift + # argument + output.push({:key=>current.key, :value=>current.value}) + if current.left != nil + to_be_chkd.push(current.left) + end + if current.right != nil + to_be_chkd.push(current.right) + end + end + return output end # Useful for printing diff --git a/test/tree_test.rb b/test/tree_test.rb index dbf3447..04ca96f 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -1,4 +1,5 @@ require_relative 'test_helper' +require 'pry' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -39,8 +40,8 @@ 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"}] + {:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"}, + {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] end end @@ -64,8 +65,8 @@ 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"}] + {:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"}, + {:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}] end end @@ -76,8 +77,8 @@ 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"}] + {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, + {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] end end @@ -96,7 +97,7 @@ end it "will report the height for a balanced tree" do - expect(tree_with_nodes.height).must_equal 3 + expect(tree_with_nodes.height).must_equal 4 end it "will report the height for unbalanced trees" do