-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Nikki V #21
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 - Nikki V #21
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,115 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: best case O(log n) / worst case O(n) | ||
# Space Complexity: O(n) | ||
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 | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
|
||
if @root.nil? | ||
@root = new_node | ||
else | ||
add_helper(@root, new_node) | ||
end | ||
|
||
return @root | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: best case O(log n) / worst case O(n) | ||
# Space Complexity: O(n) | ||
def find(key) | ||
Comment on lines
+45
to
47
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. 👍 , your space complexity is correct if the tree is unbalanced. |
||
raise NotImplementedError | ||
current = @root | ||
return nil if current.nil? | ||
|
||
if current.key == key | ||
return current.value | ||
else | ||
if key < current.key | ||
return current.left.value | ||
else | ||
return current.right.value | ||
end | ||
end | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder_helper(current, order) | ||
Comment on lines
+62
to
+64
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.nil? | ||
return | ||
end | ||
|
||
inorder_helper(current.left, order) if current.left | ||
order << {key: current.key, value: current.value} | ||
inorder_helper(current.right, order) if current.right | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
raise NotImplementedError | ||
order = [] | ||
inorder_helper(@root, order) | ||
return order | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder_helper(current, order) | ||
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.nil? | ||
return | ||
end | ||
|
||
order << {key: current.key, value: current.value} | ||
preorder_helper(current.left, order) if current.left | ||
preorder_helper(current.right, order) if current.right | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
raise NotImplementedError | ||
order = [] | ||
preorder_helper(@root, order) | ||
return order | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder_helper(current, order) | ||
Comment on lines
+98
to
+100
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.nil? | ||
return | ||
end | ||
|
||
postorder_helper(current.left, order) if current.left | ||
postorder_helper(current.right, order) if current.right | ||
order << {key: current.key, value: current.value} | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
raise NotImplementedError | ||
order = [] | ||
postorder_helper(@root, order) | ||
return order | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height_helper(current) | ||
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 current.nil? | ||
return 0 | ||
else | ||
return 1 + [height_helper(current.left), height_helper(current.right)].max | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
height_helper(@root) | ||
end | ||
|
||
# Optional Method | ||
|
@@ -64,3 +139,4 @@ def to_s | |
return "#{self.inorder}" | ||
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.
👍 , your space complexity is correct if the tree is unbalanced.