Git Product home page Git Product logo

Comments (3)

amejiarosario avatar amejiarosario commented on May 22, 2024

Thanks for commenting @beblueblue! You are right about losing the left node in the case of a LL rotation. However, the use case for rotations is to balance a tree:

E.g.

 1                                 1
  \                                 \
   2*                                3
    \    --left-rotation(2)->      /  \
     3                           2*    4
      \
       4

If you have a tree with a left node (as you mentioned) it means that is balanced and it doesn't need to have a rotation.

  1*   
   \   
    2  
  /  \ 
 3    4

Do you have a use case where it needed to do a LL rotation on an already balanced tree?

from dsa.js-data-structures-algorithms-javascript.

beblueblue avatar beblueblue commented on May 22, 2024

Thanks for commenting @beblueblue! You are right about losing the left node in the case of a LL rotation. However, the use case for rotations is to balance a tree:

E.g.

 1                                 1
  \                                 \
   2*                                3
    \    --left-rotation(2)->      /  \
     3                           2*    4
      \
       4

If you have a tree with a left node (as you mentioned) it means that is balanced and it doesn't need to have a rotation.

  1*   
   \   
    2  
  /  \ 
 3    4

Do you have a use case where it needed to do a LL rotation on an already balanced tree?

Thank you for your prompt reply.

Now, Let's make a preset, we have a tree.

 16*                                  16*
 / \      --- add two node(8, 2)-->   / \
4  32                                4  32
                                    / \
                                   2  8

The tree is still balanced. The next step, we add the node 1.

     16*                                       16*
     / \                                       / \
    4   32      --- add the node (1)  -->     4  32
   / \                                       / \
  2   8                                     2   8
                                           /
                                          1

In this case that Node16.balanceFactor is 2, should we make the RR rotation for the root(16)?
If we have to do it, we use the function.

rightRotation(16)
function rightRotation(node) {
    const newParent = node.left; // E.g., node 4
    const grandparent = node.parent; // E.g., undefiend 
  
    swapParentChild(node, newParent, grandparent);
  
    newParent.setRightAndUpdateParent(node);
    // we will lose the node 8!
    node.setLeftAndUpdateParent(null);
  
    return newParent;
  }

Now, our tree became the one below.

    4                                            4
   /  \                                         / \
  2   16        --- the correct tree  -->      2  16
 /      \                                     /   / \
1       32                                   1   8  32
rightRotation(16); -- to get right tree;
function rightRotation(node) {
    const newParent = node.left; // E.g., node 4
    const grandparent = node.parent; // E.g., undefiend 
    const previousRight = newParent.right || null;
  
    swapParentChild(node, newParent, grandparent);
  
    newParent.setRightAndUpdateParent(node);
    // we should make the node-8 to be the left of the node-16
    node.setLeftAndUpdateParent(previousRight);
  
    return newParent;
  }

Last and foremost, I learn a lot form your article. Thank you!

from dsa.js-data-structures-algorithms-javascript.

amejiarosario avatar amejiarosario commented on May 22, 2024

Thank you so much @beblueblue for taking the time and providing an example. I made the fixes and added some tests. Let me know if you find anything else.

from dsa.js-data-structures-algorithms-javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.