Skip to content

fix(tree): set parent pointer in BinaryTreeNode.replaceChild - #2222

Open
LearnCodeZaid wants to merge 1 commit into
trekhleb:masterfrom
LearnCodeZaid:fix/bst-remove-single-child-2102
Open

LearnCodeZaid wants to merge 1 commit into
trekhleb:masterfrom
LearnCodeZaid:fix/bst-remove-single-child-2102

Conversation

@LearnCodeZaid

Copy link
Copy Markdown

Fixes #1102. Part of #2102.

replaceChild swapped the child pointer but never assigned replacementNode.parent, leaving stale parent links after BST removal of a single-child node and breaking any traversal relying on parent pointers.

Change: two lines assigning replacementNode.parent = this in both left and right branches.

Tests: added issue-2102 repro (insert 8/3/6, remove 3 then 6, assert both children null) plus a replaceChild re-parenting assertion to BinarySearchTree.test.js.

replaceChild swapped the child pointer but never updated
replacementNode.parent, leaving stale parent links after BST
removal of a node with one child (issue 2102) and breaking any
code walking parent pointers (issue 1102).

Adds regression tests for both issues.

Fixes trekhleb#1102
Part of trekhleb#2102
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.

replaceChild method in BinaryTreeNode is not correct

1 participant