-
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
Angele Z #41
base: master
Are you sure you want to change the base?
Angele Z #41
Changes from 4 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,105 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logn) in worst case scenario | ||
# Space Complexity: O(1) always just adding 1 new node | ||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
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 | ||
|
||
# Time Complexity: O(logn) since binary | ||
# Space Complexity: O(1) since adding nothing new | ||
def find(key) | ||
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. Similar issues with time/space to |
||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current_node, key) | ||
return nil if current_node.nil? | ||
return current_node.value if key == current_node.key | ||
|
||
if key < current_node.key | ||
find_helper(current_node.left, key) | ||
else | ||
find_helper(current_node.right, key) | ||
end | ||
end | ||
|
||
|
||
# Time Complexity: O(n) since going to each node once, backtracking but still backtracking a smaller number of times than there are nodes, so it depends on the number of nodes no matter what. | ||
# Space Complexity: O(1) - nothing being made | ||
def inorder | ||
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. Space complexity is O(n) since you're building an array! |
||
raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# helper | ||
def inorder_helper(current_node, list) | ||
# left, root, right | ||
return list if current_node.nil? | ||
# recursion! left side first | ||
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: same as inorder | ||
# Space Complexity: same as inorder | ||
def preorder | ||
Comment on lines
+72
to
74
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. Same issues as inorder! |
||
raise NotImplementedError | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current_node, list) | ||
# root, left, right | ||
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: same as inorder | ||
# Space Complexity: same as inorder | ||
def postorder | ||
Comment on lines
+87
to
89
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. Similar issues |
||
raise NotImplementedError | ||
return postorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current_node, list) | ||
# left, right, root | ||
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) since each node is visited once | ||
# Space Complexity: O(1) since it doesn't make anything new | ||
def height | ||
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 method works, but you have recursion and so some space complexity. Predictions? |
||
raise NotImplementedError | ||
return height_helper(@root, 0) | ||
end | ||
|
||
def height_helper(current_node, current_height) | ||
return 0 if current_node.nil? | ||
left_height = height_helper(current_node.left, current_height) | ||
right_height = height_helper(current_node.right, current_height) | ||
if left_height > right_height | ||
return left_height + 1 | ||
else | ||
return right_height + 1 | ||
end | ||
|
||
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.
Actually O(log n) is the best and average case runtime.
Since you're doing recursion, you do incur some 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.
@CheezItMan for the time complexity - would it be O(n) for worst case, and is that possible only if the tree is unbalanced?