diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index 44c9390e41..ca0c27f3cf 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -167,11 +167,13 @@ export default class BinaryTreeNode { if (this.left && this.nodeComparator.equal(this.left, nodeToReplace)) { this.left = replacementNode; + this.left.parent = this; return true; } if (this.right && this.nodeComparator.equal(this.right, nodeToReplace)) { this.right = replacementNode; + this.right.parent = this; return true; } diff --git a/src/data-structures/tree/__test__/BinaryTreeNode.test.js b/src/data-structures/tree/__test__/BinaryTreeNode.test.js index 8eeda4dda5..4267e48edb 100644 --- a/src/data-structures/tree/__test__/BinaryTreeNode.test.js +++ b/src/data-structures/tree/__test__/BinaryTreeNode.test.js @@ -91,6 +91,7 @@ describe('BinaryTreeNode', () => { expect(rootNode.replaceChild(rootNode.right, rootNode.right.right)).toBe(true); expect(rootNode.right.value).toBe(5); + expect(rootNode.right.parent).toBe(rootNode); expect(rootNode.right.right).toBeNull(); expect(rootNode.traverseInOrder()).toEqual([1, 2, 5]);