-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Jocelyn #23
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?
Time - Jocelyn #23
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,108 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def add_help(current, new_node) | ||
return new_node if current.nil? | ||
|
||
new_node.key <= current.key ? | ||
current.left = add_help(current.left , new_node): | ||
current.right = add_help(current.right, new_node) | ||
|
||
return current | ||
end | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_help(@root, TreeNode.new(key,value)) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: log(n) | ||
# Space Complexity: O(1) | ||
def help_find(current, key) | ||
Comment on lines
+36
to
+38
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. 👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced (time as well) and O(n) if the tree is unbalanced. |
||
return nil if current.nil? | ||
|
||
if current.key == key | ||
return current.value | ||
elsif current.key < key | ||
current = current.right | ||
else | ||
current = current.left | ||
end | ||
|
||
return help_find(current,key) | ||
end | ||
|
||
def find(key) | ||
raise NotImplementedError | ||
return help_find(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def help_inorder(current) | ||
Comment on lines
+56
to
+58
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. 👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced. |
||
result = [] | ||
return [] if current.nil? | ||
result += help_inorder(current.left) | ||
result.push({:key=>current.key, :value => current.value}) | ||
result += help_inorder(current.right) | ||
return result | ||
end | ||
|
||
def inorder | ||
raise NotImplementedError | ||
return help_inorder(@root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: log(n) | ||
# Space Complexity: O(1) | ||
def help_preorder(current) | ||
Comment on lines
+71
to
+73
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. 👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced. |
||
result = [] | ||
return [] if current.nil? | ||
result.push({:key=>current.key, :value => current.value}) | ||
result += help_preorder(current.left) | ||
result += help_preorder(current.right) | ||
return result | ||
end | ||
|
||
def preorder | ||
raise NotImplementedError | ||
return help_preorder(@root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: log(n) | ||
# Space Complexity: O(1) | ||
def help_postorder(current) | ||
Comment on lines
+86
to
+88
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. 👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced. |
||
result = [] | ||
return [] if current.nil? | ||
result += help_postorder(current.left) | ||
result += help_postorder(current.right) | ||
result.push({:key=>current.key, :value => current.value}) | ||
return result | ||
end | ||
|
||
def postorder | ||
raise NotImplementedError | ||
return help_postorder(@root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: log(n) | ||
# Space Complexity: O(1) | ||
def height_helper(current) | ||
num, left, right = 0, 0, 0 | ||
return 0 if current.nil? | ||
left += height_helper(current.left) | ||
right += height_helper(current.right) | ||
num += 1 | ||
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. Why not just set num to 1 in line 104? |
||
|
||
if left == right | ||
return num + right | ||
elsif left > right | ||
return num + left | ||
else | ||
return num + right | ||
end | ||
end | ||
|
||
def height | ||
raise NotImplementedError | ||
return 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.
👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced (time as well) and O(n) if the tree is unbalanced.