Fix binary tree node.

This commit is contained in:
Oleksii Trekhleb 2018-04-06 07:08:14 +03:00
parent 9eefd13615
commit 81253e8a5d

View File

@ -31,18 +31,36 @@ export default class BinaryTreeNode {
} }
setLeft(node) { setLeft(node) {
// Reset parent for left node since it is going to be detached.
if (this.left) {
this.left.parent = null;
}
// Attach new node to the left.
this.left = node; this.left = node;
if (node) {
// Make current node to be a parent for new left one.
if (this.left) {
this.left.parent = this; this.left.parent = this;
} }
return this; return this;
} }
setRight(node) { setRight(node) {
// Reset parent for right node since it is going to be detached.
if (this.right) {
this.right.parent = null;
}
// Attach new node to the right.
this.right = node; this.right = node;
// Make current node to be a parent for new right one.
if (node) { if (node) {
this.right.parent = this; this.right.parent = this;
} }
return this; return this;
} }