We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This challenge took me more than 2 days... unbelievable. I was trying to use recursion but I didn't have the option to write another function. 2 days later, I finally realize how to use the balance factor to determine how to rotate. I think my method is a little strange, but I am glad it works. I spend 4 hours debugging case 10 because I could not draw out the tree and had to keep guessing what was wrong with it.
My problem with case 10 was, I did not properly update my height after rotating, resulting in my tree having 2 balance factor that was -2 and 2. After the first balance, the 2nd one should become 1 depth lower.
Tips: definitely rotate base on balance factor. This method is very efficient in my opinion. 1 or 2 rotation and the insertion becomes perfectly balanced.
The example tells you exactly how to rotate it
Check for balanced insertion.
Self Balancing Tree
You are viewing a single comment's thread. Return to all comments →
This challenge took me more than 2 days... unbelievable. I was trying to use recursion but I didn't have the option to write another function. 2 days later, I finally realize how to use the balance factor to determine how to rotate. I think my method is a little strange, but I am glad it works. I spend 4 hours debugging case 10 because I could not draw out the tree and had to keep guessing what was wrong with it. My problem with case 10 was, I did not properly update my height after rotating, resulting in my tree having 2 balance factor that was -2 and 2. After the first balance, the 2nd one should become 1 depth lower. Tips: definitely rotate base on balance factor. This method is very efficient in my opinion. 1 or 2 rotation and the insertion becomes perfectly balanced. The example tells you exactly how to rotate it Check for balanced insertion.