ads lab programs
ads lab programs
ads lab programs
Construct 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.
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
int data;
struct TreeNode* left;
struct TreeNode* right;
int height;
};
int height(struct TreeNode* node)
{
if (node == NULL)
return 0;
return node->height;
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
struct TreeNode* createNode(int key)
{
struct TreeNode* newNode =(struct TreeNode*)malloc(sizeof(struct TreeNode));
if (newNode != NULL)
{
newNode->data = key;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
} return newNode;
}
OUTPUT
AVL Tree operations:
1.Insert node
2.Delete node
3.Inorder Traversal
4.Exit
Enter Your Choice 1
Enter the key to Insert 30
Enter your Choice 1
Enter the key to Insert 20
Enter Your choice 1
Enter the key to Insert 10
Enter Your Choice 3
10 20 30
Enter your Choice 2
Enter the key to Delete 10
Enter your choice 3
20 30
Enter your choice 4
Exiting
}
}
insertKey(&node->children[i + 1], key);
}
}
// Function to split a child node
void splitChild(BTreeNode* node, int index) {
BTreeNode* child = node->children[index];
BTreeNode* newChild = createNode();
node->children[index + 1] = newChild;
newChild->leaf = child->leaf;
int mid = child->numKeys / 2;
newChild->numKeys = child->numKeys - mid - 1;
for (int i = mid + 1; i < child->numKeys; i++) {
newChild->keys[i - mid - 1] = child->keys[i];
}
if (!child->leaf) {
for (int i = mid + 1; i <= child->numKeys; i++) {
newChild->children[i - mid - 1] = child->children[i];
}
}
child->numKeys = mid;
node->numKeys++;
for (int i = node->numKeys; i > index + 1; i--) {
node->keys[i] = node->keys[i - 1];
}
node->keys[index + 1] = child->keys[mid];
for (int i = node->numKeys + 1; i > index + 2; i--) {
node->children[i] = node->children[i - 1];
}
node->children[index + 2] = newChild;
}
// Function to print the B-tree
void printTree(BTreeNode* node, int level) {
for (int i = 0; i < level; i++) {
printf(" ");
}
for (int i = 0; i < node->numKeys; i++) {
printf("%d ", node->keys[i]);
}
printf("\n");
if (!node->leaf) {
Output:
```
B-tree:
452
123 234 345
12 23 34 45
1234
567 678 789
5678
901 234 567
89 90 12 34
9012
3. Construct min, max heap using arrays delete any element stored in arrayof
the heap?
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void maxHeapify(int arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
maxHeapify(arr, n, largest);
}
}
void buildMaxHeap(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
maxHeapify(arr, n, i);
}
void deleteMaxHeap(int arr[], int* n)
{
if (*n == 0)
return;
arr[0] = arr[*n - 1];
(*n)--;
maxHeapify(arr, *n, 0);
}
void minHeapify(int arr[], int n, int i)
{
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] < arr[smallest])
smallest = left;
if (right < n && arr[right] < arr[smallest])
smallest = right;
if (smallest != i) {
swap(&arr[i], &arr[smallest]);
minHeapify(arr, n, smallest);
}
}
void buildMinHeap(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
minHeapify(arr, n, i);
}
void deleteMinHeap(int arr[], int* n)
{
if (*n == 0)
return;
arr[0] = arr[*n - 1];
(*n)--;
minHeapify(arr, *n, 0);
}
void displayHeap(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {4, 2, 9, 6, 5, 1, 8, 3, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
displayHeap(arr, n);
buildMaxHeap(arr, n);
printf("Max heap: ");
displayHeap(arr, n);
deleteMaxHeap(arr, &n);
printf("Max heap after deletion: ");
displayHeap(arr, n);
buildMinHeap(arr, n);
printf("Min heap: ");
displayHeap(arr, n);
deleteMinHeap(arr, &n);
printf("Min heap after deletion: ");
displayHeap(arr, n);
return 0;
}
Output:
Original array: 4 2 9 6 5 1 8 3 7
Max heap: 9 6 8 4 5 1 3 2 7
Max heap after deletion: 8 6 7 4 5 1 3 2
Min heap: 1 2 3 4 5 6 7 8
Min heap after deletion: 2 4 3 8 5 6 7
#include <stdio.h>
void bfs(int v)
{
for(i =1; i <= n; i++)
if(adj[v][i]&&!visited[i])
queue[++rear]= i;
if(front <= rear)
{
visited[queue[front]]=1;
bfs(queue[front++]);
}
}
void main()
{
int v;
printf("Enter the number of vertices: ");
scanf("%d",&n);
for(i =1; i <= n; i++)
{
queue[i]=0;
visited[i]=0;
}
printf("Enter graph data in matrix form: \n");
for(i =1; i <= n; i++)
for(j =1; j <= n; j++)
scanf("%d",&adj[i][j]);
printf("Enter the starting vertex: ");
scanf("%d",&v);
bfs(v);
printf("The node which are reachable are: \n");
for(i =1; i <= n; i++)
if(visited[i])
printf("%d\t", i);
else
printf("BFS is not possible. Not all nodes are reachable");
return0;
}
OUTPUT
Enter the number of vertices: 4
Enter graph data in matrix form:
0110
1001
1001
0110
Enter the starting vertex: 2
The node which are reachable are:
1 2 3 4
BFS USING ADJACENCY LIST
#include <stdio.h>
#include <stdlib.h>
struct node
{
int vertex;
struct node *next;
};
struct Graph
{
int numVertices;
struct node **adjLists;
int*visited;
};
int i;
for(i =0; i < vertices; i++)
{
graph->adjLists[i]= NULL;
graph->visited[i]=0;
}
return graph;
}
while(!isEmpty(queue))
{
printQueue(queue);
int currentVertex = dequeue(&queue);
printf("Visited %d ", currentVertex);
while(temp)
{
int adjVertex = temp->vertex;
if(graph->visited[adjVertex]==0)
{
graph->visited[adjVertex]=1;
enqueue(&queue, adjVertex);
}
temp = temp->next;
}
}
}
int main(void)
{
struct Graph *graph = createGraph(6);
printf("\nWhat do you want to do?\n");
printf("1. Add edge\n");
printf("2. Print graph\n");
printf("3. BFS\n");
printf("4. Exit\n");
int choice;
scanf("%d",&choice);
while(choice !=4)
{
if(choice ==1)
{
int src, dest;
printf("Enter source and destination: ");
scanf("%d %d",&src,&dest);
addEdge(graph, src, dest);
}
elseif(choice ==2)
{
printGraph(graph);
}
elseif(choice ==3)
{
int startVertex;
printf("Enter starting vertex: ");
scanf("%d",&startVertex);
bfs(graph, startVertex);
}
else
{
printf("Invalid choice\n");
}
printf("What do you want to do?\n");
printf("1. Add edge\n");
printf("2. Print graph\n");
printf("3. BFS\n");
printf("4. Exit\n");
scanf("%d",&choice);
}
return0;
}
OUTPUT:
#include<stdio.h>
voidDFS(int**graph,intnumVertices,intstartV
ertex)
{
int*visited=(int*)malloc(numVertices*sizeof(int));
for(inti=0;i<numVertices;i++)
{ visited[i] = 0;
}
printf("DFS traversal: ");
DFSUtil(graph,numVertices,startVertex,visite
d);
printf("\n");
free(visited);
}
voidDFSUtil(int**graph,intnumVertices,intvertex,int*visited)
{
visited[vertex] = 1;
printf("%d",vertex);
for(inti=0;i<numVertices;i++)
{
if (graph[vertex][i] == 1 && !visited[i]) {
DFS(graph,numVertices,i,visited);
}
}
}
intmain()
{
intnumVertices=5;
int**graph=(int**)malloc(numVertices*sizeof
(int*));
for (int i = 0; i < numVertices; i++)
{
graph[i]=(int*)malloc(numVertices*sizeof
(int));
for (int j = 0; j < numVertices; j++)
{
graph[i][j]=0;
}
}
graph graph[0][1] = 1;
graph[0][2]=1;
graph[1][3]=1;
graph[2][4]=1;
graph[3][4]=1;
printf("Adjacencymatri
x:\n");
for (int i = 0; i < numVertices;
i++)
{ for(intj=0;j<numVertices;j
++){
printf("%d",graph[i][j]);
}
printf("\n");
}
DFS(graph,numVertices,0);
forgraph
for(inti=0;i<numVertices;i++)
{
free(graph[i]);
}
free(graph);
return0;
}
Output:
Adjacencymat
rix: 0 1 1 0 0
00010
00001
00001
00000
DFStraversal:01342
int numVertices = 5;
int numEdges = 5;
Node** graph = (Node**)malloc(numVertices * sizeof(Node*));
for (int i = 0; i < numVertices; i++)
{
graph[i] = NULL;
}
addEdge(graph, numVertices, 0, 1);
addEdge(graph, numVertices, 0, 2);
addEdge(graph, numVertices, 1, 3);
addEdge(graph, numVertices, 2, 4);
addEdge(graph, numVertices, 3, 4);
printf("Adjacency list:\n");
for (int i = 0; i < numVertices; i++)
{
Node* temp = graph[i];
printf("%d -> ", i);
while (temp) {
printf("%d ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
int* visited = (int*)malloc(numVertices * sizeof(int));
for (int i = 0; i < numVertices; i++)
{
visited[i] = 0;
}
printf("DFS traversal: ");
DFS(graph, numVertices, 0, visited);
printf("\n");
for (int i = 0; i < numVertices; i++)
{
Node* temp = graph[i];
while (temp) {
Node* next = temp->next;
free(temp);
temp = next;
}
}
free(graph);
free(visited);
return 0;
}
Output:
```
Adjacency list:
0 -> 2 1
1 -> 3
2 -> 4
3 -> 4
4 ->
DFS traversal: 0 1 3 4 2
#include <stdio.h>
#include <stdlib.h>
typedef struct Edge
{
int v;
struct Edge* next;
} Edge;
typedef struct Vertex
{
int id;
Edge* adj;
int low;
int disc;
int parent;
int visited;
} Vertex;
Edge* newEdge(int v)
{
Edge* e = (Edge*)malloc(sizeof(Edge));
e->v = v;
e->next = NULL;
return e;
}
Vertex* newVertex(int id)
{
Vertex* v = (Vertex*)malloc(sizeof(Vertex));
v->id = id;
v->adj = NULL;
v->low = 0;
v->disc = 0;
v->parent = -1;
v->visited = 0;
return v;
}
Output:
Biconnected component: 1 2 3 4
Biconnected component: 1 2 3
6. Implement Quicksort and Mergesort and observe the execution time for
various input sizes(Averagecase ,bestcase,worstcase)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];
for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (leftArr[i] <= rightArr[j])
{
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
}
free(distances);
free(visited);
}
free(distances);
free(visited);
}
int main()
{
int numVertices = 5;
int** graphList = (int**)malloc(numVertices * sizeof(int*));
for (int i = 0; i < numVertices; i++)
{
graphList[i] = (int*)malloc(numVertices * sizeof(int));
for (int j = 0; j < numVertices; j++)
{
graphList[i][j] = 0;
}
}
graphList[0][1] = 4;
graphList[0][2] = 1;
graphList[1][3] = 1;
graphList[2][3] = 2;
graphList[2][4] = 5;
printf("Adjacency list:\n");
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
{
printf("%d ", graphList[i][j]);
}
printf("\n");
}
dijkstraList(graphList, numVertices, 0);
int** graphMatrix = (int**)malloc(numVertices * sizeof(int*));
for (int i = 0; i < numVertices; i++)
{
graphMatrix[i] = (int*)malloc(numVertices * sizeof(int));
for (int j = 0; j < numVertices; j++)
{
graphMatrix[i][j] = 0;
}
}
OUTPUT:
Adjacency list:
04100
00010
00025
00000
00000
Shortest distances using adjacency list:
Vertex 0: 0
Vertex 1: 4
Vertex 2: 1
Vertex 3: 3
Vertex 4: 6
#include<stdio.h>
#include<stdlib.h>
// Item structure to store job no, deadinee and profit for each job.
int no;
int deadline;
int profit;
}job;
// function that return the index of the job with max profit among all unchecked jobs
int maxi=-1;
maxi=i;
return maxi;
int main(){
int n;
//take input
scanf("%d", &n);
//array of jobs
scanf("%d", &jobs[i].deadline);
scanf("%d", &jobs[i].profit);
jobs[i].no= i+1;
checked[i]=0;
if(maxdeadline<jobs[i].deadline){
maxdeadline = jobs[i].deadline;
//create slot array to keep the alloted slot of job for execution(create schedule)
slots[i]=0;
while(i<=n){
checked[maxi]=1;
//find a free slot starting from the last of the job's deadline to the beginning of timeline
t=jobs[maxi].deadline-1;
t--;
//if a free slot is found add the job to the slot array at t index and add the job's profit
//else the job is not added to the schedule and its profit is not added.
slots[t]= jobs[maxi].no;
maxprofit+=jobs[maxi].profit;
printf("%d\n", maxprofit);
i++;
if(slots[i]){
return 0;
OUTPUT:
20
10
15
12
16
5
9
13
16
18
Total Profit= 18
2 4 3 5 1
// Driver code
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
printf("%d", knapSack(W, weight, profit, n));
return 0;
}
OUTPUT:
220
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
int n,i,j;
scanf("%d",&n);
queen(1,n);
return 0;
}
void print(int n)
int i,j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
printf("\n\n%d",i);
if(board[i]==j)
else
int i;
for(i=1;i<=row-1;++i)
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
int column;
for(column=1;column<=n;++column)
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
queen(row+1,n);
OUTPUT:
Solution 1:
1 2 3 4
1 - Q - -
2 - - - Q
3 Q - - -
4 - - Q -
Solution 2:
1 2 3 4
1 - - Q -
2 Q - - -
3 - - - Q
4 - Q - -
if (wt[n - 1] > W)
return knapsackRecursive(W, wt, val, n - 1);
else
return max(val[n - 1]
+ knapsackRecursive(W - wt[n - 1],
wt, val, n - 1),
knapsackRecursive(W, wt, val, n - 1));
}
// Driver Code
int main()
{
int values[] = { 3, 4, 5, 6 };
int weight[] = { 2, 3, 4, 5 };
int W = 8;
// Find the number of items
int n = sizeof(values) / sizeof(values[0]);
return 0;
}
OUTPUT:
12.Implement Travelling Sales Person problem using Branch and Bound approach.
OUTPUT:
Minimum cost : 80
Path Taken : 0 1 3 2 0