Vous êtes sur la page 1sur 45

#include<stdio.

h>

#include<stdlib.h>

struct tree {

int info;

struct tree *left;

struct tree *right;

};

struct tree *insert(struct tree *,int);

void inorder(struct tree *);

void postorder(struct tree *);

void preorder(struct tree *);

struct tree *delet(struct tree *,int);

struct tree *search(struct tree *);

int main(void)

struct tree *root;

int choice, item,item_no;

root = NULL;
do {

do {

printf("\n \t 1. Insert in Binary Tree ");

printf("\n\t 2. Delete from Binary Tree ");

printf("\n\t 3. Inorder traversal of Binary tree");

printf("\n\t 4. Postorder traversal of Binary tree");

printf("\n\t 5. Preorder traversal of Binary tree");

printf("\n\t 6. Search and replace ");

printf("\n\t 7. Exit ");

printf("\n\t Enter choice : ");

scanf(" %d",&choice);

if(choice<1 || choice>6)

printf("\n Invalid choice - try again");

while (choice<1 || choice>6);

switch(choice) {

case 1:

printf("\n Enter new element: ");

scanf("%d", &item);

root= insert(root,item);

printf("\n root is %d",root->info);

printf("\n Inorder traversal of binary tree is : ");

inorder(root);

break;

case 2:

printf("\n Enter the element to be deleted : ");


scanf(" %d",&item_no);

root=delet(root,item_no);

inorder(root);

break;

case 3:

printf("\n Inorder traversal of binary tree is : ");

inorder(root);

break;

case 4:

printf("\n Postorder traversal of binary tree is : ");

postorder(root);

break;

case 5:

printf("\n Preorder traversal of binary tree is : ");

preorder(root);

break;

default:

printf("\n End of program ");

while(choice !=6);

return(0);

struct tree *insert(struct tree *root, int x) {

if(!root) {

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


root->info = x;

root->left = NULL;

root->right = NULL;

return(root);

if(root->info > x)

root->left = insert(root->left,x); else {

if(root->info < x)

root->right = insert(root->right,x);

return(root);

void inorder(struct tree *root) {

if(root != NULL) {

inorder(root->left);

printf(" %d",root->info);

inorder(root->right);

return;

void postorder(struct tree *root) {

if(root != NULL) {

postorder(root->left);

postorder(root->right);

printf(" %d",root->info);

}
return;

void preorder(struct tree *root) {

if(root != NULL) {

printf(" %d",root->info);

preorder(root->left);

preorder(root->right);

return;

struct tree *delet(struct tree *ptr,int x) {

struct tree *p1,*p2;

if(!ptr) {

printf("\n Node not found ");

return(ptr);

} else {

if(ptr->info < x) {

ptr->right = delet(ptr->right,x);

} else if (ptr->info >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;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

void insert();

void delete2();

void inorder(struct btnode *t);

void create();

void search(struct btnode *t);

void preorder(struct btnode *t);

void postorder(struct btnode *t);

void search1(struct btnode *t,int data);


int smallest(struct btnode *t);

int largest(struct btnode *t);

int flag = 1;

int main()

int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Delete an element from the tree\n");

printf("3 - Inorder Traversal\n");

printf("4 - Preorder Traversal\n");

printf("5 - Postorder Traversal\n");

printf("6 - Exit\n");

while(1)

printf("\nEnter your choice : ");

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 :

printf("Wrong choice, Please enter correct choice ");

break;

void insert()

create();

if (root == NULL)

root = temp;

else

search(root);

void create()

{
int data;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

void search(struct btnode *t)

if ((temp->value > t->value) && (t->r != NULL))

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL))

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL))

search(t->l);

else if ((temp->value < t->value) && (t->l == NULL))

t->l = temp;

void inorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

if (t->l != NULL)
inorder(t->l);

printf("%d -> ", t->value);

if (t->r != NULL)

inorder(t->r);

void delete2()

int data;

if (root == NULL)

printf("No elements in a tree to delete");

return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);

void preorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display");

return;

}
printf("%d -> ", t->value);

if (t->l != NULL)

preorder(t->l);

if (t->r != NULL)

preorder(t->r);

void postorder(struct btnode *t)

if (root == NULL)

printf("No elements in a tree to display ");

return;

if (t->l != NULL)

postorder(t->l);

if (t->r != NULL)

postorder(t->r);

printf("%d -> ", t->value);

void search1(struct btnode *t, int data)

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();

void delete1(struct btnode *t)

int k;

if ((t->l == NULL) && (t->r == NULL))

if (t1->l == t)

t1->l = NULL;

else

t1->r = NULL;

t = NULL;

free(t);

return;
}

else if ((t->r == NULL))

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;

else if (t->l == NULL)

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;

else if ((t->l != NULL) && (t->r != NULL))

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);

int largest(struct btnode *t)

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;

struct node *next;

}*start=NULL,*q,*temp,*ne;

typedef struct node N;

int main()

int ch;

do

printf("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4] EXIT\t:");

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));

printf("ENTER THE ELT.TO BE INSERTED :\t");

scanf("%d",&item);

printf("ENTER ITS PRIORITY :\t");

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;

while(q->next != NULL && q->next->priority<=itprio)

q=q->next;

ne->next=q->next;

q->next=ne;

void del()

if(start==NULL)

printf("\nQUEUE UNDERFLOW\n");

else

ne=start;

printf("\nDELETED ITEM IS %d\n",ne->info);

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)

printf("\n%d priority =%d\n",temp->info,temp->priority);}}}

