0% found this document useful (0 votes)
7 views35 pages

C 320 01fe23bec113 Shashank Assignment-03

The document outlines a series of programming tasks related to data structures, specifically focusing on Binary Search Trees (BST), AVL trees, and binary trees. It includes code implementations for constructing, modifying, and traversing these data structures, along with input/output specifications for various operations. Additionally, it discusses graph representation and traversal methods, including BFS and DFS, with corresponding code examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

C 320 01fe23bec113 Shashank Assignment-03

The document outlines a series of programming tasks related to data structures, specifically focusing on Binary Search Trees (BST), AVL trees, and binary trees. It includes code implementations for constructing, modifying, and traversing these data structures, along with input/output specifications for various operations. Additionally, it discusses graph representation and traversal methods, including BFS and DFS, with corresponding code examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

KLE Technological University

School of Electronics and Communication Engineering

Name : Shashank M Dollin

USN : 01FE23BEC113

Roll Number : 320

Sem : IV, ECE

Div : C

Subject : Data Structures Applications Laboratory


KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Name of the student: Shashank M Dollin


USN: 01FE23BEC113
Div: C
Roll No: 320
1. You are given a set of integers: [45, 23, 78, 12, 34, 56, 89, 67, 05, 99]. Construct a Binary
Search Tree (BST) by inserting these elements in the given order. Subsequently,
demonstrate the step-by-step process for:
a) Deleting the node with value 34 (a leaf node).
b) Deleting the node with value 78 (a node with two children).
c) Inserting the value 40 into the modified BST. Draw the state of the BST after each
operation.
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node *l, *r;
} Node;

Node* newNode(int data) {


Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->l = node->r = NULL;
return node;
}

Node* insert(Node* root, int data) {


if (root == NULL) return newNode(data);
if (data < root->data)
root->l = insert(root->l, data);
else
root->r = insert(root->r, data);
return root;
}

Node* findMin(Node* root) {


while (root && root->l != NULL)
root = root->l;
return root;
}

Node* delete(Node* root, int k) {


KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

if (root == NULL) return NULL;


if (k < root->data)
root->l = delete(root->l, k);
else if (k > root->data)
root->r = delete(root->r, k);
else {
if (root->l == NULL && root->r == NULL) {
free(root);
return NULL;
} else if (root->l == NULL) {
Node* temp = root->r;
free(root);
return temp;
} else if (root->r == NULL) {
Node* temp = root->l;
free(root);
return temp;
}
Node* temp = findMin(root->r);
root->data = temp->data;
root->r = delete(root->r, temp->data);
}
return root;
}

void pTree(Node* root, int level, FILE* out) {


if (root == NULL) return;
pTree(root->r, level + 1, out);
for (int i = 0; i < level; i++)
fprintf(out, " ");
fprintf(out, "%04d\n", root->data);
pTree(root->l, level + 1, out);
}

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;
}

int n, value, op;


Node* root = NULL;
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

fscanf(in, "%d", &n);


for (int i = 0; i < n; i++) {
fscanf(in, "%d", &value);
root = insert(root, value);
}

fprintf(out, "Initial BST:\n");


pTree(root, 0, out);

while (fscanf(in, "%d %d", &op, &value) != EOF) {


if (op == 1) {
root = delete(root, value);
fprintf(out, "\nAfter deleting %d:\n", value);
pTree(root, 0, out);
} else if (op == 2) {
root = insert(root, value);
fprintf(out, "\nAfter inserting %d:\n", value);
pTree(root, 0, out);
}
}

fclose(in);
fclose(out);

printf("Execution complete. Check 'outputq1.txt' for the BST output.\n");


return 0;
}
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Output Q1:
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

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

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Node* lRotate(Node* x) {
Node* y = x->r;
Node* T2 = y->l;
y->l = x;
x->r = T2;
updateH(x);
updateH(y);
return y;
}

Node* insert(Node* node, int k) {


if (!node)
return newNode(k);
if (k < node->k)
node->l = insert(node->l, k);
else if (k > node->k)
node->r = insert(node->r, k);
else
return node;

updateH(node);
int balance = getBalance(node);

if (balance > 1 && k < node->l->k)


return rRotate(node);
if (balance < -1 && k > node->r->k)
return lRotate(node);
if (balance > 1 && k > node->l->k) {
node->l = lRotate(node->l);
return rRotate(node);
}
if (balance < -1 && k < node->r->k) {
node->r = rRotate(node->r);
return lRotate(node);
}

return node;
}

