diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..4afb8d3 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -14,42 +14,121 @@ class Tree attr_reader :root def initialize @root = nil + @nodes = [] + end + + def add_helper(current, key) + if key <= current.key + return current if current.left.nil? + add_helper(current.left, key) + else + return current if current.right.nil? + add_helper(current.right, key) + end end # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + if @root == nil + @root = TreeNode.new(key, value) + return + end + + node = add_helper(@root, key) + if key <= node.key + node.left = TreeNode.new(key, value) + else + node.right = TreeNode.new(key, value) + end + end + + def find_helper(current, key) + if key <= current.key + return current.value if current.key == key + find_helper(current.left, key) + elsif key > current.key + return current.value if current.key == key + find_helper(current.right, key) + end end # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return nil if @root.nil? + return @root.value if @root.key == key + + current = @root + find_key = find_helper(current, key) + + return find_key + end + + # inorder helper + def traverse_inorder(tree) + traverse_inorder(tree.left) if tree.left + @nodes.push({key: tree.key, value: tree.value}) + traverse_inorder(tree.right) if tree.right end # Time Complexity: # Space Complexity: + # left, root, right def inorder - raise NotImplementedError + return [] if @root.nil? + traverse_inorder(@root) + return @nodes end + # preorder helper + def traverse_preorder(tree) + @nodes.push({key: tree.key, value: tree.value}) + traverse_preorder(tree.left) if tree.left + traverse_preorder(tree.right) if tree.right + end + # Time Complexity: # Space Complexity: def preorder - raise NotImplementedError + return [] if @root.nil? + traverse_preorder(@root) + return @nodes end + # postorder helper + def traverse_postorder(tree) + traverse_postorder(tree.left) if tree.left + traverse_postorder(tree.right) if tree.right + @nodes.push({key: tree.key, value: tree.value}) + end + # Time Complexity: # Space Complexity: def postorder - raise NotImplementedError + return [] if @root.nil? + traverse_postorder(@root) + return @nodes + end + + def height_helper(tree, height) + return height_helper(tree.left, height) if tree.left + return height_helper(tree.right, height) if tree.right + return height + 1 end # Time Complexity: # Space Complexity: def height - raise NotImplementedError + return 0 if @root.nil? + return 1 if (@root.left.nil? && @root.right.nil?) + + left_height = 1 + height_helper(@root.left, 1) if @root.left + right_height = 1 + height_helper(@root.right, 1) if @root.right + + return left_height >= right_height ? left_height : right_height end # Optional Method diff --git a/test/tree_test.rb b/test/tree_test.rb index 21c4a80..1f45c90 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -34,6 +34,16 @@ tree.add(3, "Paul") expect(tree.find(3)).must_equal "Paul" + + tree.add(4, "Mary") + expect(tree.find(4)).must_equal "Mary" + + tree.add(10, "Karla") + expect(tree.find(10)).must_equal "Karla" + + tree.add(1, "Jenny") + expect(tree.find(1)).must_equal "Jenny" + end it "can't find anything when the tree is empty" do @@ -49,6 +59,15 @@ 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(balanced_tree.inorder).must_equal [ + {:key=>1, :value=>"Mary"}, + {:key=>3, :value=>"Paul"}, + {:key=>4, :value=>"Kate"}, + {:key=>5, :value=>"Peter"}, + {:key=>8, :value=>"Ada"}, + {:key=>10, :value=>"Karla"}, + {:key=>25, :value=>"Kari"} + ] end end