#include<stdio.h>

#include<stdlib.h>

#define MAX 20

typedef struct Q

int data[MAX];

int R,F;

}Q;

typedef struct node

{
struct node *next;

int vertex;

}node;

void enqueue(Q *,int);

int dequeue(Q *,int);

int empty(Q *);

int full(Q *);

void BFS(int);

void readgraph();

void insert(int vi,int vj);

void DFS(int i);

int visited[MAX];

node *G[20];

int n;

int main()

int i,op;

do

printf("\n\n1)Create\n2)BFS\n3)DFS\n4)Quit");

printf("\nEnter Your Choice: ");

scanf("%d",&op);

switch(op)

case 1:readgraph();break;

case 2:printf("\nStarting Node No. : ");

scanf("%d",&i);
BFS(i);break;

case 3:for(i=0;i<n;i++)

visited[i]=0;

printf("\nStarting Node No. : ");

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;

int empty(Q *P)

if(P->R==-1)

return(1);

return(0);
}

int full(Q *P)

if(P->R==MAX-1)

return(1);

return(0);

void enqueue(Q *P,int x)

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 dequeue(Q *P,int s)

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;

printf("\nEnter no. of vertices :");

scanf("%d",&n);

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

G[i]=NULL;

printf("\nEnter no of edges :");

scanf("%d",&no_of_edges);

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

printf("\nEnter an edge (u,v) :");

scanf("%d%d",&vi,&vj);

insert(vi,vj);

insert(vj,vi);

void insert(int vi,int vj)

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 visited[10]= {0},min,mincost=0,cost[10][10];

int main()

printf("\n Enter the number of nodes:");


scanf("%d",&n);

printf("\n Enter the adjacency matrix:\n");

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)

printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);

mincost+=min;

visited[b]=1;
}

cost[a][b]=cost[b][a]=999;

printf("\n Minimun cost=%d",mincost);

getch();

#include<stdio.h>

int main()

int array[100],search,c,n;

printf("Enter the Number of elements\n");

scanf("%d",&n);

printf("Enter the Elements\n");

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

scanf("%d", &array[c]);

printf("Enter the number to search\n");

scanf("%d", &search);

for(c=0;c<n;c++)
{

if(array[c] == search)

printf("%d is present at location %d.\n",search,c+1);

break;

if(c==n)

printf("%d is not present in array.\n",search);

return 0;

#include<stdio.h>

int BinarySearch(int *array, int n, int key)


{

int low = 0, high = n-1, mid;

while(low <= high)

mid = (low + high)/2;

if(array[mid] < key)

low = mid + 1;

else if(array[mid] == key)

return mid;

else if(array[mid] > key)

high = mid-1;

return -1;

int main()

int n=5;

int array[n];

int i,key,index;

printf("\nEnter the 5 elements");

//scanf("%d",&n);
printf("\nEnter the elements");

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

scanf("%d",&array[i]);

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

if(array[i] < array[i - 1])

printf("Given input is not sorted\n");

return 0;

printf("Enter the key to be searched");

scanf("%d",&key);

index = BinarySearch(array,n,key);

if(index==-1)

printf("Element not found\n");

else

printf("Element is at index %d\n",index);

return 0;

}
#include <stdio.h>

int main()

int data[100],i,n,step,temp;

printf("Enter the number of elements to be sorted: ");

scanf("%d",&n);

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

printf("%d. Enter element: ",i+1);

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;

printf("In ascending order: ");

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

printf("%d ",data[i]);

return 0;

#include <stdio.h>

int main()

int data[100],i,n,steps,temp;

printf("Enter the number of elements to be sorted: ");

scanf("%d",&n);

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

printf("%d. Enter element: ",i+1);


scanf("%d",&data[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;

printf("In ascending order: ");

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

printf("%d ",data[i]);

return 0;

}
#include<stdio.h>

int main()

int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");

scanf("%d",&count);

printf("Enter %d elements: ", 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 hasht(int key)

int i ;

i = key%tsize ;

return i;
}

int rehashl(int key)

int i ;

i = (key+1)%tsize ;

return i ;

int rehashq(int key, int j)

int i ;

i = (key+(j*j))%tsize ;

return i ;

int main()

int key,arr[20],hash[20],i,n,s,op,j,k ;

printf ("Enter the size of the hash table: ");

scanf ("%d",&tsize);

printf ("\nEnter the number of elements: ");

scanf ("%d",&n);

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

hash[i]=-1 ;

printf ("Enter Elements: ");

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

scanf("%d",&arr[i]);
}

do

printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option:");

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 ;

printf("\nThe elements in the array are: ");

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

printf("\n Element at position %d: %d",i,hash[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 ;

printf("\nThe elements in the array are: ");

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

printf("\n Element at position %d: %d",i,hash[i]);

break ;

}while(op!=3);

Vous aimerez peut-être aussi