-
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
Leaves-Yasmin #25
base: master
Are you sure you want to change the base?
Leaves-Yasmin #25
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 |
---|---|---|
|
@@ -17,41 +17,139 @@ def initialize | |
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Space Complexity: o(n) | ||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
def add_helper(current_node, key, value) | ||
return TreeNode.new(key, value) if current_node.nil? | ||
if key < current_node.key | ||
current_node.left = add_helper(current_node.left, key, value) | ||
else | ||
current_node.right = add_helper(current_node.right, key, value) | ||
end | ||
return current_node | ||
end | ||
|
||
|
||
# def add(key, value) | ||
# if @root.nil? | ||
# @root = TreeNode.new(key, value) | ||
# else | ||
# current = @root | ||
# while true | ||
# if key < current.key | ||
# if !current.left.nil? | ||
# current = current.left | ||
# else | ||
# current.left = TreeNode.new(key, value) | ||
# end | ||
# else | ||
# if !current.right.nil? | ||
# current = current.right | ||
# else | ||
# current.right = TreeNode.new(key, value) | ||
# end | ||
# end | ||
# end | ||
# end | ||
# end | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Space Complexity: o(n) | ||
def find(key) | ||
Comment on lines
+61
to
62
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/time complexity is O(log n) if the tree is balanced and O(n) if it's not. |
||
raise NotImplementedError | ||
# raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current_node, key) | ||
return nil if current_node.nil? | ||
if current_node.key == key | ||
return current_node.value | ||
elsif key <= current_node.key | ||
return find_helper(current_node.left, key) | ||
else | ||
return find_helper(current_node.right, key) | ||
end | ||
end | ||
|
||
|
||
|
||
# Time Complexity:o(n) | ||
# Space Complexity:o(n) | ||
def inorder | ||
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. 👍 |
||
raise NotImplementedError | ||
# raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
def inorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
inorder_helper(current_node.left, list) | ||
list << {key:current_node.key, value: current_node.value} | ||
inorder_helper(current_node.right, list) | ||
return list | ||
end | ||
|
||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def preorder | ||
Comment on lines
+97
to
99
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 | ||
# raise NotImplementedError | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
list << { key: current_node.key, value: current_node.value } | ||
preorder_helper(current_node.left, list) | ||
preorder_helper(current_node.right, list) | ||
return list | ||
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 | ||
# raise NotImplementedError | ||
return postorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
postorder_helper(current_node.left, list) | ||
postorder_helper(current_node.right, list) | ||
list << { key: current_node.key, value: current_node.value } | ||
return list | ||
end | ||
|
||
|
||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def height | ||
Comment on lines
+133
to
135
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 your recursive stack depends on the height of the tree the space complexity is O(log n) for balanced trees and O(n) for unbalanced trees. |
||
raise NotImplementedError | ||
# raise NotImplementedError | ||
return height_helper(@root) | ||
end | ||
def height_helper(current_node) | ||
return 0 if current_node.nil? | ||
|
||
left_height = height_helper(current_node.left) | ||
right_height = height_helper(current_node.right) | ||
if left_height > right_height | ||
return left_height += 1 | ||
else | ||
return right_height += 1 | ||
end | ||
end | ||
|
||
|
||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
|
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 space/time complexity is O(log n) if the tree is balanced and O(n) if it's not.