Descripción
Conocer una secuencia de caracteres introducidos en orden, como abd, por ejemplo, cf, (donde, representa un nodo vacío). Cree el árbol binario y genere todos los nodos hoja del árbol binario de arriba a abajo y de izquierda a derecha.
Aporte
Los datos de entrada tienen varias líneas y cada línea es una cadena de menos de 50 caracteres.
Producción
Genere los nodos hoja del árbol binario de arriba a abajo y de izquierda a derecha.
Muestra
Aporte
abd, p. ej., cf,
xnl, i, u,
Salida
dfg
uli
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int k;
char a[100];
struct node
{
char data;
struct node * ltree,* rtree;
};
void cengci(struct node * root)
{
if(root==NULL)
{
return ;
}
int f,r;
f=1;r=1;
struct node * s[100],*p;
s[1]=root;
while(f<=r)
{
p=s[f++];
if(p->rtree==NULL&&p->ltree==NULL)
{
printf("%c",p->data);
}
if(p->ltree!=NULL)
{
s[++r]=p->ltree;
}
if(p->rtree!=NULL)
{
s[++r]=p->rtree;
}
}
}
struct node * create(struct node * root)
{
root=(struct node *)malloc(sizeof(struct node));
if(a[k]!=',')
{
root->data=a[k++];
root->ltree=create(root->ltree);
root->rtree=create(root->rtree);
}
else if(a[k]==',')
{
root=NULL;
k++;
}
return root;
}
int main()
{
struct node * root;
while(gets(a))
{
k=0;;
root=create(root);
cengci(root);
printf("\n");
}
return 0;
}