Skip to content
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

What should be returned by the remove method of the BinarySearchTreeNode? #57

Closed
m-maksyutin opened this issue Jun 8, 2018 · 4 comments
Labels
bug Something isn't working

Comments

@m-maksyutin
Copy link
Contributor

Hi! The remove method returns a BinarySearchTreeNode object. But there is one uncertain moment. What should be contained in the properties of this object, such as the parent, left, right? Are these properties relevant? Or they must be null?

@intern0t
Copy link

intern0t commented Jun 8, 2018

It should be relevant, it is setting a delimiter whether to continue, fall back or if nodeToRemove is actually the last one. The question about whether it must be null depends on the properties left and right. If right is null, that means there are no nodes beyond the current one, nodeToRemove and is the last one.

Sorry, if I am not clear enough.

@m-maksyutin
Copy link
Contributor Author

Thanks for the explanation, @intern0t .

For clarity, please specify these things:

Should these properties of the returned object correspond to the tree state before the node is deleted or after?

For example, is it correct to change the right property here?

Same question to the value property:


Also, if you add this check to the BinarySearchTreeNode.test.js, it will fail:

it('should return removed node containing a value that should have been deleted', () => {
  const bstRootNode = new BinarySearchTreeNode();
  bstRootNode.insert(10);
  bstRootNode.insert(20);
  bstRootNode.insert(30);
  bstRootNode.insert(15);

  const removedNode1 = bstRootNode.remove(20);
  expect(removedNode1.value).toBe(20);
});

@rakshithjk
Copy link

rakshithjk commented Jun 10, 2018

I think that properties of the returned object correspond to the tree state before the node is deleted.

110. nodeToRemove.value = nodeToRemove.right.value;

You have changed the value of nodeToRemove with the value that has to be replaced (in the example you have mentioned, 30).Now when you return nodeToRemove.value it would return 30 whereas 20 was expected. Instead return parent

@trekhleb trekhleb added the bug Something isn't working label Jun 13, 2018
@trekhleb
Copy link
Owner

Yes, you right @m-maksyutin, there is an uncertainty in removal method. To avoid similar confusion I've changed the code so that removal method will return boolean (whether node has been successfully deleted or not). Because returning the node truly brings confusion about its value, left, right and parent properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants