-
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
Fire - Alice D #20
base: master
Are you sure you want to change the base?
Fire - Alice D #20
Changes from all commits
94fec62
263d75c
af90c6e
e2506ff
dfbd0ad
245ae27
050e43c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Settings><!--This file was automatically generated by Ruby plugin. | ||
You are allowed to: | ||
1. Remove rake task | ||
2. Add existing rake tasks | ||
To add existing rake tasks automatically delete this file and reload the project. | ||
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /></RakeGroup></Settings> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,49 +16,133 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) | ||
# Space Complexity: O(1) | ||
def add(key, value = nil) | ||
if @root.nil? | ||
@root = TreeNode.new(key, value) | ||
return @root.value | ||
end | ||
|
||
current = @root | ||
|
||
while key <= current.key || key > current.key | ||
if key == current.key | ||
"This key is already taken." | ||
elsif key < current.key | ||
if current.left.nil? | ||
current.left = TreeNode.new(key,value) | ||
return value | ||
else | ||
current = current.left | ||
end | ||
else | ||
if current.right.nil? | ||
current.right = TreeNode.new(key,value) | ||
return value | ||
else | ||
current = current.right | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
# Time Complexity: | ||
|
||
# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) | ||
# Space Complexity: | ||
def find(key) | ||
Comment on lines
+52
to
54
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. 👍 Correct time complexity given the assumptions space complexity ??? |
||
raise NotImplementedError | ||
current = @root | ||
while current != nil | ||
if current.key == key | ||
return current.value | ||
elsif current.key > key | ||
current = current.left | ||
elsif current.key < key | ||
current = current.right | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
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. 👍 |
||
raise NotImplementedError | ||
array = [] | ||
current = @root | ||
inorder_helper(array, current) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(array, current) | ||
return array if current.nil? | ||
|
||
#left | ||
inorder_helper(array, current.left) | ||
#middle | ||
array << {:key => current.key, :value => current.value} | ||
#right | ||
inorder_helper(array, current.right) | ||
|
||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+88
to
90
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 | ||
array = [] | ||
current = @root | ||
preorder_helper(array, current) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(array, current) | ||
return array if current.nil? | ||
#middle | ||
array << {:key => current.key, :value => current.value} | ||
#left | ||
preorder_helper(array, current.left) | ||
#right | ||
preorder_helper(array, current.right) | ||
|
||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
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. 👍 |
||
raise NotImplementedError | ||
array = [] | ||
current = @root | ||
postorder_helper(array, current) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(array, current) | ||
return array if current.nil? | ||
|
||
#left | ||
postorder_helper(array, current.left) | ||
#right | ||
postorder_helper(array, current.right) | ||
#middle | ||
array << {:key => current.key, :value => current.value} | ||
|
||
end | ||
|
||
# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n) | ||
# Space Complexity: O(1) | ||
def height | ||
Comment on lines
+127
to
129
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 is O(n) and space complexity is O(log n) if the tree is balanced and O(n) otherwise. |
||
raise NotImplementedError | ||
height_helper(@root) | ||
end | ||
|
||
def height_helper(root) | ||
return 0 if root.nil? | ||
[1 + height_helper(root.left), 1 + height_helper(root.right)].max | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
# optional | ||
end | ||
|
||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
|
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.
👍 Correct time complexity given the assumptions