#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_LEN
10
struct BST
{
char data[MAX_LEN];
struct BST *lch, *rch;
};
void insert( struct BST **tree, char temp[MAX_LEN] )
{
struct BST *p = *tree;
if( p == NULL )
{
*tree = (struct BST *) malloc (sizeof(struct BST));
strcpy( (*tree)->data, temp );
(*tree)->rch = (*tree)->lch = NULL;
return;
}
while( 1 )
{
if( strcmp( p->data, temp ) > 0 )
{
if( p->lch == NULL )
{
p->lch = (struct BST *) malloc (sizeof(struct BST));
strcpy( p->lch->data, temp );
p->lch->lch = p->lch->rch = NULL;
return;
}
else
{
p = p->lch;
}
}
else
{
if( p->rch == NULL )
{
p->rch = (struct BST *) malloc (sizeof(struct BST));
strcpy( p->rch->data, temp );
p->rch->lch = p->rch->rch = NULL;
return;
}
else
{
p = p->rch;
}
}
}
}
void print( struct BST *tree )
{
if( tree )
{
printf("%s ", tree->data );
print( tree->lch);
print( tree->rch);
}
return;
}
int main(void)
{
struct BST *tree = NULL;
char temp[MAX_LEN];
while( scanf("%s", temp) )
{
insert(&tree, temp);
}
print(tree);
printf("\n");
return 0;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册