diff --git a/README.md b/README.md index 73287d1..eec9657 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Tree Exercise +This project is due **Monday September 2nd, 2019** + In this exercise you will implement, in Ruby, several Tree methods. - `add(value)` - This method adds a value to the Binary Search Tree diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..c8aa902 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,129 @@ def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n)/ O(n) + # Space Complexity: O(log n) for a balanced BST; O(n) for unbalanced BST + def add_helper(curr, key, value) + return TreeNode.new(key, value) if curr.nil? + + if key < curr.key + curr.left = add_helper(curr.left, key, value) + else + curr.right = add_helper(curr.right, key, value) + end + + return curr + end + def add(key, value) - raise NotImplementedError + @root = add_helper(@root, key, value) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n)/O(n) + # Space Complexity: O(1) def find(key) - raise NotImplementedError + curr = @root + + until curr.nil? + if key > curr.key + curr = curr.right + elsif key < curr.key + curr = curr.left + else + return curr.value + end + end + + return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + def inorder_helper(curr, list) + return list if curr.nil? + + # inorder: left - root - right + inorder_helper(curr.left, list) + list << {key: curr.key, value: curr.value} + inorder_helper(curr.right, list) + + return list + end + def inorder - raise NotImplementedError + return inorder_helper(@root, []) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + def preorder_helper(curr, list) + return list if curr.nil? + + # preorder: root - left - right + list << { key: curr.key, value: curr.value } + preorder_helper(curr.left, list) + preorder_helper(curr.right, list) + + return list + end + def preorder - raise NotImplementedError + return preorder_helper(@root, []) + end + + # Time Complexity: O(n) + # Space Complexity: O(n) + def postorder_helper(curr, list) + return list if curr.nil? + + # postorder: left - right - root + postorder_helper(curr.left, list) + postorder_helper(curr.right, list) + list << { key: curr.key, value: curr.value } + + return list end - # Time Complexity: - # Space Complexity: def postorder - raise NotImplementedError + return postorder_helper(@root, []) + end + + # Time Complexity: O(n) + # Space Complexity: O(log n)/ O(n) + def height_helper(curr, height) + return height if curr.nil? + + height += 1 + left_height = height_helper(curr.left, height) + right_height = height_helper(curr.right, height) + + return [left_height, right_height].max end - # Time Complexity: - # Space Complexity: def height - raise NotImplementedError + return height_helper(@root, 0) end # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def bfs - raise NotImplementedError + visted = [] + return visted if @root.nil? + + to_visit = [@root] + until to_visit.empty? + curr = to_visit.shift + visted << { + key: curr.key, + value: curr.value + } + + to_visit << curr.left if curr.left + to_visit << curr.right if curr.right + end + + return visted end # Useful for printing diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..222b693 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -68,6 +68,16 @@ {:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}] end end + + describe "height" do + it "returns 0 for an empty tree" do + expect(tree.height).must_equal 0 + end + + it "returns correct height of tree" do + expect(tree_with_nodes.height).must_equal 4 + end + end describe "breadth first search" do it "will give an empty array for an empty tree" do @@ -80,4 +90,4 @@ {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] end end -end \ No newline at end of file +end