diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..b0db9b0 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..15ffe97 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..59da35e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tree-practice.iml b/.idea/tree-practice.iml new file mode 100644 index 0000000..2817928 --- /dev/null +++ b/.idea/tree-practice.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.rakeTasks b/.rakeTasks new file mode 100644 index 0000000..e409da2 --- /dev/null +++ b/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..327b1ee 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,49 +16,133 @@ def initialize @root = nil end - # Time Complexity: - # Space Complexity: - def add(key, value) - raise NotImplementedError + # Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) + # Space Complexity: O(1) + def add(key, value = nil) + if @root.nil? + @root = TreeNode.new(key, value) + return @root.value + end + + current = @root + + while key <= current.key || key > current.key + if key == current.key + "This key is already taken." + elsif key < current.key + if current.left.nil? + current.left = TreeNode.new(key,value) + return value + else + current = current.left + end + else + if current.right.nil? + current.right = TreeNode.new(key,value) + return value + else + current = current.right + end + end + end + end - # Time Complexity: + + # Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) # Space Complexity: def find(key) - raise NotImplementedError + current = @root + while current != nil + if current.key == key + return current.value + elsif current.key > key + current = current.left + elsif current.key < key + current = current.right + end + end + return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def inorder - raise NotImplementedError + array = [] + current = @root + inorder_helper(array, current) end - # Time Complexity: - # Space Complexity: + def inorder_helper(array, current) + return array if current.nil? + + #left + inorder_helper(array, current.left) + #middle + array << {:key => current.key, :value => current.value} + #right + inorder_helper(array, current.right) + + end + + # Time Complexity: O(n) + # Space Complexity: O(n) def preorder - raise NotImplementedError + array = [] + current = @root + preorder_helper(array, current) end - # Time Complexity: - # Space Complexity: + def preorder_helper(array, current) + return array if current.nil? + #middle + array << {:key => current.key, :value => current.value} + #left + preorder_helper(array, current.left) + #right + preorder_helper(array, current.right) + + end + + # Time Complexity: O(n) + # Space Complexity: O(n) def postorder - raise NotImplementedError + array = [] + current = @root + postorder_helper(array, current) end - # Time Complexity: - # Space Complexity: + def postorder_helper(array, current) + return array if current.nil? + + #left + postorder_helper(array, current.left) + #right + postorder_helper(array, current.right) + #middle + array << {:key => current.key, :value => current.value} + + end + + # Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) + # Space Complexity: O(1) def height - raise NotImplementedError + height_helper(@root) + end + + def height_helper(root) + return 0 if root.nil? + [1 + height_helper(root.left), 1 + height_helper(root.right)].max end # Optional Method # Time Complexity: # Space Complexity: def bfs - raise NotImplementedError + # optional end + # Useful for printing def to_s return "#{self.inorder}"