Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A question about balance function in AVL #33

Closed
beblueblue opened this issue Aug 27, 2019 · 3 comments · Fixed by #34
Closed

A question about balance function in AVL #33

beblueblue opened this issue Aug 27, 2019 · 3 comments · Fixed by #34

Comments

@beblueblue
Copy link

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.

@amejiarosario
Copy link
Owner

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?

@amejiarosario
Copy link
Owner

amejiarosario commented Aug 27, 2019

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

@beblueblue
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants