Download as pdf
Download as pdf
You are on page 1of 3
Powered by Al Copilot AVL Tree Rotations and Insertion Let's delve into AVL tree rotations, discuss how to fix specific cases, and provide a C++ implementation for the avLinsert method. 1. Cases of Rotations a. Left-Right Double Rotation (Case 2) * This case occurs when the excess node (in subtree A) is in the left child of the left child of the unbalanced node labeled S. ‘+ We perform a left rotation followed by a right rotation to restore balance. + Diagram: s rN Ac yN BoD © After the rotations: A 1\ Bos WN cop b. Right-Left Double Rotation (Case 3) «This case is symmetrical to the left-right rotation. # The excess node (in subtree B) is in the right child of the left child of the unbalanced node labeled S. # We perform a right rotation followed by a left rotation. + Diagram: s rN ca aN boB © After the rotations: A rN s 8 iN cD 2. C++ Code for avlinsert Method Below is a simplified example of the avitnsert method in C++: include using namespace std; struct TreeNode { int datas TreeNode* left; TreeNode* right; int height; // Height of the subtree rooted at this node // Calculate the height of a node int getHeight(TreeNode* node) { return (node == nullptr) ? -1 : node-rheight; d // Perform a single left rotation TreeNode* leftRotate(TreeNode* root) { TreeNode* newRoot = root->right; root->right = newRoot->left; newRoot->left = roots root-sheight = max(getHeight(root->left), getHeight(root->right)) + 15 newRoot->height = max(getHeight(newRoot->left), getHeight (newRoot->right)) + 13 return newRoot; y 11 Perform a single right rotation TreeNodet rightRotate(TreeNode* root) { TreeNode* newRoot = root->left; root->left = newRoot->rights nenRoot->right = root; root-sheight = max(getHeight(root->left), getHeight(root->right)) + 1; newRoot->height = max(getHeight(newRoot->left), getHeight (newRoot->right)) + 1; return newRoot; y // Perform AVL tree insertion TreeNodet aviInsert(TreeNode* root, int value) { Af (root == nullptr) { return new TreeNode{value, nullptr, nullptr, 0}; Lf (value < root->data) { root->left = avilnsert(root-rleft, value); } else { root->right = aviInsert(root->right, value); } root-sheight = max(getHeight(root->left), getHeight(root->right)) + 15 J/ Check balance factor and perform rotations if needed int balanceFactor = getHeight(root->left) - getHeight(root->right); if (balanceFactor > 1) { if (value < root->left->data) { // Left-Left case return rightRotate(rot); } else { // Left-Right case root->left = leftRotate(root->left) ; return rightRotate(root) } else if (balanceFactor < -1) { if (value > root->right->data) { // Right-Right case return leftRotate(roct); } else { // Right-Left case root->right = rightRotate(root->right); return leftRotate(root); } } return root; } J/ In-order traversal for testing void inOrder(TreeNode* root) { AF (root) { inorder (root->left); cout << root-sdata << inOrder(root->right) ; y int main() { Treeliode* root = nuliptrs root = aviinsert(root, 10); root = aviinsert(root, 20); root = avlinsert(root, 30); root = avlinsert(root, 40); root = avlinsert(root, 50); cout << "In-order traversal: "3 indrder (root); cout << endl; return @; y Feel free to explore AVL trees further or modify the code as needed! @ [1] https://www.geeksforgecks.org/avle tree-set-1-insertion

You might also like