Build AVL tree with c language
- User
Build AVL tree with c language
- Assistant
Build AVL tree with c language
Here is an example of how you can build an AVL tree using the C language:
#include <stdio.h>
#include <stdlib.h>
// AVL Tree Node
typedef struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
} Node;
// Function to get the height of a node
int height(Node* node) {
if (node == NULL)
return 0;
return node->height;
}
// Function to get the maximum between two integers
int max(int a, int b) {
return (a > b) ? a : b;
}
// Function to create a new AVL tree node
Node* createNode(int key) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->key = key;
newNode->left = newNode->right = NULL;
newNode->height = 1;
return newNode;
}
// Function to rotate the tree right
Node* rotateRight(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
// Function to rotate the tree left
Node* rotateLeft(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
// Function to get the balance factor of a node
int getBalance(Node* node) {
if (node == NULL)
return 0;
return height(node->left) - height(node->right);
}
// Function to insert a key into AVL tree
Node* insertNode(Node* node, int key) {
// Perform the normal BST insertion
if (node == NULL) {
return createNode(key);
}
if (key < node->key) {
node->left = insertNode(node->left, key);
}
else if (key > node->key) {
node->right = insertNode(node->right, key);
}
else {
// Duplicate keys are not allowed in AVL tree
return node;
}
// Update height of the current node
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor of the current node
int balance = getBalance(node);
// Perform rotation if the node is unbalanced
// Left Left Case
if (balance > 1 && key < node->left->key) {
return rotateRight(node);
}
// Right Right Case
if (balance < -1 && key > node->right->key) {
return rotateLeft(node);
}
// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}
return node;
}
// Function to print AVL tree in in-order traversal
void inOrder(Node* root) {
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->key);
inOrder(root->right);
}
}
int main() {
Node* root = NULL;
root = insertNode(root, 30);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 10);
root = insertNode(root, 25);
printf("In-order traversal of AVL tree: ");
inOrder(root);
return 0;
}
In this example, we define the AVL tree structure with a Node struct. The height function calculates the height of a node, and the max function returns the maximum of two integers.
The createNode function creates a new node with the given key. The rotateRight and rotateLeft functions rotate the tree right and left, respectively. The getBalance function calculates the balance factor of a node.
The insertNode function inserts a new node with the given key into the AVL tree. It first performs the normal binary search tree (BST) insertion and then checks if the current node is unbalanced. If it is unbalanced, the appropriate rotation is performed based on the balance factor.
The inOrder function prints the AVL tree in in-order traversal.
In the main function, we create the AVL tree, insert some nodes, and print the tree using in-order traversal.
Not yet included in a published release.