Linux环境下编译的,试过没有问题。
#include <stdio.h>
#include <stdlib.h>
//ABC^^DE^G^^F^^^
struct bi_node{
char data;
struct bi_node *lchild;
struct bi_node *rchild;
};
int create_bi_node(struct bi_node **node)
{
char ch;
int ret = 0;
struct bi_node *temp_node;
*node = NULL;
scanf("%c", &ch);
//判断节点是否空
if(ch == '^'){
return 0;
}
//分配节点内存
temp_node = malloc(sizeof(struct bi_node));
if(!temp_node)
return -1;
temp_node->data = ch;
ret = create_bi_node(&temp_node->lchild);//左节点
ret |= create_bi_node(&temp_node->rchild);//右节点
*node = temp_node;
return ret;
}
void print_pre_order(struct bi_node *node){
if(!node)
return;
printf("%c", node->data);
print_pre_order(node->lchild);
print_pre_order(node->rchild);
}
void print_in_order(struct bi_node *node){
if(!node)
return;
print_in_order(node->lchild);
printf("%c", node->data);
print_in_order(node->rchild);
}
void print_post_order(struct bi_node *node){
if(!node)
return;
print_post_order(node->lchild);
print_post_order(node->rchild);
printf("%c", node->data);
}
int main(int argc, char *argv[])
{
struct bi_node *bi_tree_node = NULL;
if(create_bi_node(&bi_tree_node) ==0)
{
printf("\nPre Order:");
print_pre_order(bi_tree_node);
printf("\nIn Order:");
print_in_order(bi_tree_node);
printf("\nPost Order:");
print_post_order(bi_tree_node);
printf("\n");
printf("Create biTree Sucess!\n\n");
}else{
printf("Create biTree error!\n");
}
}