Professional Documents
Culture Documents
Daa File
Daa File
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
};
temp->key = item;
return temp;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
else
return node;
current = current->left;
return current;
}
struct node *deleteNode(struct node *root, int key) {
else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
int main() {
inorder(root);
inorder(root);
oUtPUt :
Program of bUcket sort algo:
#include <stdio.h>
max = a[i];
return max;
int bucket[max], i;
bucket[i] = 0;
bucket[a[i]]++;
a[j++] = i;
bucket[i]--;
int main()
printArr(a, n);
bucket(a, n);
printArr(a, n);
oUtPUt :
Program of coUnting sort algo:
#include <stdio.h>
#include <string.h>
char output[strlen(arr)];
memset(count, 0, sizeof(count));
++count[arr[i]];
output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
arr[i] = output[i];
int main()
countSort(arr);
return 0;
oUtPUt:
Program of heaP sort algo:
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, N, largest);
heapify(arr, N, i);
// Heap sort
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
} }
printf("\n");
int main()
heapSort(arr, N);
printArray(arr, N);
oUtPUt:
Program of insertion sort algo:
#include <math.h>
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i;
printf("\n");
int main()
insertionSort(arr, n);
printArray(arr, n);
return 0; }
oUtPUt:
Program of merge sort algo:
#include <stdio.h>
#include <stdlib.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
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;
printf("\n"); }
int main()
printArray(arr, arr_size);
printArray(arr, arr_size);
return 0;}
oUtPUt:
Program of qUick sort algo:
#include<stdio.h>
int t = *a;
*a = *b;
*b = t;
int i
= (low- 1);
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
int i;
for (i = 0; i < size; i++)
printf("%d",arr[i]);
printf("\n");
int main()
quickSort(arr, 0, n - 1);
printf("Sorted array:");
printArray(arr, n);
return 0;
oUtPUt:
Program of radix sort algo:
#include <stdio.h>
max = a[i];
return max;
a[i] = output[i];
countingSort(a, n, place);
} printf("\n"); }
int main() {
int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122};
printArray(a,n);
radixsort(a, n);
printArray(a, n);
oUtPUt:
Program of red black tree insert algo:
#include <stdio.h>
#include <stdlib.h>
struct node {
int d;
int c;
struct node* p;
struct node* r;
struct node* l;
};
if (trav == NULL)
return temp;
trav->l->p = trav;
trav->r->p = trav;
return trav;
temp->l = left->r;
if (temp->l)
temp->l->p = temp;
left->p = temp->p;
if (!temp->p)
root = left;
temp->p->l = left;
else
temp->p->r = left;
left->r = temp;
temp->p = left;
temp->r = right->l;
if (temp->r)
temp->r->p = temp;
right->p = temp->p;
if (!temp->p)
root = right;
temp->p->l = right;
else
temp->p->r = right;
right->l = temp;
temp->p = right;
parent_pt = pt->p;
grand_parent_pt = pt->p->p;
if (parent_pt == grand_parent_pt->l)
grand_parent_pt->c = 1;
parent_pt->c = 0;
uncle_pt->c = 0;
pt = grand_parent_pt;
else {
if (pt == parent_pt->r) {
leftrotate(parent_pt);
pt = parent_pt;
parent_pt = pt->p;
rightrotate(grand_parent_pt);
int t = parent_pt->c;
parent_pt->c = grand_parent_pt->c;
grand_parent_pt->c = t;
pt = parent_pt;
else {
grand_parent_pt->c = 1;
parent_pt->c = 0;
uncle_pt->c = 0;
pt = grand_parent_pt;
else {
if (pt == parent_pt->l) {
rightrotate(parent_pt);
pt = parent_pt;
parent_pt = pt->p;
leftrotate(grand_parent_pt);
int t = parent_pt->c;
parent_pt->c = grand_parent_pt->c;
grand_parent_pt->c = t;
pt = parent_pt;
root->c = 0;
if (trav == NULL)
return;
inorder(trav->l);
inorder(trav->r);
int main()
int n = 7;
int a[7] = { 7, 6, 5, 4, 3, 2, 1 };
temp->r = NULL;
temp->l = NULL;
temp->p = NULL;
temp->d = a[i];
temp->c = 1;
inorder(root);
return 0;
oUtPUt:
Program of shell sort algo:
#include <stdio.h>
int j;
a[j] = temp;
} }
return 0; }
int i;
int main()
printArr(a, n);
shell(a, n);
printArr(a, n);
return 0; }
oUtPUt:
Program of knaPsack Problem algo:
#include<stdio.h>
if(a>b){
return a;
} else {
return b;
} }
int i, w;
int knap[n+1][W+1];
if (i==0 || w==0)
knap[i][w] = 0;
else
knap[i][w] = knap[i-1][w];
} }
return knap[n][W]; }
int main() {
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
return 0;
oUtPUt:
Program of traVelling salesman Problem
algo:
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
int i,j;
scanf("%d",&n);
scanf("%d",&ary[i][j]);
completed[i]=0;
printf("\n");
printf("\t%d",ary[i][j]);
int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity = least(city);
if(ncity==999)
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
mincost(ncity);
int least(int c)
int i,nc=999;
int min=999,kmin;
if((ary[c][i]!=0)&&(completed[i]==0))
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
if(min!=999)
cost+=kmin;
return nc;
int main()
takeInput();
return 0;
}
oUtPUt:
Program of red black tree delete algo:
#include <stdio.h>
#include <stdlib.h>
enum nodeColor {
RED,
BLACK
};
struct rbNode {
};
newnode->data = data;
newnode->color = RED;
return newnode;
// Insert an node
ptr = root;
if (!root) {
root = createNode(data);
return;
stack[ht] = root;
dir[ht++] = 0;
if (ptr->data == data) {
stack[ht] = ptr;
ptr = ptr->link[index];
dir[ht++] = index;
if (dir[ht - 2] == 0) {
ht = ht - 2;
} else {
if (dir[ht - 1] == 0) {
} else {
yPtr = xPtr->link[1];
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
xPtr->color = RED;
yPtr->color = BLACK;
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
break; }
} else {
ht = ht - 2;
} else {
if (dir[ht - 1] == 1) {
} else {
yPtr = xPtr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
yPtr->color = BLACK;
xPtr->color = RED;
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
break;
}}}
root->color = BLACK;
// Delete a node
return;
ptr = root;
if ((data - ptr->data) == 0)
break;
stack[ht] = ptr;
dir[ht++] = diff;
ptr = ptr->link[diff];
if (ptr->link[1] == NULL) {
free(ptr);
root = NULL;
root = ptr->link[0];
free(ptr);
} else {
} else {
xPtr = ptr->link[1];
if (xPtr->link[0] == NULL) {
xPtr->link[0] = ptr->link[0];
color = xPtr->color;
xPtr->color = ptr->color;
ptr->color = color;
if (ptr == root) {
root = xPtr;
} else {
dir[ht] = 1;
stack[ht++] = xPtr;
} else {
i = ht++;
while (1) {
dir[ht] = 0;
stack[ht++] = xPtr;
yPtr = xPtr->link[0];
if (!yPtr->link[0])
break;
xPtr = yPtr;
dir[i] = 1;
stack[i] = yPtr;
if (i > 0)
yPtr->link[0] = ptr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = ptr->link[1];
if (ptr == root) {
root = yPtr;
color = yPtr->color;
yPtr->color = ptr->color;
ptr->color = color;
if (ht < 1)
return;
if (ptr->color == BLACK) {
while (1) {
pPtr->color = BLACK;
break;
} if (ht < 2)
break;
if (dir[ht - 2] == 0) {
if (!rPtr)
break;
if (rPtr->color == RED) {
rPtr->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
dir[ht] = 0;
stack[ht - 1] = rPtr;
ht++;
rPtr->color = RED;
} else {
qPtr = rPtr->link[0];
rPtr->color = RED;
qPtr->color = BLACK;
rPtr->link[0] = qPtr->link[1];
qPtr->link[1] = rPtr;
rPtr->link[1]->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
break;
} else {
if (!rPtr)
break;
if (rPtr->color == RED) {
rPtr->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
dir[ht] = 1;
stack[ht - 1] = rPtr;
ht++;
rPtr->color = RED;
} else {
qPtr = rPtr->link[1];
rPtr->color = RED;
qPtr->color = BLACK;
rPtr->link[1] = qPtr->link[0];
qPtr->link[0] = rPtr;
rPtr->link[0]->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
break;} }
ht--;
}}}
if (node) {
inorderTraversal(node->link[0]);
inorderTraversal(node->link[1]);
return;
// Driver code
int main() {
while (1) {
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to insert:");
scanf("%d", &data);
insertion(data);
break;
case 2:
scanf("%d", &data);
deletion(data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Not available\n");
break;
} printf("\n"); }
return 0;}
oUtPUt:
Program of n qUeen Problem Using
backtracking algo:
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
int n,i,j;
scanf("%d",&n);
queen(1,n);
return 0;
void print(int n)
int i,j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
printf("\n\n%d",i);
if(board[i]==j)
else
}}}
for(i=1;i<=row-1;++i)
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
int column;
for(column=1;column<=n;++column){
if(place(row,column)){
queen(row+1,n);
}}}
oUtPUt:
Program of btree algo:
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
#define MIN 2
struct BTreeNode {
};
// Create a node
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
// Insert node
int j = node->count;
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
// Split node
void splitNode(int val, int *pval, int pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode) {
int median, j;
median = MIN + 1;
else
median = MIN;
j = median + 1;
j++;
node->count = median;
} else {
*pval = node->val[node->count];
(*newNode)->link[0] = node->link[node->count];
node->count--;
int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
pos = 0;
} else {
for (pos = node->count;
if (val == node->val[pos]) {
return 0;
} else {
return 1;
return 0;
int flag, i;
if (flag)
// Search node
if (!myNode) {
return;}
*pos = 0;
} else {
return;}}
return;}
int i;
if (myNode) {
traversal(myNode->link[i]);
traversal(myNode->link[i]);}}
int main() {
insert(8);
insert(9);
insert(10);
insert(11);
insert(15);
insert(16);
insert(17);
insert(18);
insert(20);
insert(23);
traversal(root);
printf("\n");
oUtPUt:
Program of fibonacci heaP algo:
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int key;
int degree;
bool mark;
bool visited;
} NODE;
int n;
NODE *min;
int phi;
int degree;
} FIB_HEAP;
FIB_HEAP *make_fib_heap();
FIB_HEAP *make_fib_heap() {
FIB_HEAP *H;
H = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));
H->degree = 0;
return H;}
NODE *x;
if (x->child == NULL) {
} else {
print_heap(x->child);
if (x->right_sibling == n) {
break; } }}
// Inserting nodes
new->key = val;
new->degree = 0;
new->mark = false;
new->parent = NULL;
new->child = NULL;
new->visited = false;
new->left_sibling = new;
new->right_sibling = new;
if (H->min == NULL) {
H->min = new;
} else {
H->min->left_sibling->right_sibling = new;
new->right_sibling = H->min;
new->left_sibling = H->min->left_sibling;
H->min->left_sibling = new;
H->min = new; } }
(H->n)++;}
// Find min node
if (H == NULL) {
return NULL;
} else
return H->min;}
// Union operation
FIB_HEAP *Hnew;
Hnew = make_fib_heap();
Hnew->min = H1->min;
temp1 = Hnew->min->right_sibling;
temp2 = H2->min->left_sibling;
Hnew->min->right_sibling->left_sibling = H2->min->left_sibling;
Hnew->min->right_sibling = H2->min;
H2->min->left_sibling = Hnew->min;
temp2->right_sibling = temp1;
Hnew->min = H2->min;
return Hnew;}
int cal_degree(int n) {
int count = 0;
while (n > 0) {
n = n / 2;
count++; }
return count;}
// Consolidate function
int degree, i, d;
degree = cal_degree(H->n);
x = H->min;
do {
d = x->degree;
y = A[d];
NODE *exchange_help;
exchange_help = x;
x = y;
y = exchange_help;
if (y == H->min)
H->min = x;
fib_heap_link(H, y, x);
if (y->right_sibling == x)
H->min = x;
A[d] = NULL;
d++;
A[d] = x;
x = x->right_sibling;
} while (x != H->min);
H->min = NULL;
if (A[i] != NULL) {
A[i]->left_sibling = A[i];
A[i]->right_sibling = A[i];
if (H->min == NULL) {
H->min = A[i];
} else {
H->min->left_sibling->right_sibling = A[i];
A[i]->right_sibling = H->min;
A[i]->left_sibling = H->min->left_sibling;
H->min->left_sibling = A[i];
if (H->min == NULL) {
H->min = A[i];
// Linking
y->right_sibling->left_sibling = y->left_sibling;
y->left_sibling->right_sibling = y->right_sibling;
if (x->right_sibling == x)
H->min = x;
y->left_sibling = y;
y->right_sibling = y;
y->parent = x;
if (x->child == NULL) {
x->child = y;
y->right_sibling = x->child;
y->left_sibling = x->child->left_sibling;
x->child->left_sibling->right_sibling = y;
x->child->left_sibling = y;
x->child = y;
(x->degree)++;
// Extract min
if (H->min == NULL)
else {
NODE *pntr;
pntr = temp;
NODE *x = NULL;
if (temp->child != NULL) {
x = temp->child;
do {
pntr = x->right_sibling;
(H->min->left_sibling)->right_sibling = x;
x->right_sibling = H->min;
x->left_sibling = H->min->left_sibling;
H->min->left_sibling = x;
H->min = x;
x->parent = NULL;
x = pntr;
(temp->left_sibling)->right_sibling = temp->right_sibling;
(temp->right_sibling)->left_sibling = temp->left_sibling;
H->min = temp->right_sibling;
H->min = NULL;
else {
H->min = temp->right_sibling;
consolidate(H);
H->n = H->n - 1;
return temp;
return H->min;
NODE *temp_parent_check;
if (node_to_be_decrease == node_to_be_decrease->right_sibling)
parent_node->child = NULL;
node_to_be_decrease->left_sibling->right_sibling = node_to_be_decrease->right_sibling;
node_to_be_decrease->right_sibling->left_sibling = node_to_be_decrease->left_sibling;
if (node_to_be_decrease == parent_node->child)
parent_node->child = node_to_be_decrease->right_sibling;
(parent_node->degree)--;
node_to_be_decrease->left_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = node_to_be_decrease;
H->min->left_sibling->right_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = H->min;
node_to_be_decrease->left_sibling = H->min->left_sibling;
H->min->left_sibling = node_to_be_decrease;
node_to_be_decrease->parent = NULL;
node_to_be_decrease->mark = false;
NODE *aux;
aux = parent_node->parent;
if (aux != NULL) {
if (parent_node->mark == false) {
parent_node->mark = true;
} else {
cascading_cut(H, aux); } }}
NODE *parent_node;
if (H == NULL) {
return;
if (node_to_be_decrease == NULL) {
else {
} else {
node_to_be_decrease->key = new_key;
parent_node = node_to_be_decrease->parent;
NODE *find_use = n;
NODE *f = NULL;
find_use->visited = true;
if (find_use->key == key) {
find_use->visited = false;
f = find_use;
decrease_key(H, f, new_key);
if (find_use->child != NULL) {
if ((find_use->right_sibling->visited != true)) {
find_use->visited = false;
FIB_HEAP *insertion_procedure() {
FIB_HEAP *temp;
NODE *new_node;
temp = NULL;
if (temp == NULL) {
temp = make_fib_heap();
scanf("%d", &no_of_nodes);
scanf("%d", &ele);
NODE *p = NULL;
p = extract_min(H);
if (p != NULL)
else
heap = NULL;
while (1) {
scanf("%d", &operation_no);
switch (operation_no) {
case 1:
heap = make_fib_heap();
break;
case 2:
if (heap == NULL) {
heap = make_fib_heap();
scanf("%d", &no_of_nodes);
scanf("%d", &ele);
break;
case 3:
min_node = find_min_node(heap);
if (min_node == NULL)
else
break;
case 4:
if (heap == NULL) {
break;
h1 = insertion_procedure();
printf("Unified Heap:\n");
print_heap(heap->min);
break;
case 5:
if (heap == NULL)
else {
extracted_min = extract_min(heap);
print_heap(heap->min);
break;
case 6:
if (heap == NULL)
else {
scanf("%d", &dec_key);
scanf("%d", &new_key);
find_use = heap->min;
find_node(heap, find_use, dec_key, new_key);
print_heap(heap->min);
break;
case 7:
if (heap == NULL)
else {
scanf("%d", &dec_key);
Delete_Node(heap, dec_key);
print_heap(heap->min);
break;
case 8:
print_heap(heap->min);
break;
case 9:
free(new_node);
free(heap);
exit(0);
default:
#include <string.h>
int i, j, m, n, LCS_table[20][20];
void lcsAlgo() {
m = strlen(S1);
n = strlen(S2);
LCS_table[i][0] = 0;
LCS_table[0][i] = 0;
} else {
lcsAlgo[index] = '\0';
int i = m, j = n;
i--;
j--;
index--; }
else
j--;
int main() {
lcsAlgo();
printf("\n");
oUtPUt:
Program of hUffman coding algo:
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 50
struct MinHNode {
char item;
unsigned freq;
};
struct MinHeap {
unsigned size;
unsigned capacity;
};
// Create nodes
temp->item = item;
temp->freq = freq;
return temp;
minHeap->size = 0;
minHeap->capacity = capacity;
return minHeap;
// Function to swap
*a = *b;
*b = t;}
// Heapify
smallest = left;
smallest = right;
if (smallest != idx) {
swapMinHNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest); }}
// Check if size if 1
// Extract min
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
// Insertion function
++minHeap->size;
int i = minHeap->size - 1;
i = (i - 1) / 2; }
minHeap->array[i] = minHeapNode;}
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
while (!checkSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
} return extractMin(minHeap);
int i;
printf("%d", arr[i]);
printf("\n");
if (root->left)
{ arr[top] = 0;
printHCodes(root->left, arr, top + 1);
if (root->right)
arr[top] = 1;
if (isLeaf(root))
printArray(arr, top); }}
// Wrapper function
}}
int main() {
printf("\n--------------------\n");
}
Program of hamiltonian cycle
#include<stdio.h>
#define V 5
if (graph [ path[pos-1] ][ v ] == 0)
return false;
if (path[i] == v)
return false;
return true;
if (pos == V)
return true;
else
return false;
path[pos] = v;
return true;
path[pos] = -1;
} }
return false;
path[i] = -1;
path[0] = 0;
return false;
printSolution(path);
return true;
printf("\n");
int main()
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
hamCycle(graph1);
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
return 0;
oUtPUt:
Program of actiVity selection Problem:
#include <stdio.h>
{ int i, j;
i = 0;
i = j; }}}
int main()
int s[] = { 1, 3, 0, 5, 8, 5 };
index
2. BUCKET SORT
3. COUNTING SORT
4. HEAP SORT
5. INSERTION SORT
6. MERGE SORT
7. QUICK SORT
8. RADIX SORT
9. RBT INSERT
15. B-TREE
17. LCS
printMaxActivities(s, f, n);
return 0;
oUtPUt: