Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

BINARY ST

#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *right;
struct tree *left;
struct tree *parent;
};
struct tree *ptr;
struct tree *ptr;
struct tree *root=NULL;
void insert();
void ddelete();
struct tree * deletee(struct tree *ptr);
void search();
void main()
{
int ch;
do
{
printf("1.Insert\n2.Delete\n3.Search\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: ddelete();
break;
case 3: search();
break;
case 4:exit(0);
default:printf("invalid choice");
}
}
while(1);
}
void insert()
{

struct tree *nw=(struct tree*)malloc(sizeof(struct tree));


if(nw==NULL)
{
printf("No nodes");
return;
}
printf("\nEnter the element to be inserted:");
scanf("%d",&nw->data);
nw->left=nw->right=NULL;
if(root==NULL)
{
root=nw;
root->parent=NULL;
return;
}
for(ptr=root;ptr!=NULL;)
{
if(nw->data<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=nw;
nw->parent=ptr;
return;
}
ptr=ptr->left;
}
else if(nw->data>ptr->data)
{
if(ptr->right==NULL)
{
ptr->right=nw;
nw->parent=ptr;
return;
}
ptr=ptr->right;
}
else
{
printf("%d already exist\n",nw->data);
return;
}
}
}

void ddelete()
{
struct tree *ptr=root;
int val;
if(root==NULL)
{
printf("Tree is empty");
return;
}
printf("Enter the element to be deleted:");
scanf("%d",&val);

while(ptr!=NULL)
{
if(val<ptr->data)
{
if(ptr->left==NULL)
{
printf("Does not present");
return;
}
ptr=ptr->left;
}
else if(val>ptr->data)
{
if(ptr->right==NULL)
{
printf("Does not present:");
return;
}
ptr=ptr->right;
}
else
{
break;
}
}
if(ptr->left==NULL&&ptr->right==NULL)
{
if(ptr==root)
root=NULL;
else
{
if(ptr==ptr->parent->left)
ptr->parent->left=NULL;
else
ptr->parent->right=NULL;
ptr->parent=NULL;
}
}
else if(ptr->right==NULL)
{
struct tree *y;
y=ptr->left;
if(ptr==root)
{
root=y;
y->parent=NULL;
}
else
{
y->parent=ptr->parent;
if(ptr==ptr->parent->left)
ptr->parent->left=y;
else
ptr->parent->right=y;
}
}
else if(ptr->left==NULL)
{
struct tree *y;
y=ptr->right;
if(ptr==root)
{
root=y;
y->parent=NULL;
}
else
{
y->parent=ptr->parent;
if(ptr==ptr->parent->left)
ptr->parent->left=y;
else
ptr->parent->right=y;
}
}
else
{
struct tree *y=deletee(ptr->right);
if(ptr==root)
{
y->left=ptr->left;
ptr->left->parent=y;
if(y->parent!=ptr)
{
y->parent->left=NULL;
y->right=ptr->right;
ptr->right->parent=y;
}
root=y;
root->parent=NULL;
return;
}
if(ptr==ptr->parent->left)
ptr->parent->left=y;
else
ptr->parent->right=y;
y->left=ptr->left;
ptr->left->parent=y;
if(y->parent!=ptr)
{
y->parent->left=NULL;
y->right=ptr->right;
ptr->right->parent=y;
}
y->parent=ptr->parent;
}
return;
}
struct tree * deletee(struct tree *ptr)
{
struct tree *y=ptr;
while(y->left!=NULL)
y=y->left;
return y;
}
void search()
{
struct tree *ptr=root;
int val;
if(root==NULL)
{
printf("Empty");
return;
}
printf("Enter the number to be sesrched:");
scanf("%d",&val);

while(ptr!=NULL)
{
if(val<ptr->data)
{
if(ptr->left==NULL)
{
printf("element not present");
return;
}
ptr=ptr->left;
}
else if(val>ptr->data)
{
if(ptr->right==NULL)
{
printf("not present");
return;
}
ptr=ptr->right;
}
else
{
if(ptr==root)
{
printf("%d is present as the root",val);
return;
}
printf("%d is present,");
return;
}
}
}

BFS.C
#include<stdio.h>
#include<conio.h>

int a[20][20],q[20],visited[20],n,f=-1,r=-1;

void bfs(int v)
{
int i;
for (i=0;i<n;i++) // check all the vertices in the graph
{
if(a[v][i] != 0 && visited[i] == 0) // adjacent to v and not visited
{
r=r+1;
q[r]=i; // insert them into queue
visited[i]=1; // mark the vertex visited
printf("%d ",i);
}
}
f=f+1; // remove the vertex at front of the queue
if(f<=r) // as long as there are elements in the queue
bfs(q[f]); // peform bfs again on the vertex at front of the queue
}main()
{
int v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=0;i<n;i++) // mark all the vertices as not visited
{
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
f=r=0;
q[r]=v;
printf("\n BFS traversal is:\n");
visited[v]=1; // mark the starting vertex as visited
printf("%d ",v);

bfs(v);
if(r != n-1)
printf("\n BFS is not possible");
getch();
return 0;
}

KRUSKAL

#include <stdio.h>
#include<conio.h>

#define MAX 30

typedef struct edge {


int u, v, w;
} edge;

typedef struct edge_list {


edge data[MAX];
int n;
} edge_list;

edge_list elist;

int Graph[MAX][MAX], n;
edge_list spanlist;

void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();

// Applying Krushkal Algo


void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;

for (i = 1; i < n; i++)


for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}

sort();

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


belongs[i] = i;

spanlist.n = 0;

for (i = 0; i < elist.n; i++) {


cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);

if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}

int find(int belongs[], int vertexno) {


return (belongs[vertexno]);
}

void applyUnion(int belongs[], int c1, int c2) {


int i;

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


if (belongs[i] == c2)
belongs[i] = c1;
}

// Sorting algo
void sort() {
int i, j;
edge temp;

for (i = 1; i < elist.n; i++)


for (j = 0; j < elist.n - 1; j++)
if (elist.data[j].w > elist.data[j + 1].w) {
temp = elist.data[j];
elist.data[j] = elist.data[j + 1];
elist.data[j + 1] = temp;
}
}
// Printing the result
void print() {
int i, cost = 0;

for (i = 0; i < spanlist.n; i++) {


printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}

printf("\nSpanning tree cost: %d", cost);


}

int main() {
int i, j, total_cost;
clrscr();

n = 6;

Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;

Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;

Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;

Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;

Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;

kruskalAlgo();
print();
getch();
return 0;
}

MERGE.C

#include <stdio.h>
void main()
{

int array1[50], array2[50], array3[100], m, n, i, j, k = 0;


printf("\n Enter size of array Array 1: ");
scanf("%d", &m);

printf("\n Enter sorted elements of array 1: \n");


for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}

printf("\n Enter size of array 2: ");


scanf("%d", &n);

printf("\n Enter sorted elements of array 2: \n");


for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}

i = 0;
j = 0;

while (i < m && j < n)


{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}

else
{
array3[k] = array2[j];
j++;
}
k++;
}

if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}

if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}

printf("\n After merging: \n");


for (i = 0; i < m + n; i++)
{
printf("\n%d", array3[i]);
}

ADDFUN.C

#include <stdio.h>
#include <conio.h>
int addTwoNumbers(int *, int *);

int main()
{

int fno, sno, sum;


clrscr();

printf("\n\n Pointer : Add two numbers using call by reference:\n");


printf("-------------------------------------------------------\n");

printf(" Input the first number : ");


scanf("%d", &fno);
printf(" Input the second number : ");
scanf("%d", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %d and %d is %d\n\n", fno, sno, sum);

getch();
}
int addTwoNumbers(int *n1, int *n2)
{
int sum;
sum = *n1 + *n2;
return sum;
return 0;
}

You might also like