Skip to content

Conversation

@geli-gel
Copy link

No description provided.

CheezItMan and others added 4 commits August 20, 2019 22:31
Copy link

@CheezItMan CheezItMan left a 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
Comment on lines 19 to 21
# Time Complexity: O(logn) in worst case scenario
# Space Complexity: O(1) always just adding 1 new node
def add(key, value)

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.

Copy link
Author

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?

Comment on lines 37 to 39
# Time Complexity: O(logn) since binary
# Space Complexity: O(1) since adding nothing new
def find(key)

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

Comment on lines 55 to 57
# 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

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!

Comment on lines +72 to 74
# Time Complexity: same as inorder
# Space Complexity: same as inorder
def preorder

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issues as inorder!

Comment on lines +87 to 89
# Time Complexity: same as inorder
# Space Complexity: same as inorder
def postorder

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar issues

Comment on lines 102 to 104
# Time Complexity: O(n) since each node is visited once
# Space Complexity: O(1) since it doesn't make anything new
def height

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants