-
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
Conversation
One for empty tree, one for tree_with_nodes
Add a test for the height 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.
Nicely done, all the methods work, but you do have issues with time/space complexity. Take a look at my comments and let me know what questions you have.
lib/tree.rb
Outdated
# Time Complexity: O(logn) in worst case scenario | ||
# Space Complexity: O(1) always just adding 1 new node | ||
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.
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?
lib/tree.rb
Outdated
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Similar issues with time/space to add
lib/tree.rb
Outdated
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Space complexity is O(n) since you're building an array!
# Time Complexity: same as inorder | ||
# Space Complexity: same as inorder | ||
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.
Same issues as inorder!
# Time Complexity: same as inorder | ||
# Space Complexity: same as inorder | ||
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.
Similar issues
lib/tree.rb
Outdated
# 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 comment
The 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?
No description provided.