Ads Lab Manual (Micro)
Ads Lab Manual (Micro)
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>
// Update heights
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
// Main function
int main()
{
struct Node* root = NULL;
// Inserting nodes
root = insert(root, 1);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 5);
root = insert(root, 6);
root = insert(root, 3);
return 0;
}
OUT PUT:
#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;
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 8
write a c program to Implement Job sequencing with deadlines using Greedy
strategy.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
int max(int a, int b)
{
return (a > b)? a : b;
}
// Returns the maximum value that can be put in a knapsack of capacity W
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
return K[n][W];
}
int main()
{
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("\nValue = %d", knapsack(W, wt, val, n));
getch();
return 0;
}
OUTPUT:
//WEEK -10
//C program to solve N Queen Problem using backtracking
#define N 4
#include <stdbool.h>
#include <stdio.h>
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) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
OUTPUT:
. . Q .
Q . . .
. . . Q
. Q . .
WEEK 11
Write a C program Using Backtracking strategy to solve 0/1 Knapsack problem
#include <stdio.h>
#define MAX_ITEMS 100
int maxProfit = 0;
int n;
int W;
int weights[MAX_ITEMS];
int values[MAX_ITEMS];
int main()
{
int i;
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the maximum weight capacity of the knapsack: ");
scanf("%d", &W);
OUTPUT: