Skip to content

fix(tree): preserve parent pointer in replaceChild#2117

Open
DeoJin wants to merge 1 commit intotrekhleb:masterfrom
DeoJin:fix/binary-tree-replacechild-parent
Open

fix(tree): preserve parent pointer in replaceChild#2117
DeoJin wants to merge 1 commit intotrekhleb:masterfrom
DeoJin:fix/binary-tree-replacechild-parent

Conversation

@DeoJin
Copy link

@DeoJin DeoJin commented Mar 17, 2026

Summary

  • set the replacement node's parent when BinaryTreeNode.replaceChild() swaps a child reference
  • add a regression assertion covering the parent pointer after replacement

Validation

  • npx eslint src/data-structures/tree/BinaryTreeNode.js src/data-structures/tree/__test__/BinaryTreeNode.test.js
  • npx -y @babel/node --presets @babel/preset-env -e "const assert = require('assert'); const BinaryTreeNode = require('./src/data-structures/tree/BinaryTreeNode').default; const root = new BinaryTreeNode(2); const oldRight = new BinaryTreeNode(3); const replacement = new BinaryTreeNode(5); oldRight.setRight(replacement); root.setRight(oldRight); assert.strictEqual(root.replaceChild(root.right, root.right.right), true); assert.strictEqual(root.right, replacement); assert.strictEqual(root.right.parent, root); console.log('binary-tree-replacechild-parent-ok');"

Closes #1102.

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