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

#include <stdio.

h>
#include <stdlib.h>
#include <conio.h>
typedef struct nod_arbore{
char cheie;
struct nod_arbore *stg,*dr;
}NOD_ARBORE;
NOD_ARBORE *construire(FILE *f)
{
int n;
char c;
NOD_ARBORE *p;
n=sizeof(NOD_ARBORE);
fscanf(f,"%c",&c);
if(c=='*') return 0;
else
{
p=(NOD_ARBORE*)malloc(n);
p->cheie=c;
p->stg=construire(f);
p->dr=construire(f);
}
return p;
}
void preordine(NOD_ARBORE *p,int nivel)
{
int i;
if(p!=0)
{
for(i=0;i<=nivel;i++)
printf(" ");
printf("%c\n",p->cheie);
preordine(p->stg,nivel+1);
preordine(p->dr,nivel+1);
}
}
void inordine(NOD_ARBORE *p, int nivel)
{
int i;
if(p!=0)
{
inordine(p->stg,nivel+1);
for(i=0;i<=nivel;i++)
printf(" ");
printf("%c\n",p->cheie);
inordine(p->dr,nivel+1);
}
}
void postordine(NOD_ARBORE *p,int nivel)
{

int i;
if(p!=0)
{
postordine(p->stg,nivel+1);
postordine(p->dr,nivel+1);
for(i=0;i<=nivel;i++)
printf(" ");
printf("%c\n",p->cheie);
}
}
int main()
{
FILE *f=fopen("arbori binari.txt","r+");
NOD_ARBORE *rad;
rad=construire(f);
printf("AFISARE PREORDINE:\n");
preordine(rad,0);
printf("\n\nAFISARE INORDINE:\n");
inordine(rad,0);
printf("\n\nAFISARE POSTORDINE:\n");
postordine(rad,0);
fclose(f);
return 0;
}

You might also like