void printTree(Node* root, int space) {


if (!root) return;
space += 5;
printTree(root->r, space);
for (int i = 5; i < space; i++)
fprintf(out, " ");
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

fprintf(out, "%d\n", root->k);


printTree(root->l, space);
}

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

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Output Q2:
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

3. Given the following binary tree:

Q3. Mention height and depth of the each node.


a) Represent given binary tree using linked list.
b) Perform an In-order traversal and list the nodes visited.
c) Perform a Pre-order traversal and list the nodes visited.
d) Perform a Post-order traversal and list the nodes visited.
e) Discuss a real-world application where each of these
traversal methods would be most appropriate.
Code:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* l;
struct Node* r;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->l = newNode->r = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) return createNode(data);
if (data < root->data) root->l = insert(root->l, data);
else root->r = insert(root->r, data);
return root;
}

void inorder(struct Node* root, FILE* f) {


if (!root) return;
inorder(root->l, f);
fprintf(f, "%d ", root->data);
inorder(root->r, f);
}

void preorder(struct Node* root, FILE* f) {


if (!root) return;
fprintf(f, "%d ", root->data);
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

preorder(root->l, f);
preorder(root->r, f);
}

void postorder(struct Node* root, FILE* f) {


if (!root) return;
postorder(root->l, f);
postorder(root->r, f);
fprintf(f, "%d ", root->data);
}

int main() {
FILE *in = fopen("q3input.txt", "r");
FILE *out = fopen("q3output.txt", "w");
if (!in || !out) return 1;

int choice, val;


char line[1024];
while (fscanf(in, "%d", &choice) != EOF) {
struct Node* root = NULL;
fgetc(in); // consume newline

if (!fgets(line, sizeof(line), in)) break;

char* token = strtok(line, " \n");


while (token != NULL) {
val = atoi(token);
root = insert(root, val);
token = strtok(NULL, " \n");
}

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

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Output Q3:
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

4. Consider the following unweighted, undirected graph:


Vertices: A, B, C, D, E, F Edges: (A, B), (A, C), (B, D), (C, E), (D, F), (E, F)
a) Represent this graph using an Adjacency Matrix.
b) Represent this graph using an Adjacency List.
c) Starting from vertex A, perform a Breadth-First Search (BFS) and list the order in
which nodes are visited.
d) Starting from vertex A, perform a Depth-First Search (DFS) and list the order in
which nodes are visited. (Assume alphabetical order for visiting adjacent unvisited
nodes in both traversals).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

char vertices[MAX];
int adjMatrix[MAX][MAX];
int visited[MAX];
int n;

typedef struct {
int items[MAX];
int front;
int rear;
} Queue;

void initQueue(Queue *q) {


q->front = 0;
q->rear = -1;
}

int isEmpty(Queue *q) {


return q->rear < q->front;
}

void enqueue(Queue *q, int value) {


if (q->rear < MAX - 1) {
q->items[++(q->rear)] = value;
}
}

int dequeue(Queue *q) {


if (!isEmpty(q)) {
return q->items[(q->front)++];
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

}
return -1;
}

int findIndex(char c) {
for (int i = 0; i < n; i++) {
if (vertices[i] == c)
return i;
}
return -1;
}

void bfs(int start, FILE *fout) {


memset(visited, 0, sizeof(visited));
Queue q;
initQueue(&q);
visited[start] = 1;
enqueue(&q, start);
fprintf(fout, "BFS Order starting from %c: ", vertices[start]);
while (!isEmpty(&q)) {
int curr = dequeue(&q);
fprintf(fout, "%c ", vertices[curr]);
for (int i = 0; i < n; i++) {
if (adjMatrix[curr][i] && !visited[i]) {
visited[i] = 1;
enqueue(&q, i);
}
}
}
fprintf(fout, "\n");
}

void dfsUtil(int curr, FILE *fout) {


visited[curr] = 1;
fprintf(fout, "%c ", vertices[curr]);
for (int i = 0; i < n; i++) {
if (adjMatrix[curr][i] && !visited[i]) {
dfsUtil(i, fout);
}
}
}

