-
Notifications
You must be signed in to change notification settings - Fork 44
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
Water - Beatrice #25
base: master
Are you sure you want to change the base?
Water - Beatrice #25
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,87 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) - call stack | ||
def add(key, value) | ||
raise NotImplementedError | ||
if @root.nil? | ||
@root = TreeNode.new(key, value) | ||
return true | ||
end | ||
|
||
add_helper(key, value, @root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
raise NotImplementedError | ||
def add_helper(key, value, current) | ||
return TreeNode.new(key, value) if current.nil? | ||
|
||
if key < current.key | ||
current.left = add_helper(key, value, current.left) | ||
else | ||
current.right = add_helper(key, value, current.right) | ||
end | ||
|
||
return current | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
raise NotImplementedError | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) - call stack | ||
def find(key, current = @root) | ||
Comment on lines
+42
to
+44
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 time & space complexities are both O(log n) if the tree is balanced and O(n) if unbalanced. |
||
return nil if current.nil? | ||
return current.value if current.key == key | ||
|
||
if key < current.key | ||
return find(key, current.left) | ||
else | ||
return find(key, current.right) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
raise NotImplementedError | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(current = @root, answer = []) | ||
Comment on lines
+55
to
+57
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 current | ||
inorder(current.left, answer) | ||
answer.push({key: current.key, value: current.value}) | ||
inorder(current.right, answer) | ||
end | ||
|
||
return answer | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
raise NotImplementedError | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(current = @root, answer = []) | ||
Comment on lines
+68
to
+70
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 current | ||
answer.push({key: current.key, value: current.value}) | ||
preorder(current.left, answer) | ||
preorder(current.right, answer) | ||
end | ||
|
||
return answer | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(current = @root, answer = []) | ||
Comment on lines
+80
to
+82
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 current | ||
postorder(current.left, answer) | ||
postorder(current.right, answer) | ||
answer.push({key: current.key, value: current.value}) | ||
end | ||
|
||
return answer | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height(current = @root) | ||
Comment on lines
+92
to
+94
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 time complexity here is right, but the space complexity is O(log n) if the tree is balanced and O(n) if it's not. |
||
unless current | ||
return 0 | ||
end | ||
|
||
return 1 + [height(current.left), height(current.right)].max | ||
end | ||
|
||
# Optional Method | ||
|
@@ -63,4 +110,4 @@ def bfs | |
def to_s | ||
return "#{self.inorder}" | ||
end | ||
end | ||
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.
👍 The time & space complexities are both O(log n) if the tree is balanced and O(n) if unbalanced.