How do I implement an AVL tree in C++?
To implement an AVL tree in C++, you need to define a node structure and include functions for insertion, deletion, and balancing.
Can you show me the code for the insertion function?
Sure! Here's a basic implementation of the insertion function for an AVL tree:
struct Node {
int key;
Node *left, *right;
int height;
};
Node* insert(Node* node, int key) {
// 1. Perform the normal BST insertion
if (node == nullptr) {
return newNode(key);
}
if (key < node->key) {
node->left = insert(node->left, key);
} else if (key > node->key) {
node->right = insert(node->right, key);
} else {
return node; // Duplicate keys are not allowed
}
// 2. Update height of this ancestor node
node->height = 1 + max(height(node->left), height(node->right));
// 3. Get the balance factor
int balance = getBalance(node);
// 4. If the node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
This function handles the insertion and balancing of the AVL tree.
What about the balancing functions like leftRotate and rightRotate?
The leftRotate and rightRotate functions are crucial for maintaining the balance of the AVL tree. Here’s how you can implement them:
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = 1 + max(height(y->left), height(y->right));
x->height = 1 + max(height(x->left), height(x->right));
// Return new root
return x;
}
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
// Return new root
return y;
}
These functions help in rotating the nodes to maintain the AVL property.
Get this answer and more from ChatGPT in Telegram, try it for free!
Get Started For FreePowered by advanced language models trained on billions of online words and conversations, MagicBuddy delivers friendly and helpful chat on demand.
Ask questions and get answers. Crack jokes and get laughs. Or even get writing ideas and study help. MagicBuddy's conversational intelligence allows for a truly unique and useful chat experience.
Curious? Try these prompts:
MagicBuddy is incredibly easy to use and the responses are lightning fast! 🥇
Impressive! MagicBuddy is my go-to AI assistant. It’s quick, accurate, and user-friendly.
Sending voice messages makes it easier to use in the middle of the day and the answers are super fast.
Love it! A friend of mine told me about it and it has made my life so much easier. It’s like having a helpful friend in my pocket!
I use 2 months already, simply amazing! MagicBuddy delivers answers in a snap. It’s a must-have tool for staying informed!
Game-changer! MagicBuddy’s speed and accuracy are unmatched. It’s my secret weapon for productivity
Say Hello to Your New Telegram BFF - Try MagicBuddy Free!
Get Started For Free