#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct node *node_pointer;
struct node{
char value;
node_pointer left, right;
};
node_pointer tree;
int dataSets(void)
{
char data[50];
node_pointer *temp;
int i;
short int b;
scanf("%s", data);
if (!strcmp(data, "*"))
return 1;
if (!strcmp(data, "$"))
return 0;
b = dataSets();
for (i = 0; data[i]; i++) {
temp = &tree;
while (*temp) {
if (data[i] < (*temp)->value)
temp = &((*temp)->left);
else
temp = &((*temp)->right);
}
*temp = malloc(sizeof(struct node));
(*temp)->value = data[i];
(*temp)->left = (*temp)->right = NULL;
}
return b;
}
void pfs (node_pointer t)
{
printf("%c", t->value);
if (t->left)
pfs(t->left);
if (t->right)
pfs(t->right);
free(t);
}
int main()
{
while (dataSets()) {
pfs(tree);
tree = NULL;
printf("\n");
}
pfs(tree);
tree = NULL;
printf("\n");
return 0;
}
POJ-1577
最新推荐文章于 2018-01-01 14:18:21 发布