-
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
Ren - Fire #29
base: master
Are you sure you want to change the base?
Ren - Fire #29
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,42 +16,140 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: O(log n) if balanced, otherwise O(n) where n is number of nodes | ||
# Space Complexity: O(n), n is height | ||
def add(key, value = nil) | ||
|
||
new_node = TreeNode.new(key, value) | ||
|
||
if @root.nil? | ||
@root = new_node | ||
else | ||
add_helper(@root, new_node) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
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 | ||
|
||
# Time Complexity: O(log n) again if balanced | ||
# Space Complexity: O(n) again | ||
def find(key) | ||
Comment on lines
+44
to
46
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 | ||
if @root.nil? | ||
return @root | ||
else | ||
return find_helper(@root, key) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current, key) | ||
if current.nil? | ||
return nil | ||
elsif key < current.key | ||
return find_helper(current.left, key) | ||
elsif key > current.key | ||
return find_helper(current.right, key) | ||
else | ||
return current.value | ||
end | ||
end | ||
|
||
# Time Complexity: O(n), n is number of nodes | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+66
to
68
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_node_arr = [] | ||
current = @root | ||
|
||
return tree_node_arr if @root.nil? | ||
|
||
inorder_helper(current, tree_node_arr) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(current, arr) | ||
# traverse the left subtree | ||
# visit the current node | ||
# traverse the right subtree | ||
|
||
if current | ||
inorder_helper(current.left, arr) | ||
arr.push({key: current.key, value: current.value}) | ||
inorder_helper(current.right, arr) | ||
end | ||
return arr | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+90
to
92
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_node_arr = [] | ||
current = @root | ||
|
||
return tree_node_arr if current.nil? | ||
|
||
preorder_helper(current, tree_node_arr) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current, arr) | ||
# visit the current node | ||
# traverse the left subtree | ||
# traverse the right subtree | ||
|
||
if current | ||
arr.push({key: current.key, value: current.value}) | ||
preorder_helper(current.left, arr) | ||
preorder_helper(current.right, arr) | ||
end | ||
return arr | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+115
to
117
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_node_arr = [] | ||
current = @root | ||
|
||
return tree_node_arr if @root.nil? | ||
postorder_helper(current, tree_node_arr) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current, arr) | ||
# traverse the left subtree | ||
# traverse the right subtree | ||
# visit the current node | ||
|
||
if current | ||
postorder_helper(current.left, arr) | ||
postorder_helper(current.right, arr) | ||
arr.push({key: current.key, value: current.value}) | ||
end | ||
return arr | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) if balanced | ||
def height | ||
Comment on lines
+138
to
140
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 | ||
return 0 if @root.nil? | ||
current = @root | ||
|
||
height_helper(current) | ||
end | ||
|
||
def height_helper(current) | ||
return 0 if current.nil? | ||
|
||
return [(height_helper(current.left) + 1), (height_helper(current.right) + 1)].max | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
|
@@ -64,3 +162,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.
👍