Exp-7 Swethaa
Exp-7 Swethaa
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;
}
717822P339
right_child->ht = height(right_child);
717822P339
else
rh = 1 + root->right->ht;
717822P339
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
// update the heights of the nodes
root->ht = height(root);
return root;
}
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);
}
if(root->data == key)
{
return root;
}
717822P339
if(key > root->data)
{
search(root->right, key);
}
else
{
search(root->left, key);
}
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
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;
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