mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +08:00
Fix binary tree node.
This commit is contained in:
parent
9eefd13615
commit
81253e8a5d
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user