-
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
Branches - Erika #45
base: master
Are you sure you want to change the base?
Branches - Erika #45
Conversation
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.
You've got some bugs and some issues with time/space complexity. Take a look at my comments and suggestions and let me know if you have questions. It looks like you had trouble debugging this. You are welcome to use my office hours or the online TA Matt, if you like for additional support.
lib/tree.rb
Outdated
Erika Maust <[email protected]> | ||
9:13 PM (1 minute ago) | ||
to erika.maust |
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.
Erika Maust <erika.maust@gmail.com> | |
9:13 PM (1 minute ago) | |
to erika.maust |
lib/tree.rb
Outdated
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) |
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.
👍 , just what's the space/time complexity?
lib/tree.rb
Outdated
@root = TreeNode.new(key,value) | ||
else | ||
current = @root | ||
while true |
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.
This loop is going to run forever...
lib/tree.rb
Outdated
# Time Complexity: Because it's recursive I want to say O log(n) | ||
# Space Complexity: O log (n) if it's balanced, and I guess O(n) if it isn't | ||
def find(key) |
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.
Because it's a binary tree the time complexity is O(log n) for a balanced tree and O(n) if it's not a balanced tree.
lib/tree.rb
Outdated
# Time Complexity: I think time and space are both O(n)? | ||
# Space Complexity: | ||
def inorder |
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.
👍
lib/tree.rb
Outdated
# Time Complexity: I feel like both are O(n) since each item has to be traversed, even if it's recursively? | ||
# Space Complexity: | ||
def preorder |
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.
👍
lib/tree.rb
Outdated
# Time Complexity: Same as above, O(n)? | ||
# Space Complexity: | ||
def postorder |
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.
👍
lib/tree.rb
Outdated
# Time Complexity: I'm not sure about time and space here. I think O(n) | ||
# Space Complexity: | ||
def height |
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.
Time complexity is O(n) since you visit each node once. Space complexity depends on the height of the tree so it's O(log n) for a balanced tree and O(n) for an unbalanced tree.
lib/tree.rb
Outdated
current.left = TreeNode.new(key,value) | ||
end | ||
else | ||
if [email protected]? |
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.
What is @current
?
if !@current.right.nil? | |
if !current.right.nil? |
lib/tree.rb
Outdated
preorder_helper(current_node.left, list) | ||
preorder_helper(current_node.right, list) |
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.
preorder_helper(current_node.left, list) | |
preorder_helper(current_node.right, list) | |
postorder_helper(current_node.left, list) | |
postorder_helper(current_node.right, list) |
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.
Nice work, you hit the learning goals here. Well done Erika!
@@ -8,6 +8,87 @@ def initialize(key, val) | |||
@left = nil | |||
@right = nil | |||
end | |||
|
|||
def add(key, value) |
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.
Nice adding these methods to the TreeNode class.
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def add(key, value) |
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.
👍 , time complexity is right if the tree is balanced.
# Time Complexity: O (log n) | ||
# Space Complexity: O(1) | ||
def find(key) |
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.
👍 , time complexity is right if the tree is balanced.
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def inorder |
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.
👍
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def preorder |
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.
👍
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def postorder |
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.
👍
# # Time Complexity: | ||
# # Space Complexity: | ||
def height |
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.
👍 , time & space complexity?
No description provided.