diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a798682 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#7f40bf", + "activityBar.activeBorder": "#c78f58", + "activityBar.foreground": "#e7e7e7", + "activityBar.inactiveForeground": "#e7e7e799", + "activityBarBadge.background": "#c78f58", + "activityBarBadge.foreground": "#15202b", + "titleBar.activeBackground": "#663399", + "titleBar.inactiveBackground": "#66339999", + "titleBar.activeForeground": "#e7e7e7", + "titleBar.inactiveForeground": "#e7e7e799", + "statusBar.background": "#663399", + "statusBarItem.hoverBackground": "#7f40bf", + "statusBar.foreground": "#e7e7e7" + }, + "peacock.color": "#639" +} \ No newline at end of file diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..e5b287f 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,13 +1,34 @@ 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 + + def inorder(list) + list = left.inorder(list) unless left.nil? + list << {key: key, value: value} + list = right.inorder(list) unless right.nil? + return list + end + + def preorder(list) + list << {key: key, value: value} + list = left.preorder(list) unless left.nil? + list = right.preorder(list) unless right.nil? + return list + end + + def postorder(list) + list = left.postorder(list) unless left.nil? + list = right.postorder(list) unless right.nil? + list << {key: key, value: value} + return list + end end class Tree @@ -15,50 +36,130 @@ class Tree def initialize @root = nil end - - # Time Complexity: - # Space Complexity: + + # Time Complexity: O(n) + # Space Complexity: o(n) + def add_helper(current, key, value) + return TreeNode.new(key, value) if current.nil? + + if key <= current.key + current.left = add_helper(current.left, key, value) + else + current.right = add_helper(current.right, key, value) + end + return current + end + def add(key, value) - raise NotImplementedError + @root = add_helper(@root, key, value) end - - # Time Complexity: - # Space Complexity: + + # Time Complexity: O(1) + # Space Complexity: O1 def find(key) - raise NotImplementedError + current = @root + until current.nil? + if current.key == key + return current.value + elsif key > current.key + current = current.right + else + current = current.left + end + end + return nil end - - # Time Complexity: - # Space Complexity: + + # Time Complexity: On + # Space Complexity: On + def inorder_helper + return inorder_helper(@root, []) + list = left.inorder(list) unless left.nil? + list << {key: key, value: value} + list = right.inorder(list) unless right.nil? + return list + end + def inorder - raise NotImplementedError + list = [] + return list if @root.nil? + return @root.inorder(list) end - - # Time Complexity: - # Space Complexity: + + # Time Complexity: On + # Space Complexity: On def preorder - raise NotImplementedError + list = [] + return list if @root.nil? + return @root.preorder([]) end - - # Time Complexity: - # Space Complexity: + + # Time Complexity: On + # Space Complexity: On def postorder - raise NotImplementedError + list = [] + return list if @root.nil? + return @root.postorder([]) end - + # Time Complexity: # Space Complexity: + def max (num1, num2) + if num1 > num2 + return num1 + else + return num2 + end + end + + def height_helper(current) + return 0 if current.nil? + return 1 + max(height_helper(current.left), height_helper(current.right)) + end + def height - raise NotImplementedError + return height_helper(@root) + end + + def delete_helper(current, key) + return nil if current.nil? + + if current.key > key + current.left = delete_helper(current.left, key) + return current + end + + if current.key < key + current.right = delete_helper(current.right, key) + return current + end + + if current.left.nil? && current.right.nil? + return nil + end + + if current.left.nil? + return current.right + end + + if current.right.nil? + return current.left + end + + end + + def delete(key) + value = self.find(key) + @root = delete_helper(@root, key) + return value end - # Optional Method # Time Complexity: # Space Complexity: def bfs raise NotImplementedError end - + # Useful for printing def to_s return "#{self.inorder}" diff --git a/test/tree_test.rb b/test/tree_test.rb index 345bf66..e677ede 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -2,121 +2,121 @@ describe Tree do let (:tree) {Tree.new} - + let (:tree_with_nodes) { + tree.add(5, "Peter") + tree.add(3, "Paul") + tree.add(1, "Mary") + tree.add(10, "Karla") + tree.add(15, "Ada") + tree.add(25, "Kari") + tree +} + +describe "add and find" do + it "add & find values" do tree.add(5, "Peter") - tree.add(3, "Paul") - tree.add(1, "Mary") - tree.add(10, "Karla") + expect(tree.find(5)).must_equal "Peter" + tree.add(15, "Ada") - tree.add(25, "Kari") - tree - } - - describe "add and find" do - it "add & find values" do - tree.add(5, "Peter") - expect(tree.find(5)).must_equal "Peter" - - tree.add(15, "Ada") - expect(tree.find(15)).must_equal "Ada" - - tree.add(3, "Paul") - expect(tree.find(3)).must_equal "Paul" - end - - it "can't find anything when the tree is empty" do - expect(tree.find(50)).must_be_nil - end + expect(tree.find(15)).must_equal "Ada" + + tree.add(3, "Paul") + expect(tree.find(3)).must_equal "Paul" end - - describe "inorder" do - it "will give an empty array for an empty tree" do - expect(tree.inorder).must_equal [] - 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"}] - end + + it "can't find anything when the tree is empty" do + expect(tree.find(50)).must_be_nil end +end +describe "inorder" do + it "will give an empty array for an empty tree" do + expect(tree.inorder).must_equal [] + 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"}] + 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"}] - 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"}] + end +end - describe "postorder" do - it "will give an empty array for an empty tree" do - expect(tree.postorder).must_equal [] - 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"}] - end +describe "postorder" do + it "will give an empty array for an empty tree" do + expect(tree.postorder).must_equal [] + 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"}] end +end - describe "breadth first search" do - it "will give an empty array for an empty tree" do - expect(tree.bfs).must_equal [] - end +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"}] + end +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 +describe "height" do + it "will return 0 if tree is empty" do + expect(tree.height()).must_equal 0 end - describe "height" do - it "will return 0 if tree is empty" do - expect(tree.height()).must_equal 0 - end + it "will return the nuber of nodes in the longest path" do + expect(tree_with_nodes.height).must_equal 4 + tree_with_nodes.add(60, "sam") + tree_with_nodes.add(58, "penny") + tree_with_nodes.add(65, "sam") + expect(tree_with_nodes.height).must_equal 6 + end + + it "will give the correct height of a binary search tree" do + tree_with_nodes.add(30, "Tatiana") + expect(tree_with_nodes.height).must_equal 5 + end +end - it "will return the nuber of nodes in the longest path" do - expect(tree_with_nodes.height).must_equal 4 - tree_with_nodes.add(60, "sam") - tree_with_nodes.add(58, "penny") - tree_with_nodes.add(65, "sam") - expect(tree_with_nodes.height).must_equal 6 - end +describe "delete" do + it "can delete a note in the tree" do + # Arrange & Assert + expect(tree_with_nodes.find(15)).must_equal "Ada" + + # Act + tree_with_nodes.delete(15) - it "will give the correct height of a binary search tree" do - tree_with_nodes.add(30, "Tatiana") - expect(tree_with_nodes.height).must_equal 5 - end + # Assert + expect(tree_with_nodes.find(15)).must_be_nil end - - describe "delete" do - it "can delete a note in the tree" do - # Arrange & Assert - expect(tree_with_nodes.find(15)).must_equal "Ada" - - # Act - tree_with_nodes.delete(15) - - # Assert - expect(tree_with_nodes.find(15)).must_be_nil - end - - it "will return nil if the node is not in the tree when it's deleted" do - # Arrange & Act - answer = tree_with_nodes.delete(47) - - # Assert - expect(answer).must_be_nil - expect(tree_with_nodes.find(47)).must_be_nil - end + + it "will return nil if the node is not in the tree when it's deleted" do + # Arrange & Act + answer = tree_with_nodes.delete(47) + + # Assert + expect(answer).must_be_nil + expect(tree_with_nodes.find(47)).must_be_nil end end +end