void dfs(int start, FILE *fout) {


memset(visited, 0, sizeof(visited));
fprintf(fout, "DFS Order starting from %c: ", vertices[start]);
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

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

Data Structures Applications Laboratory (21EECF201/23EVTF203)

return 0;
}

Output Q4 :
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

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

char nodes[N] = {'A', 'B', 'C', 'D', 'E', 'F'};

void showState(FILE *fout, int distances[], int visitedFlags[]) {


fprintf(fout, "Node\tDist\tVisited\n");
for (int i = 0; i < N; i++) {
fprintf(fout, " %c\t", nodes[i]);
if (distances[i] == MAX)
fprintf(fout, "INF\t");
else
fprintf(fout, "%d\t", distances[i]);
fprintf(fout, "%s\n", visitedFlags[i] ? "Yes" : "No");
}
fprintf(fout, "--------------------------------------------\n");
}

int findMinDist(int distances[], int visitedFlags[]) {


int minimum = MAX, index = -1;
for (int i = 0; i < N; i++) {
if (!visitedFlags[i] && distances[i] <= minimum) {
minimum = distances[i];
index = i;
}
}
return index;
}

void dijkstraAlgo(FILE *fout, int graph[N][N], int start) {


int dist[N];
int visited[N] = {0};
for (int i = 0; i < N; i++)
dist[i] = MAX;
dist[start] = 0;

fprintf(fout, "Initial distances:\n");


showState(fout, dist, visited);
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

for (int step = 0; step < N - 1; step++) {


int u = findMinDist(dist, visited);
visited[u] = 1;
for (int v = 0; v < N; v++) {
if (!visited[v] && graph[u][v] && dist[u] != MAX &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
fprintf(fout, "After visiting %c:\n", nodes[u]);
showState(fout, dist, visited);
}

fprintf(fout, "Shortest distances from %c:\n", nodes[start]);


for (int i = 0; i < N; i++) {
fprintf(fout, "%c -> %c: %d\n", nodes[start], nodes[i], (dist[i] == MAX ? -1 : dist[i]));
}
}

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

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Output Q5:
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

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

typedef struct Node {


int k;
struct Node* next;
} Node;

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;
}

void insertChaining(Node* table[], int k) {


int index = hash(k);
Node* temp = newNode(k);
temp->next = table[index];
table[index] = temp;
}

void insertLinear(int table[], int k) {


int index = hash(k);
while (table[index] != -1) {
index = (index + 1) % SIZE;
}
table[index] = k;
}
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

void insertQuadratic(int table[], int k) {


int index = hash(k);
int i = 0;
int newIndex = index;
while (table[newIndex] != -1) {
i++;
newIndex = (index + i * i) % SIZE;
}
table[newIndex] = k;
}

void printTables(FILE* out, Node* chainingTable[], int linearTable[], int quadTable[]) {


fprintf(out, "Final State of Hash Tables using h(k) = (k * %d) mod %d\n\n", 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");
}

fprintf(out, "\nLinear Probing:\n");


for (int i = 0; i < SIZE; i++) {
fprintf(out, "Index %d: ", i);
if (linearTable[i] != -1)
fprintf(out, "%d\n", linearTable[i]);
else
fprintf(out, "-\n");
}

fprintf(out, "\nQuadratic Probing:\n");


for (int i = 0; i < SIZE; i++) {
fprintf(out, "Index %d: ", i);
if (quadTable[i] != -1)
fprintf(out, "%d\n", quadTable[i]);
else
fprintf(out, "-\n");
}
}
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

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]);
}

Node* chainingTable[SIZE] = {NULL};


int linearTable[SIZE];
int quadTable[SIZE];
for (int i = 0; i < SIZE; i++) {
linearTable[i] = -1;
quadTable[i] = -1;
}

for (int i = 0; i < n; i++) {


switch (1) {
case 1: insertChaining(chainingTable, keys[i]);
}
switch (2) {
case 2: insertLinear(linearTable, keys[i]);
}
switch (3) {
case 3: insertQuadratic(quadTable, keys[i]);
}
}

printTables(out, chainingTable, linearTable, quadTable);


fclose(in);
fclose(out);
return 0;
}
KLE Technological University
School of Electronics and Communication Engineering

Data Structures Applications Laboratory (21EECF201/23EVTF203)

Output Q6:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy