-
Notifications
You must be signed in to change notification settings - Fork 44
Fire-Kalki-Tree-Practice #17
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?
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .idea | ||
| Gemfile.lock | ||
| Gemfile |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,45 +16,124 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def add(key, value = nil) | ||
| # raise NotImplementedError | ||
| new_node = TreeNode.new(key, value) | ||
|
|
||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| def add_helper(current, new_node) | ||
| return new_node if current.nil? | ||
|
|
||
| if new_node.key < current.key | ||
| current.left = add_helper(current.left, new_node) | ||
| else | ||
| current.right = add_helper(current.right, new_node) | ||
| end | ||
| return current | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(l) | ||
| def find(key) | ||
|
Comment on lines
+43
to
45
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/time complexities are O(n) if the tree is unbalanced and O(log n) if the tree is balanced. I'm uncertain what O(l) is? |
||
| raise NotImplementedError | ||
| return @root if @root.nil? | ||
|
|
||
| current = @root | ||
|
|
||
| until current.nil? | ||
| if current.key == key | ||
| return current.value | ||
| elsif key <= current.key | ||
| current = current.left | ||
| else | ||
| current = current.right | ||
| end | ||
| end | ||
|
|
||
| return current | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) going through each node | ||
| # Space Complexity: O(n) creating an array | ||
| 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 | ||
| # raise NotImplementedError | ||
| return [] if @root.nil? | ||
|
|
||
| return inorder_helper(@root, []) | ||
| end | ||
|
|
||
| def inorder_helper(current, values) | ||
| return values if current.nil? | ||
|
|
||
| inorder_helper(current.left, values) | ||
| values << { key: current.key, value: current.value } | ||
| inorder_helper(current.right, values) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity:O(n) | ||
| def preorder | ||
|
Comment on lines
+81
to
83
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 | ||
| # raise NotImplementedError | ||
| return [] if @root.nil? | ||
|
|
||
| return preorder_helper(@root, []) | ||
| end | ||
|
|
||
| def preorder_helper(current, values) | ||
| return values if current.nil? | ||
|
|
||
| values << { key: current.key, value: current.value} | ||
| preorder_helper(current.left, values) | ||
| preorder_helper(current.right, values) | ||
|
|
||
| return values | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
|
Comment on lines
+100
to
102
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 | ||
| # raise NotImplementedError | ||
| return [] if @root.nil? | ||
|
|
||
| return postorder_helper(@root, []) | ||
| 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 << { key: current.key, value: current.value} | ||
|
|
||
| return values | ||
| end | ||
|
|
||
| # Time Complexity:O(n) | ||
| # Space Complexity:O(n) | ||
| def height | ||
|
Comment on lines
+119
to
121
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 is O(n) if the tree is unbalanced and O(log n) if the tree is balanced. |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| height_helper(@root, 0) | ||
| end | ||
|
|
||
| def height_helper(current, count) | ||
| return count if current.nil? | ||
|
|
||
| left = height_helper(current.left, count + 1) | ||
| right = height_helper(current.right, count + 1) | ||
| return [left, right].max | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
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.
👍 Good use of default parameters. The space/time complexities are O(n) if the tree is unbalanced and O(log n) if the tree is balanced.