trekhleb / javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
MIT License
185.13k stars 29.86k forks source link

binary tree node #964

Closed alifhs closed 1 year ago

alifhs commented 1 year ago
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;

    // Make current node to be a parent for new left one.
    if (this.left) {
      this.left.parent = this;
    }

    return this;
  }

I think this

 if (this.left) {
      this.left.parent = null;
    }

portion of the code is redundant. Since we are assigning this.left = node, the previous node is going to be garbage collected and we don't need to explicitly assign this.left.parent = null. Correct me If I'm wrong.