-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Denisse #18
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?
Time - Denisse #18
Changes from all commits
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 |
|---|---|---|
|
|
@@ -16,40 +16,101 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| @root = add_helper(@root, key, value) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add_helper(root, key, value) | ||
| if root.nil? | ||
| root = TreeNode.new(key, value) | ||
| return root | ||
| end | ||
|
|
||
| if root.key > key | ||
| root.left = add_helper(root.left, key, value) | ||
| else | ||
| root.right = add_helper(root.right, key, value) | ||
| end | ||
|
|
||
| return root | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def find(key) | ||
|
Comment on lines
+40
to
42
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. 👍 , well done here, very compact. |
||
| raise NotImplementedError | ||
| return find_helper(@root, key) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def find_helper(node, key) | ||
| return nil if node.nil? | ||
|
|
||
| if node.key == key | ||
| return node.value | ||
| elsif node.key < key | ||
| return find_helper(node.right, key) | ||
| else | ||
| return find_helper(node.left, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
|
Comment on lines
+58
to
60
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 | ||
| result = [] | ||
| inorder_helper(@root, result) | ||
| return result | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_helper(node, result) | ||
| return if node.nil? | ||
|
|
||
| inorder_helper(node.left, result) | ||
| result.push({:key => node.key, :value => node.value}) | ||
| inorder_helper(node.right, result) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder | ||
|
Comment on lines
+74
to
76
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 | ||
| result = [] | ||
| preorder_helper(@root, result) | ||
| return result | ||
| end | ||
| def preorder_helper(node, result) | ||
| return if node.nil? | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| result.push({:key => node.key, :value => node.value}) | ||
| preorder_helper(node.left, result) | ||
| preorder_helper(node.right, result) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
|
Comment on lines
+89
to
91
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 | ||
| result = [] | ||
| postorder_helper(@root, result) | ||
| return result | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder_helper(node, result) | ||
| return if node.nil? | ||
|
|
||
| postorder_helper(node.left, result) | ||
| postorder_helper(node.right, result) | ||
| result.push({:key => node.key, :value => node.value}) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def height | ||
|
Comment on lines
+105
to
107
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. The space complexity would be O(h), where h is the height of the tree, due to the call stack. |
||
| raise NotImplementedError | ||
| return height_helper(@root, 0) | ||
| end | ||
|
|
||
| def height_helper(node, steps) | ||
| return steps if node.nil? | ||
| return [height_helper(node.left, steps + 1), height_helper(node.right, steps + 1)].max | ||
| 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.
👍