Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81253e8

Browse files
committedApr 6, 2018
Fix binary tree node.
1 parent 9eefd13 commit 81253e8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
 

‎src/data-structures/tree/BinaryTreeNode.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,36 @@ export default class BinaryTreeNode {
3131
}
3232

3333
setLeft(node) {
34+
// Reset parent for left node since it is going to be detached.
35+
if (this.left) {
36+
this.left.parent = null;
37+
}
38+
39+
// Attach new node to the left.
3440
this.left = node;
35-
if (node) {
41+
42+
// Make current node to be a parent for new left one.
43+
if (this.left) {
3644
this.left.parent = this;
3745
}
46+
3847
return this;
3948
}
4049

4150
setRight(node) {
51+
// Reset parent for right node since it is going to be detached.
52+
if (this.right) {
53+
this.right.parent = null;
54+
}
55+
56+
// Attach new node to the right.
4257
this.right = node;
58+
59+
// Make current node to be a parent for new right one.
4360
if (node) {
4461
this.right.parent = this;
4562
}
63+
4664
return this;
4765
}
4866

0 commit comments

Comments
 (0)
Please sign in to comment.