# include <stdio.h>;
# include <conio.h>;
# include <alloc.h>;
# include <string.h>;
struct nod {char rom[100], eng[100]; struct nod *s, *d;};
typedef struct nod NODarb;
NODarb * r;
int n,x;
int i;
FILE * pf;
char cuv[100];
NODarb* creare(NODarb * r)
{ NODarb * q, * p;
pf=fopen("dict.txt","r");
while(!feof(pf))
{
if(r==NULL)
{
q=(NODarb*)malloc(sizeof(NODarb));
fgets(cuv,100,pf);
strcpy(q->rom,cuv);
fgets(cuv,100,pf);
strcpy(q->eng,cuv);
q->s=q->d=NULL;
r=q;//q devine radacina pentru noul arbore
}
else
{
q=(NODarb*)malloc(sizeof(NODarb));
fgets(cuv,100,pf);
strcpy(q->rom,cuv);
fgets(cuv,100,pf);
strcpy(q->eng,cuv);
q->s=q->d=NULL;
p=r;
while((p->s!=NULL)&&(p->d!=NULL))
if(strcmp(p->rom,q->rom)<0) p=p->d;
else p=p->s;
if(strcmp(p->rom,q->rom)<0) p->d=q;
else p->s=q;
}
}
fclose(pf);
return r;
}
//******************************* PARCURGERI ********************************
void RSD(NODarb * r)
{
if(r!=NULL)
{
printf("%s ",r->rom);
printf("%s\n",r->eng);
RSD(r->s);
RSD(r->d);
}
}
void SRD(NODarb * r)
{
if(r!=NULL)
{
SRD(r->s);
printf("%s ",r->rom);
printf("%s\n",r->eng);
SRD(r->d);
}
}
void SDR(NODarb * r)
{
if(r!=NULL)
{
SDR(r->s);
SDR(r->d);
printf("%s ",r->rom);
printf("%s\n",r->eng);
}
}
void main(void)
{
clrscr();
r=creare(r);
printf("\nParcurgere in preordine:\n");
RSD(r);
printf("\nParcurgere in inordine:\n");
SRD(r);
printf("\nParcurgere in postordine:\n");
SDR(r);
getche();
}
# include <stdio.h>;
# include <conio.h>;
# include <alloc.h>;
struct nod {int info; struct nod *s, *d;};
typedef struct nod NODarb;
NODarb * r;
int n,x;
int i;
NODarb* insert(NODarb * r,int x)
{ NODarb * q;
if(r==NULL)
{
q=(NODarb*)malloc(sizeof(NODarb));
q->info=x;
q->s=q->d=NULL;
r=q;//q devine radacina pentru noul arbore
}
else
{
if (r->info>x) r->s=insert(r->s,x);
else r->d=insert(r->d,x);
}
return r;
}
void RSD(NODarb * r)
{
if(r!=NULL)
{
printf("%d ",r->info);
RSD(r->s);
RSD(r->d);
}
}
void SRD(NODarb * r)
{
if(r!=NULL)
{
SRD(r->s);
printf("%d ",r->info);
SRD(r->d);
}
}
void SDR(NODarb * r)
{
if(r!=NULL)
{
SDR(r->s);
SDR(r->d);
printf("%d ",r->info);
}
}
void main(void)
{
clrscr();
printf("Intr nr de noduri: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
r=insert(r,x);
}
printf("\nParcurgere in preordine:\n");
RSD(r);
printf("\nParcurgere in inordine:\n");
SRD(r);
printf("\nParcurgere in postordine:\n");
SDR(r);
getche();
}
/*
1 2 0 0 3 4 5 0 0 6 0 0 7 0 0
RSD: 1 2 3 4 5 6 7
SRD: 2 1 5 4 6 3 7
SDR: 2 5 6 4 7 3 1
*/
# include <stdio.h>;
# include <conio.h>;
# include <alloc.h>;
struct nod {int info; struct nod *s, *d;};
typedef struct nod NODarb;
NODarb * r;
int i,n;
NODarb* creare(NODarb * r)
{
int x;
NODarb * q;
scanf("%d",&x);
if(x==0) r=NULL;
else
{
q=(NODarb*)malloc(sizeof(NODarb));
q->info=x;
q->s=q->d=NULL;
r=q;//q devine radacina pentru noul arbore
q->s=creare(q->s);
q->d=creare(q->d);
}
return r;
}
void RSD(NODarb * r)
{
if(r!=NULL)
{
printf("%d ",r->info);
RSD(r->s);
RSD(r->d);
}
}
void SRD(NODarb * r)
{
if(r!=NULL)
{
SRD(r->s);
printf("%d ",r->info);
SRD(r->d);
}
}
void SDR(NODarb * r)
{
if(r!=NULL)
{
SDR(r->s);
SDR(r->d);
printf("%d ",r->info);
}
}
void main(void)
{
clrscr();
r=creare(r);
printf("\nParcurgere in preordine:\n");
RSD(r);
printf("\nParcurgere in inordine:\n");
SRD(r);
printf("\nParcurgere in postordine:\n");
SDR(r);
getche();
}
/*
1 2 0 0 3 4 5 0 0 6 0 0 7 0 0
RSD: 1 2 3 4 5 6 7
SRD: 2 1 5 4 6 3 7
SDR: 2 5 6 4 7 3 1
*/
# include <stdio.h>;
# include <conio.h>;
# include <alloc.h>;
struct nod {int info; struct nod *s, *d;};
typedef struct nod NODarb;
NODarb * r;
int i,n;
void creare(NODarb * &r)
{
int x;
NODarb * q;
scanf("%d",&x);
if(x==0) q=NULL;
else
{
q=(NODarb*)malloc(sizeof(NODarb));
q->info=x;
q->s=q->d=NULL;
creare(q->s);
creare(q->d);
}
//trimitem ca referinta adresa lui r pt ca in subprogram aceasta se modifica
r=q;
}
void RSD(NODarb * r)
{
if(r!=NULL)
{
printf("%d ",r->info);
RSD(r->s);
RSD(r->d);
}
}
void SRD(NODarb * r)
{
if(r!=NULL)
{
SRD(r->s);
printf("%d ",r->info);
SRD(r->d);
}
}
void SDR(NODarb * r)
{
if(r!=NULL)
{
SDR(r->s);
SDR(r->d);
printf("%d ",r->info);
}
}
void main(void)
{
clrscr();
creare(r);
printf("\nParcurgere in preordine:\n");
RSD(r);
printf("\nParcurgere in inordine:\n");
SRD(r);
printf("\nParcurgere in postordine:\n");
SDR(r);
getche();
}
/*
1 2 0 0 3 4 5 0 0 6 0 0 7 0 0
RSD: 1 2 3 4 5 6 7
SRD: 2 1 5 4 6 3 7
SDR: 2 5 6 4 7 3 1
*/
# include <stdio.h>;
# include <conio.h>;
# include <alloc.h>;
struct nod {int info; struct nod *s, *d;};
typedef struct nod NODarb;
NODarb * r;
int i,n;
FILE * pf;
NODarb* creare(NODarb * r)
{
int x;
fscanf(pf,"%d",&x);
if(x==0) r=NULL;
else
{
r=(NODarb*)malloc(sizeof(NODarb));
r->info=x;
r->s=r->d=NULL;
r->s=creare(r->s);
r->d=creare(r->d);
}
return r;
}
void RSD(NODarb * r)
{
if(r!=NULL)
{
printf("%d ",r->info);
RSD(r->s);
RSD(r->d);
}
}
void SRD(NODarb * r)
{
if(r!=NULL)
{
SRD(r->s);
printf("%d ",r->info);
SRD(r->d);
}
}
void SDR(NODarb * r)
{
if(r!=NULL)
{
SDR(r->s);
SDR(r->d);
printf("%d ",r->info);
}
}
void main(void)
{
clrscr();
pf=fopen("c:\\arbore.txt","r");
r=creare(r);
printf("\nParcurgere in preordine:\n");
RSD(r);
printf("\nParcurgere in inordine:\n");
SRD(r);
printf("\nParcurgere in postordine:\n");
SDR(r);
fclose(pf);
getche();
}
/*
1 2 0 0 3 4 5 0 0 6 0 0 7 0 0
RSD: 1 2 3 4 5 6 7
SRD: 2 1 5 4 6 3 7
SDR: 2 5 6 4 7 3 1
*/
Niciun comentariu:
Trimiteți un comentariu