C 320 01fe23bec113 Shashank Assignment-03
C 320 01fe23bec113 Shashank Assignment-03
USN : 01FE23BEC113
Div : C
int main() {
FILE *in = fopen("inputq1.txt", "r");
FILE *out = fopen("outputq1.txt", "w");
if (in == NULL || out == NULL) {
printf("Error opening inputq1.txt or outputq1.txt\n");
return 1;
}
fclose(in);
fclose(out);
Output Q1:
KLE Technological University
School of Electronics and Communication Engineering
2. Consider an initially empty AVL tree. Insert the following sequence of values into
the AVL tree, one by one: [10, 20, 30, 25, 28, 05, 08]. For each insertion, clearly show
the balance factor of all affected nodes and demonstrate any necessary rotations
(single or double) performed to maintain the AVL tree property. Draw the tree after
each insertion and subsequent balancing.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int k, h;
struct Node *l, *r;
} Node;
FILE *out;
int h(Node* node) {
return node ? node->h : 0;
}
int max(int a, int b) {
return a > b ? a : b;
}
void updateH(Node* node) {
if (node)
node->h = 1 + max(h(node->l), h(node->r));
}
int getBalance(Node* node) {
return node ? h(node->l) - h(node->r) : 0;
}
Node* newNode(int k) {
Node* node = (Node*)malloc(sizeof(Node));
node->k = k;
node->l = node->r = NULL;
node->h = 1;
return node;
}
Node* rRotate(Node* y) {
Node* x = y->l;
Node* T2 = x->r;
x->r = y;
y->l = T2;
updateH(y);
updateH(x);
return x;
}
KLE Technological University
School of Electronics and Communication Engineering
Node* lRotate(Node* x) {
Node* y = x->r;
Node* T2 = y->l;
y->l = x;
x->r = T2;
updateH(x);
updateH(y);
return y;
}
updateH(node);
int balance = getBalance(node);
return node;
}
int main() {
FILE *in = fopen("q2input.txt", "r");
out = fopen("q2output.txt", "w");
if (!in || !out) {
printf("Error opening files.\n");
return 1;
}
int n, val;
Node* root = NULL;
fscanf(in, "%d", &n);
for (int i = 0; i < n; i++) {
fscanf(in, "%d", &val);
root = insert(root, val);
printTree(root, 0);
fprintf(out, "\n");
}
fclose(in);
fclose(out);
return 0;
}
KLE Technological University
School of Electronics and Communication Engineering
Output Q2:
KLE Technological University
School of Electronics and Communication Engineering
struct Node {
int data;
struct Node* l;
struct Node* r;
};
preorder(root->l, f);
preorder(root->r, f);
}
int main() {
FILE *in = fopen("q3input.txt", "r");
FILE *out = fopen("q3output.txt", "w");
if (!in || !out) return 1;
switch (choice) {
case 1: inorder(root, out); break;
case 2: preorder(root, out); break;
case 3: postorder(root, out); break;
}
fprintf(out, "\n");
}
fclose(in);
fclose(out);
return 0;
}
KLE Technological University
School of Electronics and Communication Engineering
Output Q3:
KLE Technological University
School of Electronics and Communication Engineering
char vertices[MAX];
int adjMatrix[MAX][MAX];
int visited[MAX];
int n;
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
}
return -1;
}
int findIndex(char c) {
for (int i = 0; i < n; i++) {
if (vertices[i] == c)
return i;
}
return -1;
}
dfsUtil(start, fout);
fprintf(fout, "\n");
}
int main() {
FILE *fin = fopen("q4input.txt", "r");
FILE *fout = fopen("q4output.txt", "w");
int e;
fscanf(fin, "%d", &n);
for (int i = 0; i < n; i++) {
fscanf(fin, " %c", &vertices[i]);
}
fscanf(fin, "%d", &e);
memset(adjMatrix, 0, sizeof(adjMatrix));
for (int i = 0; i < e; i++) {
char u, v;
fscanf(fin, " %c %c", &u, &v);
int idx1 = findIndex(u);
int idx2 = findIndex(v);
adjMatrix[idx1][idx2] = 1;
adjMatrix[idx2][idx1] = 1;
}
char startVertex;
fscanf(fin, " %c", &startVertex);
int startIndex = findIndex(startVertex);
char line[100];
fgets(line, sizeof(line), fin);
fgets(line, sizeof(line), fin);
char *token = strtok(line, " ");
while (token != NULL) {
int choice = atoi(token);
switch (choice) {
case 1:
bfs(startIndex, fout);
break;
case 2:
dfs(startIndex, fout);
break;
default:
fprintf(fout, "Invalid choice %d\n", choice);
}
token = strtok(NULL, " ");
}
fclose(fin);
fclose(fout);
KLE Technological University
School of Electronics and Communication Engineering
return 0;
}
Output Q4 :
KLE Technological University
School of Electronics and Communication Engineering
5. Consider a weighted, directed graph with at least six vertices. Implement Dijkstra's
Algorithm by selecting a starting vertex, and illustrate each step of the distance
updates throughout the process.
Code:
. #include <stdio.h>
#include <stdlib.h>
#define N 6
#define MAX 99999
int main() {
FILE *fout = fopen("q5output.txt", "w");
if (!fout) {
printf("Error opening output file\n");
return 1;
}
int graph[N][N] = {
{ 0, 4, 2, 0, 0, 0 },
{ 0, 0, 3, 2, 3, 0 },
{ 0, 1, 0, 4, 5, 0 },
{ 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 0, 2 },
{ 0, 0, 0, 0, 0, 0 }
};
int startNode = 0;
dijkstraAlgo(fout, graph, startNode);
fclose(fout);
return 0;
}
KLE Technological University
School of Electronics and Communication Engineering
Output Q5:
KLE Technological University
School of Electronics and Communication Engineering
6. You have a hash table of size 10 (indices 0-9) and the hash function h(k)=kpmod10.
Insert the following sequence of keys: [43, 22, 1, 31, 77, 99, 11, 55, 60]. Perform the
insertions using the following collision resolution techniques:
a) Chaining (using linked lists for collisions)
b) Linear probing
c) Quadratic probing
For each method, provide the final state of the hash table. In the case of chaining,
include the linked lists representing collided elements at each index.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
#define MAX_KEYS 100
#define P 7
Node* newNode(int k) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->k = k;
temp->next = NULL;
return temp;
}
int hash(int k) {
return (k * P) % SIZE;
}
fprintf(out, "Chaining:\n");
for (int i = 0; i < SIZE; i++) {
fprintf(out, "Index %d:", i);
Node* curr = chainingTable[i];
while (curr) {
fprintf(out, " %d", curr->k);
if (curr->next) fprintf(out, " ->");
curr = curr->next;
}
fprintf(out, "\n");
}
int main() {
FILE *in = fopen("q6input.txt", "r");
FILE *out = fopen("q6output.txt", "w");
if (!in || !out) return 1;
int n, keys[MAX_KEYS];
fscanf(in, "%d", &n);
for (int i = 0; i < n; i++) {
fscanf(in, "%d", &keys[i]);
}
Output Q6: