I was wondering whether the AVL-tree or Red-black-tree balancing method (which happens at each node insertion by simple rotation) could be extended to any given binary tree at any stage of the binary tree construction.
I found that following AVL or Red-black rotating rules for any given unbalanced binary tree lead to tie situations in which you can choose at least two way to balance the tree. If you follow AVL rule you'll get same level difference which means there are at least two nodes which you can choose in order to balance the tree.
Apparently, any decision you make for balancing leads to a different balanced tree (perhaps one has a better situation in terms of level difference to the other). I tested this procedure on a simple example, I'm not sure if it's applicable in every case so I want to know:
- if it could be generalized
- if there is an example that proves that it could not be generalized?
Why even think about this situation?
- It's fun to know!
- suppose a binary tree data structure (and you don't want to use AVL or red-black). So you don't want to balance all the time but you may want to balance sometimes in the middle of construction of the tree.
A simple example:
- Suppose our elements consist of {1, 3, 5, 7}
- RR: rotate right
- RL: rotate left
The given binary tree would look like this:
If you balance it with RR (pivot 5) then RL (pivot 3) it'll be as following (same tree if AVL approach used from beginning):