-
Notifications
You must be signed in to change notification settings - Fork 44
India - Water #32
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?
India - Water #32
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,98 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| def add_helper(current, key, value) | ||
| if current.nil? | ||
| current = TreeNode.new(key, value) | ||
| elsif key <= current.key | ||
| current.left = add_helper(current.left, key, value) | ||
| else | ||
| current.right = add_helper(current.right, key, value) | ||
| end | ||
| return current | ||
| end | ||
| # Time Complexity: O(logn) | ||
| # Space Complexity: O(logn) | ||
| def add(key, value = nil) | ||
| @root = add_helper(@root, key, value) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def find(key) | ||
| raise NotImplementedError | ||
| def find_helper(current, key) | ||
| return nil if current.nil? | ||
|
|
||
| if current.key == key | ||
| return current.value | ||
| elsif key < current.key | ||
| return find_helper(current.left, key) | ||
| else | ||
| return find_helper(current.right, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(logn) | ||
| # Space Complexity: O(logn) | ||
|
|
||
| def find(key, current = @root) | ||
|
Comment on lines
+47
to
+50
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 method is Log n if the tree is roughly balanced. |
||
| return find_helper(current, key) | ||
| end | ||
|
|
||
| def inorder_helper(current, values) | ||
| return values if current.nil? | ||
|
|
||
| inorder_helper(current.left, values) | ||
| values.push({:key=>current.key, :value=>current.value}) | ||
| inorder_helper(current.right, values) | ||
| return values | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
|
Comment on lines
+63
to
65
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 | ||
| values = [] | ||
| return inorder_helper(@root, values) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(current, values) | ||
| return values if current.nil? | ||
|
|
||
| values.push({:key=>current.key, :value=>current.value}) | ||
| preorder_helper(current.left, values) | ||
| preorder_helper(current.right, values) | ||
| return values | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder | ||
|
Comment on lines
+79
to
81
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 | ||
| values = [] | ||
| return preorder_helper(@root, values) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder_helper(current, values) | ||
| return values if current.nil? | ||
|
|
||
| postorder_helper(current.left, values) | ||
| postorder_helper(current.right, values) | ||
| values.push({:key=>current.key, :value=>current.value}) | ||
| return values | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
|
Comment on lines
+95
to
97
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 | ||
| values = [] | ||
| return postorder_helper(@root, values) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height_helper(current) | ||
| return 0 if current.nil? | ||
|
|
||
| return 1 + [height_helper(current.left), height_helper(current.right)].max | ||
| end | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def height | ||
|
Comment on lines
+107
to
109
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. 👍 Because the recursion depth is dependent on the height of the tree it's O(log n) for space complexity for a balanced tree. It's O(n) if the tree isn't balanced. |
||
| raise NotImplementedError | ||
| height_helper(@root) | ||
| 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.
👍 the method is Log n if the tree is roughly balanced.