In C++, Write a function to apply left or right rotations to a binary search tre
ID: 3678253 • Letter: I
Question
In C++, Write a function to apply left or right rotations to a binary search tree based on the height of the left and right sub-trees of the root. The function should first determine if a binary search tree is height balanced, and if not, rotate the tree until it is. Your algorithm may need to apply a left or right rotation multiple times. You will not need to apply both a left and right rotation to any tree. The function should return the root of the tree.
Assignment 8 - Part A-Co... t | 1) 15Mar, 16:31 fn Computer Science Moo x https://moodle cs.color ×V https://moodle.cs.color × MCourse: CSCI 2270-Hoexy Assignment 8 PartA-C X ¢ | https://moodle.cs.colorado.edu/mod/quiz/attempt.php?attempt-91649&page;=2&scrollpos;=0#q2 (2) New messages from CS-Moodle cu- Engineering Computer Science- English-United States (en-us)" Amanda Tran struct TreeNode int key; TreeNode leftChild; TreeNode rightChild; TreeNode "parent; 3: Example: Input Tree: 15 8 18 5 12 Expected Output: 8 5 15 3 6 12 18Explanation / Answer
TreeNode* CheckHeightAndRotateLeft(TreeNode *k2);{
SBBSTNode *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max(height(k2->left), height(k2->right)) + 1;
k1->height = max(height(k1->left), k2->height) + 1;
return k1;
}
/* Rotate binary tree node with right child */
TreeNode* CheckHeightAndRotateRight(TreeNode *k1);
{
SBBSTNode *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max(height(k1->left), height(k1->right)) + 1;
k2->height = max(height(k2->right), k1->height) + 1;
return k2;
}