Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views3 pages

P5 BinaryTree

The document describes an implementation of an AVL tree in C with functions for insertion, rotation, finding the balance factor, and a preorder traversal. It includes the full code for an AVL tree program that takes user input to insert multiple elements into an initially empty AVL tree and then performs a preorder traversal to print out the elements.

Uploaded by

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

P5 BinaryTree

The document describes an implementation of an AVL tree in C with functions for insertion, rotation, finding the balance factor, and a preorder traversal. It includes the full code for an AVL tree program that takes user input to insert multiple elements into an initially empty AVL tree and then performs a preorder traversal to print out the elements.

Uploaded by

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

EXP No.

: 07
Kunal Shantaram Bhoi
(2022600007)
CSE (AIML)
Batch : A
--------------------------------------------------------------------------------
Problem Statement :
Insertion (AVL Tree)
--------------------------------------------------------------------------------
Program :

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
int height;
};

int height(struct Node* node) {


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

int max(int a, int b) {


return (a > b) ? a : b;
}

struct Node* newNode(int data) {


struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
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;

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;

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


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

return y;
}

int getBalance(struct Node* node) {


if (node == NULL)
return 0;
return height(node->left) - height(node->right);
}

struct Node* insert(struct Node* node, int data) {

if (node == NULL)
return newNode(data);

if (data < node->data)


node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
else
return node;

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

int balance = getBalance(node);

if (balance > 1 && data < node->left->data) //LL


return rightRotate(node);

if (balance < -1 && data > node->right->data) //RR


return leftRotate(node);

if (balance > 1 && data > node->left->data) { //LR


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

if (balance < -1 && data < node->right->data) { //RL


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

void preOrder(struct Node* root) {


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

int main() {
struct Node* root = NULL;
int n, data;

printf("Enter the number of elements to insert: ");


scanf("%d", &n);

printf("Enter the elements to insert into AVL tree: ");


for (int i = 0; i < n; i++) {
scanf("%d", &data);
root = insert(root, data);
}

printf("Pre-order traversal of the AVL tree: ");


preOrder(root);

return 0;
}

-------------------------------------------------------------------------------------
Output :

You might also like