0% found this document useful (0 votes)
12 views12 pages

Exp-7 Swethaa

This document discusses implementing operations on an AVL tree, which is a self-balancing binary search tree. It includes the aim, pseudocode, source code and output of a C program to perform insertions, deletions, searches, and traversals on an AVL tree while maintaining the balance factor through rotations.

Uploaded by

pp5006277
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)
12 views12 pages

Exp-7 Swethaa

This document discusses implementing operations on an AVL tree, which is a self-balancing binary search tree. It includes the aim, pseudocode, source code and output of a C program to perform insertions, deletions, searches, and traversals on an AVL tree while maintaining the balance factor through rotations.

Uploaded by

pp5006277
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/ 12

EXP NO:7A

DATE: IMPLEMENTATION OF BALANCED SEARCH TREE (AVL) OPERATIONS

AIM:
To write a C program for implementing AVL trees by performing appropriate
rotations. Data Structure Used: Tree
Time Complexity:
Best case : O(log n)
Worst case : O(log n)
Average case: O(log n)

PSEUDO CODE:
BEGIN
struct node* rotate_left(struct node* root)
{
struct node* right_child = root-
>right; root->right = right_child-
>left; right_child->left = root;
root->ht = height(root);
right_child->ht = height(right_child);
return right_child;
}
struct node* rotate_right(struct node* root)
{
struct node* left_child = root-
>left; root->left = left_child-
>right; left_child->right = root;
root->ht = height(root);
left_child->ht = height(left_child);
return left_child;
}
END
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
int ht;
};
struct node* root = NULL;

717822P339
struct node* create(int);
struct node* insert(struct node*, int);
struct node* delete(struct node*, int);
struct node* search(struct node*, int);
struct node* rotate_left(struct node*);
struct node* rotate_right(struct node*);
int balance_factor(struct node*);
int height(struct node*);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);

