Ads La5 - Avl Tree Insertion
Ads La5 - Avl Tree Insertion
Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA-3
Explanation: AVL Trees are self-balancing Binary Search Trees with the
additional condition that the difference between heights of left and right
subtrees cannot be more than one for all nodes. For insertion in an AVL tree,
the BST tree insertion rules apply, with additional functions to maintain the AVL
tree condition.
In our code, to represent the final AVL tree, we are displaying the preorder
form of the tree.
Code
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};
x->right = y;
y->left = T2;
// Updating heights
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
return x;
}
struct Node *leftRotate(struct Node *x)
{
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
// Updating heights
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
return y;
}
node->height = 1 + max(height(node->left),
height(node->right));
return node;
}
int main()
{
struct Node *root = NULL;
int a[6];
return 0;
}