1. #include <stdio.
h>
#define size 100
int heap[size], current_node=0;
void ins(int x)
current_node=current_node+1;
int temp=current_node;
while (temp>1 && x>heap[temp/2])
heap[temp]=heap[temp/2];
temp=temp/2;
heap[temp]=x;
void display()
int i;
printf("Heap: ");
for(i =1;i<=current_node;i++)
printf("\n%d",heap[i]);
printf("\n");
void delete()
{
int n=current_node;
int temp=heap[n];
heap[1]=temp;
current_node--;
int parent=1;
int child=2;
while(child<=current_node)
if(child+1<=current_node&&heap[child]<heap[child+1])
child=child+1;
if(heap[parent]>=heap[child])
break;
temp = heap[parent];
heap[parent]=heap[child];
heap[child]=temp;
parent=child;
child=child*2;
int main()
int choice,x,root=heap[1];;
while(1)
{
printf("1.insert\n");
printf("2.display\n");
printf("3.delete\n");
printf("4.exit\n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
case 1:
printf("enter element to insert: ");
scanf("%d",&x);
ins(x);
break;
case 2:
display();
break;
case 3:
if (current_node>0)
printf("the root element is deleted\n");
delete();
} else
printf("heap is empty\n");
break;
case 4:
return 0;
default:
printf("invalid choice.\n");
return 0;
2. #include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node* left;
struct node* right;
};
struct node* newnode(int key) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->key = key;
node->left = node->right = NULL;
return node;
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);
return node;
struct node* minvaluenode(struct node* node) {
struct node* current = node;
while (current && current->left != NULL) {
current = current->left;
return current;
struct node* deletenode(struct node* root, int key) {
if (root == NULL) {
return root;
if (key < root->key) {
root->left = deletenode(root->left, key);
} else if (key > root->key) {
root->right = deletenode(root->right, key);
} else {
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
struct node* temp = minvaluenode(root->right);
root->key = temp->key;
root->right = deletenode(root->right, temp->key);
return root;
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
int main() {
struct node* root = NULL;
int choice, value;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Inorder Traversal\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deletenode(root, value);
break;
case 3:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
return 0;
3.
#include<stdio.h>
#include<stdlib.h>
struct node
struct node *left;
int data;
struct node *right;
};
struct node *root,*newnode,*temp,*parent,*temp1,*parent1;
int min;
void insertion(int item)
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=item;
newnode->left=0;
newnode->right=0;
if(root==NULL)
root=newnode;
return;
else
temp=root;
while(temp!=NULL)
parent=temp;
if(item<temp->data)
temp=temp->left;
else{
temp=temp->right;
if(item<parent->data)
{
parent->left=newnode;
else{
parent->right=newnode;
void deletion(int key)
temp=root;
while(temp!=NULL)
if(key==temp->data)
break;
else if(key<temp->data)
parent=temp;
temp=temp->left;
else
parent=temp;
temp=temp->right;
}
if(temp==NULL)
printf("element is not found:");
else if((temp->left==NULL) &&(temp->right==NULL))
if(parent==NULL)
root=NULL;
else if (key<parent->data)
parent->left=NULL;
else
parent->right=NULL;
else if((temp->left==NULL) &&(temp->right!=NULL))
if(parent==NULL)
root=temp->right;
else if (key<parent->data)
parent->left=temp->right;
else
parent->right=temp->right;
else if((temp->left!=NULL) &&(temp->right==NULL))
if(parent==NULL)
root=temp->left;
else if (key<parent->data)
parent->left=temp->left;
else
parent->right=temp->left;
else if((temp->left!=NULL) &&(temp->right!=NULL))
temp1=temp->right;
min=temp1->data;
while(temp1->left!=NULL)
parent1=temp1;
temp1=temp1->left;
min=temp->data;
if (min==temp->right->data)
temp->data=min;
temp->right=temp->right->right;
else
temp->data=min;
parent->left=temp->right;
void print(struct node* root)
if (root == NULL)
return;
printf("%d ", root->data);
print(root->left);
print(root->right);
int search(int key)
struct node *temp = root;
while (temp != NULL)
if (key == temp->data)
return 1;
else if (key < temp->data)
temp = temp->left;
else
temp = temp->right;
int main()
int n,i,item,ch,del,key;
while(1)
{
printf("\nOPERATION PERFORMED");
printf("\n1.INSERTION");
printf("\n2.DELETION");
printf("\n3.SEARCH");
printf("\n4.PRINT");
printf("\n5.EXIT");
printf("\nENTER THE CHOICE:");
scanf("%d",&ch);
if(ch==1)
printf("ENTER THE NO OF NODES: \n");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("ENTER THE ITEM:");
scanf("%d",&item);
insertion(item);
else if(ch==2)
printf("enter the element to be deleted:");
scanf("%d",&del);
deletion(del);
else if(ch==3)
{
printf("ENTER THE ELEMENT TO BE SEARCHED: ");
scanf("%d", &key);
if (search(key))
printf("Element %d found in the BST.\n", key);
else
printf("Element %d not found in the BST.\n", key);
else if(ch==4)
print(root);
else
break;
4. #include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};
struct Node* newNode(int key) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = node->right = NULL;
node->height = 1;
return node;
int height(struct Node* node) {
return node ? node->height : 0;
int getBalance(struct Node* node) {
return node ? height(node->left) - height(node->right) : 0;
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 {
return node;
}
node->height = 1 + (height(node->left) > height(node->right) ? height(node->left) : height(node-
>right));
return node;
int isAVL(struct Node* root) {
if (root == NULL) {
return 1;
int balance = getBalance(root);
return (balance >= -1 && balance <= 1) && isAVL(root->left) && isAVL(root->right);
void traverseAndPrint(struct Node* root) {
if (root) {
traverseAndPrint(root->left);
printf("Key: %d, Balance Factor: %d\n", root->key, getBalance(root));
traverseAndPrint(root->right);
int main() {
struct Node* root = NULL;
int n, value;
printf("Enter the number of elements to insert: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &value);
root = insert(root, value);
printf("\nTraversing the tree:\n");
traverseAndPrint(root);
if (isAVL(root)) {
printf("\nThe tree is an AVL tree.\n");
} else {
printf("\nThe tree is not an AVL tree.\n");
return 0;
5.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};
struct Node* newNode(int key) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = node->right = NULL;
node->height = 1;
return node;
int height(struct Node* node) {
return node ? node->height : 0;
int getBalance(struct Node* node) {
return node ? height(node->left) - height(node->right) : 0;
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 {
return node;
node->height = 1 + (height(node->left) > height(node->right) ? height(node->left) : height(node-
>right));
return node;
}
void displayBalancedNodes(struct Node* root) {
if (root) {
displayBalancedNodes(root->left);
int balance = getBalance(root);
if (balance == 0 || balance == 1) {
printf("Key: %d, Balance Factor: %d\n", root->key, balance);
displayBalancedNodes(root->right);
int main() {
struct Node* root = NULL;
int n, value;
printf("Enter the number of elements to insert: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &value);
root = insert(root, value);
printf("\nNodes with balance factors 0 and 1:\n");
displayBalancedNodes(root);
return 0;
}