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 {
31
31
}
32
32
33
33
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.
34
40
this . left = node ;
35
- if ( node ) {
41
+
42
+ // Make current node to be a parent for new left one.
43
+ if ( this . left ) {
36
44
this . left . parent = this ;
37
45
}
46
+
38
47
return this ;
39
48
}
40
49
41
50
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.
42
57
this . right = node ;
58
+
59
+ // Make current node to be a parent for new right one.
43
60
if ( node ) {
44
61
this . right . parent = this ;
45
62
}
63
+
46
64
return this ;
47
65
}
48
66
You can’t perform that action at this time.
0 commit comments