Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LL Rotation, lose the newParent previous right node; RR Rotation too. #30

Closed
@beblueblue

Description

@beblueblue

If the newParent.left previous is exist, we will lose it;

functionleftRotation(node){constnewParent=node.right;// E.g., node 3constgrandparent=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);returnnewParent;}functionsetLeftAndUpdateParent(node){this.left=node;if(node){node.parent=this;node.parentSide=LEFT;}}

If we do this:newParent.setLeftAndUpdateParent(node); inleftRotation, and
this.left = node; insetLeftAndUpdateParent,
we will lose the left of newParent.

I think the above should write as follow.

functionleftRotation(node){constnewParent=node.right;// E.g., node 3constgrandparent=node.parent;// E.g., node 1constpreviousLeft=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);returnnewParent;}

Add this:const previousLeft = newParent.left; andnode.setRightAndUpdateParent(previousLeft);
right? Maybe I am wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp