-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Jessica #29
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
base: master
Are you sure you want to change the base?
Space - Jessica #29
Conversation
CheezItMan
left a comment
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.
Overall well done, but you have a bug in your add method. Take a look at my comments and let me know what questions you have.
| if @root == nil | ||
| @root = TreeNode.new(key, value) | ||
| return | ||
| end | ||
|
|
||
| node = add_helper(@root, key) | ||
| if key <= node.key | ||
| node.left = TreeNode.new(key, value) | ||
| else | ||
| node.right = TreeNode.new(key, value) | ||
| end |
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 doesn't actually work, you only can construct a 3-node tree your way.
Consider:
| if @root == nil | |
| @root = TreeNode.new(key, value) | |
| return | |
| end | |
| node = add_helper(@root, key) | |
| if key <= node.key | |
| node.left = TreeNode.new(key, value) | |
| else | |
| node.right = TreeNode.new(key, value) | |
| end | |
| @root = add_helper(@root, key, value) | |
| end | |
| def add_helper(current, key, value) | |
| return TreeNode.new(key, value) if current.nil? | |
| if key <= current.key | |
| current.left = add_helper(current.left, key, value) | |
| else | |
| current.right = add_helper(current.right, key, value) | |
| end | |
| return current | |
| end |
| return current.value if current.key == key | ||
| find_helper(current.left, key) | ||
| elsif key > current.key | ||
| return current.value if current.key == 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.
This can't be true.
| return current.value if current.key == key |
| # Time Complexity: | ||
| # Space Complexity: | ||
| # left, root, right | ||
| 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/space complexities?
| # Time Complexity: | ||
| # 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.
👍 Time/space complexities?
| # Time Complexity: | ||
| # 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.
👍 Time/space complexities?
| # 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 complexities?
did not implement height successfully