int main()
{
int user_choice, data;
char user_continue = 'y';
struct node* result = NULL;
printf("------- AVL TREE--------\n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Inorder");
printf("\n5. Preorder");
printf("\n6. Postorder");
printf("\n7. EXIT");
printf("\nEnter Your Choice: ");
scanf("%d", &user_choice);
while(user_choice!=-1)
{
switch(user_choice)
{
case 1:
printf("Enter data: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
root = delete(root,
data); break;
case 3:
printf("Enter data: ");
scanf("%d", &data);
result = search(root,
data); if (result ==
NULL)
{
printf("Node not found!");
}

717822P339
else
{
printf("Node found");
}
break;
case 4:
inorder(root);
break;
case 5:
preorder(root);
break;
case 6:
postorder(root);
break;
case 7:
printf("\n\tProgram Terminated");
return 1;
default:
printf("\n\tInvalid Choice");
}
printf("Enter Your Choice: ");
scanf("%d", &user_choice);
}
return 0;
}

struct node* create(int data)


{
struct node* new_node = (struct node*) malloc (sizeof(struct node));

// if a memory error has occurred


if (new_node == NULL)
{
printf("\nMemory can't be allocated\n");
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// rotates to the left
struct node* rotate_left(struct node* root)
{
struct node* right_child = root->right;
root->right = right_child->left;
right_child->left = root;

// update the heights of the nodes


root->ht = height(root);

717822P339
right_child->ht = height(right_child);

// return the new node after rotation


return right_child;
}
// rotates to the right
struct node* rotate_right(struct node* root)
{
struct node* left_child = root->left;
root->left = left_child->right;
left_child->right = root;
// update the heights of the nodes
root->ht = height(root);
left_child->ht = height(left_child);

// return the new node after rotation


return left_child;
}

// calculates the balance factor of a node


int balance_factor(struct node* root)
{
int lh, rh;
if (root == NULL)
return 0;
if (root->left == NULL)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;
return lh - rh;
}

// calculate the height of the node


int height(struct node* root)
{
int lh, rh;
if (root == NULL)
{
return 0;
}
if (root->left == NULL)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;

717822P339
else
rh = 1 + root->right->ht;

if (lh > rh)


return (lh);
return (rh);
}

// inserts a new node in the AVL tree


struct node* insert(struct node* root, int data)
{
if (root == NULL)
{
struct node* new_node = create(data);
if (new_node == NULL)
{
return NULL;
}
root = new_node;
}
else if (data > root->data)
{
// insert the new node to the right
root->right = insert(root->right, data);

// tree is unbalanced, then rotate it


if (balance_factor(root) == -2)
{
if (data > root->right->data)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
// insert the new node to the left
root->left = insert(root->left, data);

// tree is unbalanced, then rotate it


if (balance_factor(root) == 2)
{
if (data < root->left->data)
{
root = rotate_right(root);

717822P339
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
// update the heights of the nodes
root->ht = height(root);
return root;
}

// deletes a node from the AVL tree


struct node * delete(struct node *root, int x)
{
struct node * temp = NULL;

if (root == NULL)
{
return NULL;
}

if (x > root->data)
{
root->right = delete(root->right, x);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else if (x < root->data)
{
root->left = delete(root->left, x);
if (balance_factor(root) == -2)
{
if (balance_factor(root->right) <= 0)
{
root = rotate_left(root);
}
else
{

717822P339
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
if (root->right != NULL)
{
temp = root->right;
while (temp->left != NULL)
temp = temp->left;

root->data = temp->data;
root->right = delete(root->right, temp->data);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else
{
return (root->left);
}
}
root->ht = height(root);
return (root);
}

// search a node in the AVL tree


struct node* search(struct node* root, int key)
{
if (root == NULL)
{
return NULL;
}

if(root->data == key)
{
return root;
}

717822P339
if(key > root->data)
{
search(root->right, key);
}
else
{
search(root->left, key);
}
}

// inorder traversal of the tree


void inorder(struct node* root)
{
if (root == NULL)
{
return;
}

inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

// preorder traversal of the tree


void preorder(struct node* root)
{
if (root == NULL)
{
return;
}

printf("%d ", root->data);


preorder(root->left);
preorder(root->right);
}

// postorder traversal of the tree


void postorder(struct node* root)
{
if (root == NULL)
{
return;
}

postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}

717822P339
OUTPUT:

Result:
Thus, the C program for implementing AVL trees by performing appropriate rotations is executed
successfully and the output is verified.

717822P339
EXP. NO :7B
Kth SMALLEST ELEMENT IN A BST
DATE :

AIM:
To write a C program to return the kth smallest value (1-indexed) of all the values of the nodes in the
tree.
Data Structure Used: Tree
Time Complexity:
Best case: O(log n) Worst
case:O(n) Average
case:O(log n)

PSEUDO CODE:
BEGIN
void inorder(struct TreeNode* root, int* result, int* count, int k) { if
(root == NULL)
return;
inorder(root->left, result, count, k); (*count)++;
if (*count == k) {
*result = root->val;
return;
}
inorder(root->right, result, count, k);
} END

SOURCE CODE:
#include <stdio.h>
include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void inorder(struct TreeNode* root, int* result, int* count, int k)
{ if (root == NULL)
return;
// Traverse left subtree
inorder(root->left, result, count, k);
// Visit the current node (*count)++;
if (*count == k) {
*result = root->val; // Found the kth smallest element return;
}
// Traverse right subtree
inorder(root->right, result, count, k);
}
// Function to find the kth smallest element in the BST
int kthSmallest(struct TreeNode* root, int k) {
// Array to store in-order traversal results
int result = 0;
int count = 0;

// Perform in-order traversal to find the kth smallest element


inorder(root, &result, &count, k);
return result;
}
// Function to insert a new node into the BST
struct TreeNode* insert(struct TreeNode* root, int val) {
// If the tree is empty, create a new node as the root
if (root == NULL) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct
TreeNode)); newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Otherwise, recur down the tree
if (val < root->val) {
root->left = insert(root->left, val);
} else if (val > root->val) {
root->right = insert(root->right, val);
}
return root;
}
// Helper function to create a new node
struct TreeNode* newNode(int val) {
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct
TreeNode)); node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
struct TreeNode* root = NULL;
int val;
char choice;
printf("Enter elements to insert into the BST (enter any character to stop):\n");
do {
printf("Enter element: ");
if (scanf("%d", &val) != 1) {
scanf("%c", &choice); // Clear newline
character break;
}

717822P339-PRIYA.C
root = insert(root, val);
} while(1);
int k;
printf("Enter the value of k (1-indexed) to find the kth smallest element: ");
scanf("%d", &k);
int result = kthSmallest(root, k);
printf("The %dth smallest element in the BST is: %d\n", k, result);
return 0;
}

OUTPUT:

Result:
Thus, the C program to return the kth smallest value (1-indexed) of all the values of the nodes in the tree
is executed successfully and the output is verified.

717822P339-PRIYA.C

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy