mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +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) {
|
||||
// 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;
|
||||
if (node) {
|
||||
|
||||
// Make current node to be a parent for new left one.
|
||||
if (this.left) {
|
||||
this.left.parent = this;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Make current node to be a parent for new right one.
|
||||
if (node) {
|
||||
this.right.parent = this;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user