- 
                Notifications
    
You must be signed in to change notification settings  - Fork 934
 
Closed
Description
If the newParent.left previous is exist, we will lose it;
function leftRotation(node) {
    const newParent = node.right; // E.g., node 3
    const grandparent = node.parent; // E.g., node 1
    // swap node 1 left child from 2 to 3.
    swapParentChild(node, newParent, grandparent);
    // Update node 3 left child to be 2, and
    // updates node 2 parent to be node 3 (instead of 1).
    newParent.setLeftAndUpdateParent(node);
    // remove node 2 left child (previouly was node 3)
    node.setRightAndUpdateParent(null);
    return newParent;
}
function setLeftAndUpdateParent(node) {
    this.left = node;
    if (node) {
      node.parent = this;
      node.parentSide = LEFT;
    }
  }If we do this: newParent.setLeftAndUpdateParent(node); in leftRotation, and
this.left = node; in setLeftAndUpdateParent,
we will lose the left of newParent.
I think the above should write as follow.
function leftRotation(node) {
    const newParent = node.right; // E.g., node 3
    const grandparent = node.parent; // E.g., node 1
    const previousLeft = newParent.left;
  
    // swap node 1 left child from 2 to 3.
    swapParentChild(node, newParent, grandparent);
  
    // Update node 3 left child to be 2, and
    // updates node 2 parent to be node 3 (instead of 1).
    newParent.setLeftAndUpdateParent(node);
    // remove node 2 left child (previouly was node 3)
    node.setRightAndUpdateParent(previousLeft);
  
    return newParent;
  }Add this: const previousLeft = newParent.left; and node.setRightAndUpdateParent(previousLeft);
right? Maybe I am wrong.
Metadata
Metadata
Assignees
Labels
No labels