Académique Documents
Professionnel Documents
Culture Documents
h>
#include<stdlib.h>
struct tree {
int info;
};
int main(void)
root = NULL;
do {
do {
scanf(" %d",&choice);
if(choice<1 || choice>6)
switch(choice) {
case 1:
scanf("%d", &item);
root= insert(root,item);
inorder(root);
break;
case 2:
root=delet(root,item_no);
inorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
preorder(root);
break;
default:
while(choice !=6);
return(0);
if(!root) {
root->left = NULL;
root->right = NULL;
return(root);
if(root->info > x)
if(root->info < x)
root->right = insert(root->right,x);
return(root);
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right);
return;
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info);
}
return;
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right);
return;
if(!ptr) {
return(ptr);
} else {
if(ptr->info < x) {
ptr->right = delet(ptr->right,x);
ptr->left=delet(ptr->left,x);
return ptr;
} else
if(ptr->info == x)
if(ptr->left == ptr->right)
{
free(ptr);
return(NULL);
} else if(ptr->left==NULL)
p1=ptr->right;
free(ptr);
return p1;
} else if(ptr->right==NULL)
p1=ptr->left;
free(ptr);
return p1;
} else {
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2;
return(ptr);
}
#include <stdio.h>
#include <stdlib.h>
struct btnode
int value;
void insert();
void delete2();
void create();
int flag = 1;
int main()
int ch;
printf("\nOPERATIONS ---");
printf("6 - Exit\n");
while(1)
scanf("%d", &ch);
switch (ch)
case 1:
insert();
break;
case 2:
delete2();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
break;
void insert()
create();
if (root == NULL)
root = temp;
else
search(root);
void create()
{
int data;
scanf("%d", &data);
temp->value = data;
search(t->r);
t->r = temp;
search(t->l);
t->l = temp;
if (root == NULL)
return;
if (t->l != NULL)
inorder(t->l);
if (t->r != NULL)
inorder(t->r);
void delete2()
int data;
if (root == NULL)
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
if (root == NULL)
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
if (root == NULL)
return;
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
if ((data>t->value))
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
t1 = t;
search1(t->l, data);
else if ((data==t->value))
delete2();
int k;
if (t1->l == t)
t1->l = NULL;
else
t1->r = NULL;
t = NULL;
free(t);
return;
}
if (t1 == t)
root = t->l;
t1 = root;
else if (t1->l == t)
t1->l = t->l;
else
t1->r = t->l;
t = NULL;
free(t);
return;
if (t1 == t)
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL)
k = smallest(t->r);
flag = 1;
else
k =largest(t->l);
flag = 2;
search1(root, k);
t->value = k;
}
int smallest(struct btnode *t)
t2 = t;
if (t->l != NULL)
t2 = t;
return(smallest(t->l));
else
return (t->value);
if (t->r != NULL)
t2 = t;
return(largest(t->r));
else
return(t->value);
}
#include<stdio.h>
#include<malloc.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);
main( *)malloc(sizeof(struct node));
) root = NULL;
{
bool
ht_in
c;
int
info ;
int
choic
e;
struc
t
node
*root
=
(stru
ct
node
while(1)
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}
}
}
struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;
if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = 1;
break;
case 1:
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}
}
}
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1:
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0:
pptr->balance = -1;
break;
case -1:
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}
*ht_inc = FALSE;
}
}
}
return(pptr);
}
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}
}
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}
I
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
int priority;
int info;
}*start=NULL,*q,*temp,*ne;
int main()
int ch;
do
scanf("%d",&ch);
switch(ch)
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
while(ch<4);
void insert()
int item,itprio;
ne=(N*)malloc(sizeof(N));
scanf("%d",&item);
scanf("%d",&itprio);
ne->info=item;
ne->priority=itprio;
ne->next=NULL;
if(start==NULL )
//new->next=start;
start=ne;
else if(start!=NULL&&itprio<=start->priority)
ne->next=start;
start=ne;
}
else
q=start;
q=q->next;
ne->next=q->next;
q->next=ne;
void del()
if(start==NULL)
printf("\nQUEUE UNDERFLOW\n");
else
ne=start;
start=start->next;
void display()
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY\n");
else
printf("QUEUE IS:\n");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct Q
int data[MAX];
int R,F;
}Q;
{
struct node *next;
int vertex;
}node;
void BFS(int);
void readgraph();
int visited[MAX];
node *G[20];
int n;
int main()
int i,op;
do
printf("\n\n1)Create\n2)BFS\n3)DFS\n4)Quit");
scanf("%d",&op);
switch(op)
case 1:readgraph();break;
scanf("%d",&i);
BFS(i);break;
case 3:for(i=0;i<n;i++)
visited[i]=0;
scanf("%d",&i);
DFS(i);break;
}while(op!=4);
void BFS(int v)
int w,i,visited[MAX];
Q q;
node *p;
q.R=q.F=-1;
for(i=0;i<n;i++)
visited[i]=0;
enqueue(&q,v);
printf("\nVisit\t%d",v);
visited[v]=1;
while(!empty(&q))
v=dequeue(&q,0);
for(p=G[v];p!=NULL;p=p->next)
w=p->vertex;
if(visited[w]==0)
{
enqueue(&q,w);
visited[w]=1;
printf("\nvisit\t%d",w);
void DFS(int i)
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
if(P->R==-1)
return(1);
return(0);
}
if(P->R==MAX-1)
return(1);
return(0);
if(P->R==-1)
P->R=P->F=0;
P->data[P->R]=x;
else
P->R=P->R+1;
P->data[P->R]=x;
int x;
x=P->data[P->F];
if(P->R==P->F)
P->R=-1;
P->F=-1;
else
P->F=P->F+1;
return(x);
void readgraph()
int i,vi,vj,no_of_edges;
scanf("%d",&n);
for(i=0;i<n;i++)
G[i]=NULL;
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
scanf("%d%d",&vi,&vj);
insert(vi,vj);
insert(vj,vi);
node *p,*q;
q=(node *)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else {
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int main()
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
visited[1]=1;
printf("\n");
while(ne<n)
for (i=1,min=999;i<=n;i++)
for (j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0)
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
getch();
#include<stdio.h>
int main()
int array[100],search,c,n;
scanf("%d",&n);
scanf("%d", &array[c]);
scanf("%d", &search);
for(c=0;c<n;c++)
{
if(array[c] == search)
break;
if(c==n)
return 0;
#include<stdio.h>
low = mid + 1;
return mid;
high = mid-1;
return -1;
int main()
int n=5;
int array[n];
int i,key,index;
//scanf("%d",&n);
printf("\nEnter the elements");
scanf("%d",&array[i]);
return 0;
scanf("%d",&key);
index = BinarySearch(array,n,key);
if(index==-1)
else
return 0;
}
#include <stdio.h>
int main()
int data[100],i,n,step,temp;
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",&data[i]);
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
if(data[i]>data[i+1])
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
#include <stdio.h>
int main()
int data[100],i,n,steps,temp;
scanf("%d",&n);
for(i=0;i<n;++i)
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
if(data[steps]>data[i])
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
#include<stdio.h>
int main()
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i<count;i++)
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0))
number[j+1]=number[j];
j=j-1;
number[j+1]=temp;
}
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
#include <stdio.h>
int tsize;
int i ;
i = key%tsize ;
return i;
}
int i ;
i = (key+1)%tsize ;
return i ;
int i ;
i = (key+(j*j))%tsize ;
return i ;
int main()
int key,arr[20],hash[20],i,n,s,op,j,k ;
scanf ("%d",&tsize);
scanf ("%d",&n);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
}
do
scanf("%d",&op);
switch(op)
case 1:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
i = rehashl(i);
hash[i]=key ;
for (i=0;i<tsize;i++)
break ;
case 2:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
i = rehashq(i,j);
j++ ;
hash[i]=key ;
for (i=0;i<tsize;i++)
break ;
}while(op!=3);