ADS
ADS
And
implement insert and delete operation on the constructed tree. Write contents of tree
SOURCE CODE:
#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;
if (node == NULL)
return 0;
return height(node->left) - height(node->right);
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 the unchanged node pointer
return root;
current = current->left;
return current;
if (root == NULL)
return root;
else {
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
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;
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:
break;
case 3:
inOrderTraversal(root);
printf("\n");
break;
case 4:
freeAVLTree(root);
printf("Exiting...\n");
break;
default:
return 0;
OUTPUT:
2. Construct B-Tree an order of 5 with a set of 100 random elements stored in array.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define T 3 // Minimum degree for B-Tree of order 5
int *keys;
bool leaf;
} BTreeNode;
while (!cur->leaf)
cur = cur->children[cur->n];
while (!cur->leaf)
cur = cur->children[0];
return cur->keys[0];
int idx = 0;
idx++;
return idx;
}
BTreeNode* createNode(bool leaf) {
node->n = 0;
node->leaf = leaf;
return node;
child->keys[T - 1] = node->keys[idx];
int i;
child->keys[i + T] = sibling->keys[i];
if (!child->leaf) {
child->children[i + T] = sibling->children[i];
child->n += sibling->n + 1;
node->n--;
free(sibling->keys);
free(sibling->children);
free(sibling);
int i;
if (root != NULL) {
printf("\n");
if (!root->leaf) {
parent->children[i + 1] = newChild;
newChild->n = T - 1;
fullChild->n = T - 1;
int j;
if (!fullChild->leaf) {
}
for (j = parent->n; j > i; j--)
parent->children[j + 1] = parent->children[j];
parent->children[i] = fullChild;
parent->n++;
int i = node->n - 1;
if (node->leaf) {
node->keys[i + 1] = node->keys[i];
i--;
node->keys[i + 1] = key;
node->n++;
} else {
i--;
i++;
if (node->children[i]->n == 2 * T - 1) {
splitChild(node, i);
i++;
insertNonFull(node->children[i], key);
BTreeNode *r = *root;
if (r->n == 2 * T - 1) {
BTreeNode *s = createNode(false);
*root = s;
s->children[0] = r;
splitChild(s, 0);
insertNonFull(s, key);
} else {
insertNonFull(r, key);
int i = 0;
i++;
return true;
if (root->leaf)
return false;
if (node->children[idx]->n >= T) {
node->keys[idx] = pred;
deleteKey(node->children[idx], pred);
node->keys[idx] = succ;
} else {
merge(node, idx);
deleteKey(node->children[idx], key);
}
}
int i;
node->keys[i - 1] = node->keys[i];
node->n--;
borrowFromPrev(node, idx);
borrowFromNext(node, idx);
else {
if (idx != node->n)
merge(node, idx);
else
int i;
child->keys[i + 1] = child->keys[i];
if (!child->leaf) {
child->children[i + 1] = child->children[i];
if (!node->leaf)
child->children[0] = sibling->children[sibling->n];
sibling->n--;
child->keys[child->n] = node->keys[idx];
if (!child->leaf)
child->children[child->n + 1] = sibling->children[0];
node->keys[idx] = sibling->keys[0];
int i;
sibling->keys[i - 1] = sibling->keys[i];
if (!sibling->leaf) {
sibling->children[i - 1] = sibling->children[i];
child->n++;
sibling->n--;
if (node->leaf)
removeFromLeaf(node, idx);
else
removeFromNonLeaf(node, idx);
} else {
if (node->leaf) {
return;
fill(node, idx);
else
deleteKey(node->children[idx], key);
int main() {
int i;
insert(&root, key);
printf("B-Tree structure:\n");
traverse(root, 0);
deleteKey(root, keyToDelete);
traverse(root, 0);
return 0;
}
OUTPUT:
3. Construct Min and Max Heap using arrays, delete any element and display the content
of the Heap.
SOURCE CODE:
Max Heap:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int arr[MAX_SIZE];
int size;
} MaxHeap;
void swap(int *a, int *b) {
*a = *b;
*b = temp;
largest = left;
largest = right;
if (largest != idx) {
swap(&heap->arr[idx], &heap->arr[largest]);
maxHeapify(heap, largest);
if (heap->size == MAX_SIZE) {
printf("Heap overflow\n");
return;
heap->arr[idx] = key;
idx = (idx - 1) / 2;
}
if (heap->size <= 0) {
printf("Heap is empty\n");
return;
if (heap->size == 1) {
heap->size--;
return;
heap->size--;
maxHeapify(heap, 0);
int i;
printf("\n");
int main() {
MaxHeap heap;
heap.size = 0;
insertMaxHeap(&heap, 3);
insertMaxHeap(&heap, 2);
insertMaxHeap(&heap, 15);
insertMaxHeap(&heap, 5);
insertMaxHeap(&heap, 4);
insertMaxHeap(&heap, 45);
printf("Max-Heap: ");
displayMaxHeap(&heap);
printf("Deleting root...\n");
deleteMaxHeap(&heap);
displayMaxHeap(&heap);
return 0;
OUTPUT:
MIN HEAP:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int arr[MAX_SIZE];
int size;
} MinHeap;
void swap(int *a, int *b) {
*a = *b;
*b = temp;
smallest = left;
smallest = right;
if (smallest != idx) {
swap(&heap->arr[idx], &heap->arr[smallest]);
minHeapify(heap, smallest);
if (heap->size == MAX_SIZE) {
printf("Heap overflow\n");
return;
heap->arr[idx] = key;
idx = (idx - 1) / 2;
}
if (heap->size <= 0) {
printf("Heap is empty\n");
return;
if (heap->size == 1) {
heap->size--;
return;
heap->size--;
minHeapify(heap, 0);
int i;
printf("\n");
int main() {
MinHeap heap;
heap.size = 0;
insertMinHeap(&heap, 3);
insertMinHeap(&heap, 2);
insertMinHeap(&heap, 15);
insertMinHeap(&heap, 5);
insertMinHeap(&heap, 4);
insertMinHeap(&heap, 45);
printf("Min-Heap: ");
displayMinHeap(&heap);
printf("Deleting root...\n");
deleteMinHeap(&heap);
displayMinHeap(&heap);
return 0;
OUTPUT:
4. Implement BFT and DFT for given graph, when graph is represented by
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int adjMatrix[MAX][MAX];
int visited[MAX];
int queue[MAX];
int front = -1, rear = -1;
if (rear == MAX - 1)
return;
if (front == -1)
front = 0;
queue[++rear] = vertex;
int dequeue() {
return -1;
return queue[front++];
int i;
enqueue(startVertex);
visited[startVertex] = 1;
enqueue(i);
visited[i] = 1;
int main() {
int n, i, j, startVertex;
scanf("%d", &n);
scanf("%d", &adjMatrix[i][j]);
scanf("%d", &startVertex);
BFT(startVertex, n);
return 0;
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
int visited[MAX];
int i;
visited[vertex] = 1;
DFT(i, n);
int main() {
int n, i, j, startVertex;
scanf("%d", &n);
scanf("%d", &adjMatrix[i][j]);
scanf("%d", &startVertex);
DFT(startVertex, n);
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
Node* adjList[MAX];
int visited[MAX];
int queue[MAX];
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
newNode->next = adjList[src];
adjList[src] = newNode;
newNode = createNode(src);
newNode->next = adjList[dest];
adjList[dest] = newNode;
if (rear == MAX - 1)
return;
if (front == -1)
front = 0;
queue[++rear] = vertex;
int dequeue() {
return -1;
return queue[front++];
int i;
enqueue(startVertex);
visited[startVertex] = 1;
while (temp) {
enqueue(adjVertex);
visited[adjVertex] = 1;
temp = temp->next;
int main() {
scanf("%d", &n);
adjList[i] = NULL;
while (1) {
break;
addEdge(src, dest);
scanf("%d", &startVertex);
BFT(startVertex);
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
int vertex;
} Node;
Node* adjList[MAX];
int visited[MAX];
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
newNode->next = adjList[src];
adjList[src] = newNode;
newNode = createNode(src);
newNode->next = adjList[dest];
adjList[dest] = newNode;
visited[vertex] = 1;
if (visited[connectedVertex] == 0) {
DFT(connectedVertex);
temp = temp->next;
int main() {
scanf("%d", &n);
adjList[i] = NULL;
}
while (1) {
break;
addEdge(src, dest);
scanf("%d", &startVertex);
DFT(startVertex);
return 0;
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int vertex;
} Node;
Node* adjList[MAX];
bool visited[MAX];
int timeCounter;
Node* stack[MAX];
Node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
newNode->next = adjList[src];
adjList[src] = newNode;
newNode = createNode(src);
newNode->next = adjList[dest];
adjList[dest] = newNode;
stack[++stackTop] = node;
}
Node* pop() {
if (stackTop == -1)
return NULL;
return stack[stackTop--];
pop();
node = pop();
if (node != NULL)
printf("\n");
void findBiconnectedComponents(int u) {
visited[u] = true;
int children = 0;
int v = adjListPtr->vertex;
if (!visited[v]) {
children++;
parent[v] = u;
push(adjListPtr);
findBiconnectedComponents(v);
low[u] = (low[u] < low[v]) ? low[u] : low[v];
printBiconnectedComponent(adjListPtr, u, v);
push(adjListPtr);
adjListPtr = adjListPtr->next;
int main() {
scanf("%d", &n);
adjList[i] = NULL;
visited[i] = false;
while (1) {
break;
addEdge(src, dest);
if (!visited[i]) {
findBiconnectedComponents(i);
return 0;
OUTPUT:
6. Implement Quick sort and Merge sort and observe the execution time for various
SOURCE CODE:
//MERGE SORT
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i;
int i;
printf("\n");
int n1 = m - l + 1;
int n2 = r - m;
int i,j;
int k = l;
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
}
free(L);
free(R);
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
// Main function
int main() {
int i;
generateArray(arr1, size);
double cpu_time_used;
start = clock();
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
free(arr1);
return 0;
OUTPUT:
//QUICK SORT
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
*a = *b;
*b = temp;
int i = low - 1;
int j;
for (j = low; j < high; j++) {
i++;
swap(&arr[i], &arr[j]);
return i + 1;
quickSort(arr, pi + 1, high);
int i;
int i;
printf("\n");
}
// Main function
int main() {
int i;
generateArray(arr1, size);
double cpu_time_used;
start = clock();
end = clock();
free(arr1);
getchar();
return 0;
OUTPUT:
7. Compare the performance of Single Source Shortest Paths using Greedy method when
SOURCE CODE:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
return min_index;
// array
// shortest
// included in shortest
// finalized
// false
dist[src] = 0;
sptSet[u] = true;
// picked vertex.
printSolution(dist);
}
// driver's code
int main()
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
// Function call
dijkstra(graph, 0);
return 0;
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
int deadline;
int profit;
} Job;
int i,j;
int result[n];
result[i] = -1;
break;
int totalProfit = 0;
printf("Scheduled Jobs:\n");
if (result[i] != -1) {
jobs[result[i]].id,
jobs[result[i]].deadline,
jobs[result[i]].profit);
totalProfit += jobs[result[i]].profit;
int main() {
int n,i;
scanf("%d", &n);
Job jobs[n];
jobSequencing(jobs, n);
return 0;
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
// Base Case
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
else
return max(
val[n - 1]
// Driver code
int main()
int W = 50;
return 0;
OUTPUT:
#define N 4
#include <stdbool.h>
#include <stdio.h>
{int i,j;
if(board[i][j])
printf("Q ");
else
printf(". ");
printf("\n");
int i, j;
if (board[row][i])
return false;
if (board[i][j])
return false;
// Check lower diagonal on left side
if (board[i][j])
return false;
return true;
// Queen problem
int i;
if (col >= N)
return true;
// board[i][col]
if (isSafe(board, i, col)) {
board[i][col] = 1;
return true;
board[i][col] = 0; // BACKTRACK
return false;
bool solveNQ()
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
return false;
printSolution(board);
return true;
int main()
solveNQ();
return 0;
}
OUTPUT:
#include <bits/stdc++.h>
// value of Item
struct Item
float weight;
int value;
};
struct Node
float weight;
};
// 0 as expected bound
if (u.weight >= W)
return 0;
// item index
int j = u.level + 1;
// condition
totweight += arr[j].weight;
profit_bound += arr[j].value;
j++;
if (j < n)
arr[j].weight;
return profit_bound;
{
// sorting Item on basis of value per unit
// weight.
queue<Node> Q;
Node u, v;
u.level = -1;
u.profit = u.weight = 0;
Q.push(u);
int maxProfit = 0;
while (!Q.empty())
// Dequeue a node
u = Q.front();
Q.pop();
if (u.level == -1)
v.level = 0;
if (u.level == n-1)
continue;
// update maxprofit
maxProfit = v.profit;
// consideration
Q.push(v);
v.weight = u.weight;
v.profit = u.profit;
Q.push(v);
return maxProfit;
}
int main()
return 0;
OUTPUT:
12. Implement Travelling Sales Person problem using Branch and Bound approach.
#include <bits/stdc++.h>
const int N = 4;
int final_path[N+1];
// in a particular path
bool visited[N];
// Stores the final minimum weight of shortest tour.
final_path[i] = curr_path[i];
final_path[N] = curr_path[0];
if (adj[i][k]<min && i != k)
min = adj[i][k];
return min;
if (i == j)
continue;
second = first;
first = adj[i][j];
}
adj[i][j] != first)
second = adj[i][j];
return second;
if (level==N)
if (adj[curr_path[level-1]][curr_path[0]] != 0)
// solution we got
adj[curr_path[level-1]][curr_path[0]];
copyToFinal(curr_path);
final_res = curr_res;
return;
{
if (adj[curr_path[level-1]][i] != 0 &&
visited[i] == false)
curr_weight += adj[curr_path[level-1]][i];
if (level==1)
firstMin(adj, i))/2);
else
firstMin(adj, i))/2);
curr_path[level] = i;
visited[i] = true;
curr_path);
curr_weight -= adj[curr_path[level-1]][i];
curr_bound = temp;
visited[curr_path[j]] = true;
}
// This function sets up final_path[]
int curr_path[N+1];
int curr_bound = 0;
memset(visited, 0, sizeof(curr_path));
curr_bound += (firstMin(adj, i) +
secondMin(adj, i));
curr_bound/2;
visited[0] = true;
curr_path[0] = 0;
// Driver code
int main()
};
TSP(adj);
OUTPUT:
Minimum cost : 80
Path Taken : 0 1 3 2 0