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

replaceChild method in BinaryTreeNode is not correct #1102

Open
le-huy-jh opened this issue Jan 31, 2024 · 1 comment
Open

replaceChild method in BinaryTreeNode is not correct #1102

le-huy-jh opened this issue Jan 31, 2024 · 1 comment

Comments

@le-huy-jh
Copy link

le-huy-jh commented Jan 31, 2024

Missing update parent. It should be

    if (!nodeToReplace || !replacementNode) {
      return false;
    }

    if (this.left && this.left === nodeToReplace) {
      this.left = replacementNode;
      replacementNode.parent = this;
      return true;
    }

    if (this.right && this.right === nodeToReplace) {
      this.right = replacementNode;
      replacementNode.parent = this;
      return true;
    }

    return false;
  }
@Prakash-2002
Copy link

explain(nodeToReplace, replacementNode) {
if (!nodeToReplace || !replacementNode) {
return false;
}

if (this.left && this.left === nodeToReplace) {
  this.left = replacementNode;
  replacementNode.parent = this;
  return true;
}

if (this.right && this.right === nodeToReplace) {
  this.right = replacementNode;
  replacementNode.parent = this;
  return true;
}

// Add the following update to set the parent of replacementNode correctly
if (this.left && this.left.explain(nodeToReplace, replacementNode)) {
  return true;
}

if (this.right && this.right.explain(nodeToReplace, replacementNode)) {
  return true;
}

return false;

}

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

No branches or pull requests

2 participants