Ads Lab
Ads Lab
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;
}
}
}
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");
return 0;
}
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;
}
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
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);
}
else if (choice == 2)
{
printGraph(graph);
}
else if (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);
}
return 0;
}
OUTPUT:
#include <stdio.h>
int main()
{
int numVertices = 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("Adjacency
matrix:\n");
for (int i = 0; i <
numVertices; i++) { for
(int j = 0; j <
numVertices; j++) {
printf("%d ", graph[i][j]);
}
printf("\n");
}
Output:
Adjacency
matrix: 0 1
100
00010
00001
00001
00000
DFS traversal: 0 1 3 4 2
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
5. Write a program for finding the biconnected components in a given
graph
#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;
}
}
jobs[2][1] = 5;
jobs[3][0] = 2;
jobs[3][1] = 1;
jobs[4][0] = 4;
jobs[4][1] = 7;
scheduleJobs(jobs, numJobs);
return 0;
}
```
Output:
```
Scheduled jobs:
Job 1: Time = 2, Deadline = 3
Job 3: Time = 2, Deadline = 1
Job 2: Time = 3, Deadline = 5
Job 5: Time = 4, Deadline = 7
int main()
{
int numItems = 4;
int capacity = 10;
int weights[] = {3, 4, 5, 2};
int values[] = {10, 20, 30, 40};
int maxValue = knapsack(weights, values, numItems, capacity);
printf("Maximum value that can be put in knapsack: %d\n", maxValue);
return 0;
}
```
Output:
```
Maximum value that can be put in knapsack: 70
}
return 0;
}
void printSolution(int board[10][10], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int main() {
int n = 4;
int board[10][10] = {0};
if (solveNQueens(board, 0, n))
{
printSolution(board, n);
}
else
{
printf("No solution exists\n");
}
return 0;
}
Output:
0001
1000
0100
0010
#include <stdio.h>
int knapsack(int* weights, int* values, int numItems, int capacity, int
currentIndex, int
currentValue, int currentWeight)
{
if (currentIndex == numItems || currentWeight == capacity)
{
return currentValue;
}
int includeValue = 0;
if (currentWeight + weights[currentIndex] <= capacity)
{
includeValue = knapsack(weights, values, numItems, capacity, currentIndex +
1,
currentValue + values[currentIndex], currentWeight + weights[currentIndex]);
}
int excludeValue = knapsack(weights, values, numItems, capacity, currentIndex
+ 1,
currentValue, currentWeight);
return (includeValue > excludeValue) ? includeValue : excludeValue;
}
int main() {
int numItems = 4;
int capacity = 10;
int weights[] = {3, 4, 5, 2};
int values[] = {10, 20, 30, 40};
int maxValue = knapsack(weights, values, numItems, capacity, 0, 0, 0);
printf("Maximum value that can be put in knapsack: %d\n", maxValue);
return 0;
}
Output:
```
Maximum value that can be put in knapsack: 70
#include <stdio.h>
#include <limits.h>
#define N 5
int cost[N][N] = {
};
if (visited == (1 << N) - 1) {
return;
path[city] = i;
int main() {
int path[N];
path[0] = 0;
int visited = 1;
return 0;
Output:
Minimum cost: 80