Professional Documents
Culture Documents
DS
DS
#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()
{
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
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();
sort();
spanlist.n = 0;
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}
// Sorting algo
void sort() {
int i, j;
edge temp;
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()
{
i = 0;
j = 0;
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++;
}
}
ADDFUN.C
#include <stdio.h>
#include <conio.h>
int addTwoNumbers(int *, int *);
int main()
{
getch();
}
int addTwoNumbers(int *n1, int *n2)
{
int sum;
sum = *n1 + *n2;
return sum;
return 0;
}