I am trying to balance a tree by height (an AVI tree).
When I give it a tree like this:
Pre Order Traversal:
15 8 5 3 6 12 18
It works fine, but when I test it using this tree:
Pre Order Traversal:
10 5 15 12 20 19 25 23 30 29 35
and I get:
Pre Order Traversal:
15 10 5 12 20 19 25 23 30 29 35
In your CheckHeightAndRotate, you need to add a while loop right after implementing bal_factor to check if the bal_factor is still > 1 or < -1. Then put bal_factor = diff (temp); as the first thing in the while loop. This may seem strange, but this will allow the function to properly fix trees that are heavily lopsided to one side or another by repeatedly checking if the bal_factor is still to big or small.