-
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 - Kareha #19
base: master
Are you sure you want to change the base?
Water - Kareha #19
Changes from 7 commits
64f3ddd
815173c
70ae25c
0628f13
380945e
727e30e
9bf8710
f8b6109
3de61c5
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,51 +16,137 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(logn) | ||
def add(key, value = nil) | ||
unless root | ||
@root = TreeNode.new(key, value) | ||
else | ||
add_helper(@root, key, value) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current, key, value) | ||
return TreeNode.new(key, value) unless current | ||
|
||
if key < current.key | ||
current.left = add_helper(current.left, key, value) | ||
else | ||
current.right = add_helper(current.right, key, value) | ||
end | ||
|
||
return current | ||
|
||
end | ||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(logn) | ||
def find(key) | ||
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. 👍 The time/complexities are correct if the tree is balanced and O(n) if unbalanced. |
||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
def find_helper(current, key) | ||
return current unless current | ||
return current.value if key == current.key | ||
|
||
if current.key < key | ||
find_helper(current.right, key) | ||
else | ||
find_helper(current.left, key) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+89
to
91
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 | ||
tree_in_order = [] | ||
return inorder_helper(tree_in_order, @root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(tree, current) | ||
return tree unless current | ||
|
||
inorder_helper(tree, current.left) | ||
tree.push({:key=>current.key, :value=>current.value}) | ||
inorder_helper(tree, current.right) | ||
|
||
return tree | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+106
to
108
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 | ||
tree_pre_order = [] | ||
return preorder_helper(@root, tree_pre_order) | ||
end | ||
|
||
def preorder_helper(current, tree) | ||
return tree unless current | ||
|
||
tree.push({:key=>current.key, :value=>current.value}) | ||
preorder_helper(current.left, tree) | ||
preorder_helper(current.right, tree) | ||
|
||
return tree | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+123
to
125
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 | ||
tree_post_order = [] | ||
return postorder_helper(@root, tree_post_order) | ||
end | ||
|
||
def postorder_helper(current, tree) | ||
return tree unless current | ||
|
||
postorder_helper(current.left, tree) | ||
postorder_helper(current.right, tree) | ||
tree.push({:key=>current.key, :value=>current.value}) | ||
|
||
return tree | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height | ||
Comment on lines
+140
to
142
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 | ||
return height_helper(@root) | ||
end | ||
|
||
def height_helper(current) | ||
return 0 unless current | ||
|
||
left_height = height_helper(current.left) | ||
right_height = height_helper(current.right) | ||
|
||
return left_height < right_height ? right_height + 1 : left_height + 1 | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n*m) | ||
# Space Complexity: O(n) | ||
def bfs | ||
Comment on lines
+156
to
158
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. Because you are using |
||
raise NotImplementedError | ||
q = [] | ||
tree_bfs = [] | ||
return tree_bfs unless @root | ||
|
||
q.push(@root) | ||
|
||
until q.empty? | ||
current = q.shift | ||
|
||
tree_bfs.push({:key=>current.key, :value=>current.value}) | ||
|
||
q.push(current.left) if current.left | ||
q.push(current.right) if current.right | ||
end | ||
|
||
return tree_bfs | ||
|
||
end | ||
|
||
# Useful for printing | ||
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/complexities are correct if the tree is balanced and O(n) if unbalanced.