AVL Tree Balancing Problem

binary treedata structures

Take the following tree:

          50                 
        40 
      30 
    20 
  10 

Which node will be balanced first? 50 or 30

Case 1: 50
The tree is

             30
          20    40
       10          50

Case 2: 30
The tree is

              40
            /   \
          20     50
       10    30 

Well, both are AVL Trees, so are both correct?

Best Answer

Your first tree is not an AVL tree. Even minus an element it is not an AVL tree (so we can't even consider it as an AVL tree undergoing balance).

AVL operations are defined on AVL trees to produce a new AVL tree. They are defined in terms of the node that was inserted or deleted.

So, we don't have an AVL tree to start, and we have no information about how we got that tree (was something just inserted or deleted, and if so, what?). In short, I think the question needs to be clarified.