Lab Manual
Lab Manual
Lab Manual
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.
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
};
if (node == NULL)
return 0;
return node->height;
return (a > b) ? a : b;
}
if (newNode != NULL) {
newNode->data = key;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
return x;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
return y;
return 0;
if (root == NULL)
return createNode(key);
return root;
// Get the balance factor to check whether this node became unbalanced
return rightRotate(root);
return leftRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
current = current->left;
return current;
if (root == NULL)
return root;
else {
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
} else // One child case
free(temp);
} else {
root->data = temp->data;
if (root == NULL)
return root;
// Get the balance factor to check whether this node became unbalanced
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
}/
if (root != NULL) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
if (root != NULL) {
freeAVLTree(root->left);
freeAVLTree(root->right);
free(root);
int main() {
do {
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &key);
break;
case 2:
scanf("%d", &key);
break;
case 3:
inOrderTraversal(root);
printf("\n");
break;
case 4:
freeAVLTree(root);
printf("Exiting...\n");
break;
default:
return 0;
Output:
[?2004l
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
Enter your choice: 1
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
In-order Traversal: 2 4 5 11 20 32 40
AVL Tree Operations:
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
In-order Traversal: 4 5 11 20 32 40
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
In-order Traversal: 5 11 20 32 40
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
In-order Traversal: 5 11 20 40
2. Delete a node
3. In-order Traversal
4. Exit
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
In-order Traversal: 5 20 40
1. Insert a node
2. Delete a node
3. In-order Traversal
4. Exit
Exiting...
Week 2
2.Construct B-Tree an order of 5 with a set of 100 random elements stored in array. Implement
searching, insertion and deletion operations.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
struct BTreeNode
};
int i;
if (newNode == NULL)
exit(EXIT_FAILURE);
newNode->num_keys = 0;
newNode->is_leaf = is_leaf;
for (i = 0; i < M; i++) {
newNode->children[i] = NULL;
return newNode;
int i;
newNode->num_keys = M/2 - 1;
if (!child->is_leaf)
child->num_keys = M/2 - 1;
parent->children[i + 1] = parent->children[i];
parent->children[index + 1] = newNode;
// Shift parent's keys to insert the middle key from the child
parent->keys[i + 1] = parent->keys[i];
parent->num_keys++;
int i = node->num_keys - 1;
if (node->is_leaf)
node->keys[i + 1] = node->keys[i];
i--;
}
node->keys[i + 1] = key;
node->num_keys++;
else
i--;
i++;
if (node->children[i]->num_keys == M - 1)
splitChild(node, i);
i++;
insertNonFull(node->children[i], key);
if (node == NULL)
*root = createNode(true);
(*root)->keys[0] = key;
(*root)->num_keys = 1;
else
if (node->num_keys == M - 1)
new_root->children[0] = node;
splitChild(new_root, 0);
*root = new_root;
insertNonFull(*root, key);
if (root != NULL)
int i;
traverse(root->children[i]);
traverse(root->children[i]);
int main()
insert(&root, 10);
insert(&root, 20);
insert(&root, 5);
insert(&root, 6);
insert(&root, 12);
insert(&root, 30);
traverse(root);
printf("\n");
getch();
return 0;
OUTPUT:
Program 3:
Write a C program to Construct Min and Max Heap using arrays, delete any
element and display the content of the Heap.
#include <stdio.h>
int size = 0;
*a = *b;
*b = temp;
heap[size] = element;
current = (current - 1) / 2;
size++;
heap[size] = element;
current = (current - 1) / 2;
size++;
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < size && heap[left] < heap[smallest])
smallest = left;
smallest = right;
if (smallest != i)
swap(&heap[i], &heap[smallest]);
heapifyMin(heap, smallest);
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i)
swap(&heap[i], &heap[largest]);
heapifyMax(heap, largest);
int i;
if (heap[i] == element)
break;
if (i < size)
size--;
heapifyMin(heap, i);
}
}
int i;
if (heap[i] == element)
break;
if (i < size)
size--;
heapifyMax(heap, i);
int i;
printf("\n");
int main()
while (1)
printf("\n1. Insert into Min Heap\n2. Insert into Max Heap\n3. Delete from Min Heap\n4. Delete
from Max Heap\n5. Display Min Heap\n6. Display Max Heap\n7. Exit\n");
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &element);
insertMinHeap(minHeap, element);
break;
case 2:
scanf("%d", &element);
insertMaxHeap(maxHeap, element);
break;
case 3:
scanf("%d", &element);
deleteMinHeap(minHeap, element);
break;
case 4:
scanf("%d", &element);
deleteMaxHeap(maxHeap, element);
break;
case 5:
displayHeap(minHeap);
break;
case 6:
displayHeap(maxHeap);
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
}
return 0;
OUTPUT:
WEEK 4
Write a C Program to Implement BFT and DFT forgiven graph, when graph is represented by
a) Adjacency Matrix b) Adjacency Lists
#include <stdio.h>
#include <stdlib.h>
int adjMatrix[MAX][MAX];
int visited[MAX];
if (rear == MAX - 1)
printf("\nQueue Overflow");
else
if (front == -1)
front = 0;
rear = rear + 1;
queue[rear] = vertex;
int dequeue()
int vertex;
printf("\nQueue Underflow");
return -1;
else
vertex = queue[front];
front = front + 1;
return vertex;
int i,vertex;
enqueue(startVertex);
visited[startVertex] = 1;
printf("BFS: ");
vertex = dequeue();
enqueue(i);
visited[i] = 1;
}
}
printf("\n");
int i;
visited[vertex] = 1;
DFTMatrix(n, i);
int main()
int n, i, j, startVertex;
scanf("%d", &n);
scanf("%d", &adjMatrix[i][j]);
}
scanf("%d", &startVertex);
// BFS
BFTMatrix(n, startVertex);
// DFS
printf("DFS: ");
DFTMatrix(n, startVertex);
printf("\n");
scanf("%d",&n);
return 0;
OUTPUT
WEEK 6
Program 1:
Write a c program to implement Quick sort and Merge sort and observe the execution time for
various input sizes (Average, Worst and Best cases).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
*a = *b;
*b = temp;
{
if (arr[j] < pivot)
i++;
swap(&arr[i], &arr[j]);
return i + 1;
// QuickSort function
{
int i;
int i;
arr[i] = i;
int i;
arr[i] = n - i;
}
}
void measureExecutionTime(void (*generateArray)(int[], int), int arr[], int n, const char* caseType)
double cpu_time_used;
quickSort(arr, 0, n - 1);
// Driver function
int main()
int i,arr[MAX_SIZE];
printf("Measuring execution time of Quick Sort:\n\n");
printf("\n");
return 0;
OUTPUT:
MERGE SORT:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
j = 0;
k = l;
arr[k] = L[i];
i++;
else
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
arr[k] = R[j];
j++;
k++;
if (l < r)
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int i;
arr[i] = i;
int i;
arr[i] = n - i;
int i;
arr[i] = rand() % n;
{
int i;
printf("\n");
generateCase(arr, n);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
int main()
int n;
scanf("%d", &n);
free(arr);
getch();
return 0;
Week 7
Compare the performance of Single Source Shortest Paths using Greedy method when the graph
is represented by adjacency matrix and adjacency lists.
#include <stdio.h>
distance[start] = 0;
visited_nodes[start] = 1;
counter = 1;
visited_nodes[next_node] = 1;
for (i = 0; i < size; i++)
if (!visited_nodes[i])
if (minimum_distance + cost[next_node][i] < distance[i])
{
distance[i] = minimum_distance + cost[next_node][i];
previous[i] = next_node;
}
counter++;
}
// printing the distance
for (i = 0; i < size; i++)
if (i != start)
{
printf("\nDistance from the Source Node to %d: %d", i, distance[i]);
}
}
// main function
int main()
{
size = 7;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 8;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 11;
Graph[1][6] = 0;
Graph[2][0] = 0;
Graph[2][1] = 8;
Graph[2][2] = 0;
Graph[2][3] = 7;
Graph[2][4] = 0;
Graph[2][5] = 4;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 7;
Graph[3][3] = 0;
Graph[3][4] = 9;
Graph[3][5] = 14;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 0;
Graph[4][3] = 9;
Graph[4][4] = 0;
Graph[4][5] = 10;
Graph[4][6] = 2;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 4;
Graph[5][3] = 14;
Graph[5][4] = 10;
Graph[5][5] = 0;
Graph[5][6] = 2;
Graph[6][0] = 0;
Graph[6][1] = 0;
Graph[6][2] = 0;
Graph[6][3] = 0;
Graph[6][4] = 2;
Graph[6][5] = 0;
Graph[6][6] = 1;
source = 0;
// calling the DijkstraAlgorithm() function by passing the Graph, the number of nodes and the source
node
DijkstraAlgorithm(Graph, size, source);
getch();
return 0;
}
OUTPUT:
Week 8
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
int i, w;
int K[n+1][W+1];
{
if (i==0 || w==0)
K[i][w] = 0;
else
K[i][w] = K[i-1][w];
return K[n][W];
int main()
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
getch();
return 0;
}
OUTPUT: