Skip to content

Conversation

@seaweeddol
Copy link

did not implement height successfully

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.

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.

Comment on lines +33 to +43
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

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:

Suggested change
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

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.

Suggested change
return current.value if current.key == key

Comment on lines 75 to 78
# Time Complexity:
# Space Complexity:
# left, root, right
def inorder

Choose a reason for hiding this comment

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

👍 Time/space complexities?

Comment on lines 91 to 93
# Time Complexity:
# Space Complexity:
def preorder

Choose a reason for hiding this comment

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

👍 Time/space complexities?

Comment on lines 106 to 108
# Time Complexity:
# Space Complexity:
def postorder

Choose a reason for hiding this comment

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

👍 Time/space complexities?

Comment on lines 120 to 122
# Time Complexity:
# Space Complexity:
def height

Choose a reason for hiding this comment

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

👍 Time/space complexities?

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.

2 participants