Adsa Lab Manual
Adsa Lab Manual
AIM: Construct an AVL tree for a given set of elements which are stored in a file. And implement
insert and delete operation on the constructed tree. Write contents of tree into a new file using in-
order.
PROGRAM:
/*
*/
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
int ht;
};
// function prototyping
int main()
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Inorder");
printf("\n5. Preorder");
printf("\n6. Postorder");
printf("\n7. EXIT");
scanf("%d", &user_choice);
switch(user_choice)
case 1:
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
scanf("%d", &data);
break;
case 3:
scanf("%d", &data);
if (result == NULL)
else
break;
case 4:
inorder(root);
break;
case 5:
preorder(root);
break;
case 6:
postorder(root);
break;
case 7:
printf("\n\tProgram Terminated\n");
return 1;
default:
printf("\n\tInvalid Choice\n");
return 0;
if (new_node == NULL)
return NULL;
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
root->right = right_child->left;
right_child->left = root;
root->ht = height(root);
right_child->ht = height(right_child);
return right_child;
root->left = left_child->right;
left_child->right = root;
root->ht = height(root);
left_child->ht = height(left_child);
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;
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);
return (rh);
if (root == NULL)
if (new_node == NULL)
return NULL;
root = new_node;
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
if (balance_factor(root) == 2)
root = rotate_right(root);
else
root->left = rotate_left(root->left);
root = rotate_right(root);
return root;
if (root == NULL)
return NULL;
if (x > root->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);
if (balance_factor(root) == -2)
if (balance_factor(root->right) <= 0)
root = rotate_left(root);
else
root->right = rotate_right(root->right);
root = rotate_left(root);
else
if (root->right != NULL)
temp = root->right;
temp = temp->left;
root->data = 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 == NULL)
return NULL;
if(root->data == key)
return root;
{
search(root->right, key);
else
search(root->left, key);
return 0;
if (root == NULL)
return;
inorder(root->left);
inorder(root->right);
if (root == NULL)
return;
preorder(root->right);
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
OUTPUT:
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 45
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
12 45
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter data: 12
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
45
EXERCISE-2
AIM: Construct B-Tree an order of 5 with a set of 100 random elements stored in array. Implement
searching, insertion and deletion operations.
PROGRAM:
// C Program for B trees
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
#define MIN 2
struct btreeNode {
};
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
int j = node->count;
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
void splitNode (int val, int *pval, int pos, struct btreeNode *node,
int median, j;
median = MIN + 1;
else
median = MIN;
*newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
j = median + 1;
j++;
node->count = median;
} else {
*pval = node->val[node->count];
(*newNode)->link[0] = node->link[node->count];
node->count--;
int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
}
if (val < node->val[1]) {
pos = 0;
} else {
if (val == node->val[pos]) {
return 0;
} else {
return 1;
return 0;
int flag, i;
if (flag)
}
/* copy successor for the value to be deleted */
dummy = myNode->link[pos];
dummy = dummy->link[0];
myNode->val[pos] = dummy->val[1];
/* removes the value from the given node and rearrange values */
int i = pos + 1;
myNode->val[i - 1] = myNode->val[i];
myNode->link[i - 1] = myNode->link[i];
i++;
myNode->count--;
int j = x->count;
while (j > 0) {
x->val[j + 1] = x->val[j];
x->link[j + 1] = x->link[j];
}
x->val[1] = myNode->val[pos];
x->link[1] = x->link[0];
x->count++;
x = myNode->link[pos - 1];
myNode->val[pos] = x->val[x->count];
myNode->link[pos] = x->link[x->count];
x->count--;
return;
int j = 1;
x->count++;
x->val[x->count] = myNode->val[pos];
x->link[x->count] = myNode->link[pos]->link[0];
x = myNode->link[pos];
myNode->val[pos] = x->val[1];
x->link[0] = x->link[1];
x->count--;
j++;
}
return;
/* merge nodes */
int j = 1;
x2->count++;
x2->val[x2->count] = myNode->val[pos];
x2->link[x2->count] = myNode->link[0];
x2->count++;
x2->val[x2->count] = x1->val[j];
x2->link[x2->count] = x1->link[j];
j++;
j = pos;
j++;
myNode->count--;
free(x1);
doLeftShift(myNode, 1);
} else {
mergeNodes(myNode, 1);
} else {
if (myNode->count != pos) {
doRightShift(myNode, pos);
} else {
} else {
mergeNodes(myNode, pos);
} else {
doRightShift(myNode, pos);
else
mergeNodes(myNode, pos);
if (myNode) {
flag = 0;
} else {
if (val == myNode->val[pos]) {
flag = 1;
} else {
flag = 0;
if (flag) {
if (myNode->link[pos - 1]) {
copySuccessor(myNode, pos);
if (flag == 0) {
} else {
removeVal(myNode, pos);
} else {
if (myNode->link[pos]) {
adjustNode(myNode, pos);
return flag;
}
/* delete val from B-tree */
if (!delValFromNode(val, myNode)) {
return;
} else {
if (myNode->count == 0) {
tmp = myNode;
myNode = myNode->link[0];
free(tmp);
root = myNode;
return;
if (!myNode) {
return;
*pos = 0;
} else {
if (val == myNode->val[*pos]) {
return;
/* B-Tree Traversal */
int i;
if (myNode) {
traversal(myNode->link[i]);
traversal(myNode->link[i]);
int main() {
while (1) {
scanf("%d", &ch);
switch (ch) {
case 1:
scanf("%d", &val);
insertion(val);
break;
case 2:
scanf("%d", &val);
deletion(val, root);
break;
case 3:
scanf("%d", &val);
break;
case 4:
traversal(root);
break;
case 5:
exit(0);
default:
break;
printf("\n");
OUTPUT:
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:12
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:10
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:1
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:4
1 10 12 45
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:2
Enter the element to delete:10
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:4
1 12 45
EXERCISE-3
AIM: Construct Min and Max Heap using arrays, delete any element and display the content of the
Heap.
PROGRAM:
// C Program for B trees
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (type == 1) {
// Max Heap
if (left < h->size && h->arr[left] > h->arr[largest])
largest = left;
largest = right;
largest = left;
largest = right;
if (largest != i)
{ swap(&h->arr[i], &h->arr[largest]);
if (h->size == MAX_SIZE)
return; }
h->size++;
int i = h->size - 1;
h->arr[i] = value;
while (i != 0 && ((type == 1 && h->arr[(i - 1) / 2] < h->arr[i]) || (type == 0 && h->arr[(i - 1) / 2] > h-
>arr[i])))
{
swap(&h->arr[i],
&h->arr[(i - 1) / 2]);
i = (i - 1) / 2; }
int i;
if (h->arr[i] == value)
break;
if (i == h->size)
return;
h->size--;
heapify(h, i, 1);
heapify(h, i, 0);
}
// Function to display the content of the heap
int i;
printf("\n");
int main() {
insert(&minHeap, 3, 0);
insert(&maxHeap, 3, 1);
printf("Min Heap:\n");
display(&minHeap);
printf("Max Heap:\n");
display(&maxHeap);
display(&minHeap);
display(&maxHeap);
OUTPUT:
Min Heap:
Heap elements: 3 5 17 10 84
Max Heap:
Heap elements: 84 17 5 3 10
Heap elements: 3 5 84 10
Heap elements: 84 10 5 3
EXERCISE-4
AIM: Implement BFT and DFT for given graph, when graph is represented by a) Adjacency Matrix b)
Adjacency Lists
PROGRAM:
a)BFT and DFT for Adjacency matrix
#include <stdio.h>
#include<stdlib.h>
int a[10][10],visited[10],n;
int v,f,r,q[10];
f=0,r=-1;
q[++r]=u;
visited[u]=1;
while(f<=r)
u=q[f++];
for(v=0;v<=n;v++)
if(a[u][v]==1)
if(visited[v]==0)
visited[v]=1;
q[r++]=v;
}
}
void dfs(int v)
int w;
visited[v]=1;
printf(" %d",v);
for(w=0;w<=n;w++)
if(a[v][w]==1&&visited[w]==0)
dfs(w);
void main()
int choice,source,s1,i,j,done;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(;;)
{
printf("\n1.reachable node using bfs\n2.reachable node using dfs\n3.exit\nEnter your choice\
n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&s1);
bfs(s1);
break;
case 2:for(source=0;source<n;source++)
for(i=0;i<n;i++)
visited[i]=0;
dfs(source);
break;
case 3:exit(0);
OUTPUT:
3
enter the adjacency matrix
111
111
111
3.exit
3.exit
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTICES 50
// Structure for a node in the adjacency list
struct Node {
int data;
};
struct List {
};
struct Graph {
int vertices;
};
// No. of vertices
int V;
bool adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
Graph* Graph_create(int V)
int i,j;
Graph* g = malloc(sizeof(Graph));
g->V = V;
g->adj[i][j] = false;
return g;
// Destructor
g->adj[v][w] = true;
int i,adjacent;
bool visited[MAX_VERTICES];
visited[i] = false;
int queue[MAX_VERTICES];
visited[s] = true;
queue[rear++] = s;
s = queue[front++];
// vertex s.
adjacent++) {
visited[adjacent] = true;
queue[rear++] = adjacent;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int i;
graph->vertices = vertices;
graph->array[i].head = NULL;
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
/*
newNode = createNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
*/
visited[vertex] = true;
while (currentNode) {
if (!visited[adjacentVertex]) {
currentNode = currentNode->next;
int i;
visited[i] = false;
if (!visited[order[i]]) {
int main() {
int vertices = 4;
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 0, 1);
addEdge(graph, 3, 3);
addEdge(graph, 1, 3);
Graph* g = Graph_create(4);
Graph_addEdge(g, 0, 1);
Graph_addEdge(g, 0, 2);
Graph_addEdge(g, 1, 2);
Graph_addEdge(g, 2, 0);
Graph_addEdge(g, 2, 3);
Graph_addEdge(g, 3, 3);
Graph_BFS(g, 2);
Graph_destroy(g);
return 0;
OUTPUT:
Following is Depth First Traversal (starting from vertex 2):
2013