0% found this document useful (0 votes)
35 views7 pages

Ads La5 - Avl Tree Insertion

The document provides a C program for performing insertion and balancing operations on AVL trees, which are self-balancing binary search trees. It outlines the steps for insertion, including handling unbalanced nodes and performing rotations based on four possible cases. The code includes functions for node creation, height calculation, rotations, balance factor retrieval, and preorder traversal of the AVL tree.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views7 pages

Ads La5 - Avl Tree Insertion

The document provides a C program for performing insertion and balancing operations on AVL trees, which are self-balancing binary search trees. It outlines the steps for insertion, including handling unbalanced nodes and performing rotations based on four possible cases. The code includes functions for node creation, height calculation, rotations, balance factor retrieval, and preorder traversal of the AVL tree.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AVL TREE INSERTION

Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA-3

C program to perform various operations i.e., insertions, updating height ,


verifying balance factor and perform rotations on AVL trees.

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.

Steps to follow for insertion:


Let the newly inserted node be q.
 Perform standard BST insertion for q.
 Starting from q, travel up and find the first unbalanced node (if exists).
Let c be the first unbalanced node, b be the child of c that comes on the
path from q to c and a be the grandchild of c that comes on the path
from q to c.
 Re-balance the tree by performing appropriate rotations on the subtree
rooted with z. There can be 4 possible cases that need to be handled as
a, b and c can be arranged in 4 ways.
 Following are the possible 4 arrangements:
1. b is the left child of c and a is the left child of b (Left Left Case)
2. b is the left child of c and a is the right child of b (Left Right Case)
3. b is the right child of c and a is the right child of b (Right Right Case)
4. b is the right child of c and a is the left child of b (Right Left Case)

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;
};

int height(struct Node *N)


{
if (N == NULL)
return 0;
return N->height;
}

int max(int a, int b)


{
return (a > b)? a : b;
}
struct Node* newNode(int key)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}

struct Node *rightRotate(struct Node *y)


{
struct Node *x = y->left;
struct Node *T2 = x->right;

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;
}

// Get Balance factor of node N


int getBalance(struct Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

struct Node* insert(struct Node* node, int key)


{
if (node == NULL)
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 // Equal keys are not allowed in BST
return node;

node->height = 1 + max(height(node->left),
height(node->right));

int balance = getBalance(node);

if (balance > 1 && key < node->left->key)


return rightRotate(node);

if (balance < -1 && key > node->right->key)


return leftRotate(node);

if (balance > 1 && key > node->left->key)


{
node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key)


{
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void preOrder(struct Node *root)


{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}

int main()
{
struct Node *root = NULL;
int a[6];

printf("Enter 6 nodes: ");


for(int i=0; i<6;i++)
{
scanf("%d", &a[i]);
}

for(int i=0; i<6;i++)


{
root = insert(root, a[i]);
}

printf("Preorder traversal of the constructed AVL"


" tree is \n");
preOrder(root);

return 0;
}

You might also like