File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments