-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Lee #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Space - Lee #15
Changes from all commits
0bb1a1c
223f64f
e047040
d912884
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,42 +14,114 @@ class Tree | |
| attr_reader :root | ||
| def initialize | ||
| @root = nil | ||
| @nodes = [] | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| new_node = TreeNode.new(key, value) | ||
| return @root = new_node if !@root | ||
| add_helper(@root, new_node) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def find(key) | ||
| raise NotImplementedError | ||
| def add_helper(current, new_node) | ||
| if new_node.key <= current.key | ||
| return current.left = new_node if !current.left | ||
| add_helper(current.left, new_node) | ||
| else | ||
| return current.right = new_node if !current.right | ||
| add_helper(current.right, new_node) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def find(key, current = @root) | ||
|
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return if !@root | ||
| return current.value if current.key == key | ||
|
|
||
| key <= current.key ? find(key, current.left) : find(key, current.right) | ||
| end | ||
|
|
||
| # Time Complexity: O(n), where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def inorder | ||
|
Comment on lines
+47
to
49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError | ||
| return @nodes if !@root | ||
| inorder_helper(@root) | ||
| return @nodes | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_helper(current) | ||
| return if !current | ||
| # traverse the left subtree | ||
| inorder_helper(current.left) | ||
| # visit the current node | ||
| @nodes.push( | ||
| { | ||
| key: current.key, | ||
| value: current.value | ||
| } | ||
| ) | ||
| #traverse the right subtree | ||
| inorder_helper(current.right) | ||
| end | ||
|
|
||
| # Time Complexity: O(n), where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def preorder | ||
|
Comment on lines
+70
to
72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError | ||
| return @nodes if !@root | ||
| preorder_helper(@root) | ||
| return @nodes | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(current) | ||
| return if !current | ||
| # visit the current node | ||
| @nodes.push( | ||
| { | ||
| key: current.key, | ||
| value: current.value | ||
| } | ||
| ) | ||
| # traverse the left subtree | ||
| preorder_helper(current.left) | ||
| #traverse the right subtree | ||
| preorder_helper(current.right) | ||
| end | ||
|
|
||
| # Time Complexity: O(n), where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def postorder | ||
|
Comment on lines
+93
to
95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError | ||
| return @nodes if !@root | ||
| postorder_helper(@root) | ||
| return @nodes | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height | ||
| raise NotImplementedError | ||
| def postorder_helper(current) | ||
| return if !current | ||
| # traverse the left subtree | ||
| postorder_helper(current.left) | ||
| #traverse the right subtree | ||
| postorder_helper(current.right) | ||
| # visit the current node | ||
| @nodes.push( | ||
| { | ||
| key: current.key, | ||
| value: current.value | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| # Time Complexity: O(n), where n is the number of nodes | ||
| # Space Complexity: O(n), where n is the height | ||
| def height(current = @root, height = 0) | ||
|
Comment on lines
+116
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| # If the current node is nil return 0 | ||
| return height if !current | ||
| # Otherwise return 1 plus the maximum of the heights of the right and left subtrees | ||
| left = height(current.left, height + 1) | ||
| right = height(current.right, height + 1) | ||
| return left >= right ? left : right | ||
| end | ||
|
|
||
| # Optional Method | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