amejiarosario / dsa.js-data-structures-algorithms-javascript

🥞Data Structures and Algorithms explained and implemented in JavaScript + eBook

Home Page:https://books.adrianmejia.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A question about balance function in AVL

beblueblue opened this issue · comments

function balance(node) {
  if (node.balanceFactor > 1) {
    // left subtree is higher than right subtree
    if (node.left.balanceFactor > 0) {
      return rightRotation(node);
    } if (node.left.balanceFactor < 0) {
      return leftRightRotation(node);
    }
  } else if (node.balanceFactor < -1) {
    // right subtree is higher than left subtree
    if (node.right.balanceFactor < 0) {
      return leftRotation(node);
    } if (node.right.balanceFactor > 0) {
      return rightLeftRotation(node);
    }
  }
  return node;
}

When the node.balanceFactor > 1 and node.left.balanceFactor = 0, we do nothing with balance function.
As follows:

         32                                            32        
      /     \                                          / 
     8       64*                                      8    
   /  \      / \                                     /  \  
  4   16   48  128    --- remove the node-64 --->   4   16
 / \  / \                                          / \  / \ 
2  6 10 20                                        2  6 10 20

If we use the balance as follows, (when node.left.balanceFactor = 0, we do RR rotation.):

function balance(node) {
  if (node.balanceFactor > 1) {
    // left subtree is higher than right subtree
   if (node.left.balanceFactor < 0) {
      return leftRightRotation(node);
    }
    return rightRotation(node);
  } else if (node.balanceFactor < -1) {
    // right subtree is higher than left subtree
    if (node.right.balanceFactor > 0) {
      return rightLeftRotation(node);
    }
    return leftRotation(node);
  }
  return node;
}

We can get this:

         32                                            32        
      /     \                                          / 
     8       64*                                      8    
   /  \      / \                                     /  \  
  4   16   48  128    --- remove the node-64 --->   4   16
 / \  / \                                          / \  / \ 
2  6 10 20                                        2  6 10 20

                                 8
                              /     \
--- balance the node-32 -->  4       32
                            / \     /
                           2  6   16
                                  / \
                                10  20

Do you think so?


I'd like to make a quest if I may. I want to translate your articles into Chinese. I wish more people could make a look of these wonderful articles! May I?
Thank you very much.

Hi @beblueblue thanks again for testing the code. I'll take a look at AVL tree later today and get back to you on this.

About translating into Chinese, yes! Could you please create a Pull Request with a new folder book-cn and put all the translated articles in there?

About your AVL example, you are removing node 64, but what happened to node 48 and 128? They will remain in the tree unless you delete them explicitly.

You should have something like this

         32                                                32         
      /     \                                           /     \       
     8       64*                                       8       128    
   /  \      / \                                     /  \      /    
  4   16   48  128    --- remove the node-64 --->   4   16   48    
 / \  / \                                          / \  / \           
2  6 10 20                                        2  6 10 20   

I do see the issue when removing: 48

         32         
      /     \       
     8       128    
   /  \          
  4   16       
 / \  / \           
2  6 10 20    

Is not doing rebalancing correctly. I'll fix that

Hi @amejiarosario , when I finished the translation, I will create a Pull Request.
Have a good day.