马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild, *rchild;
}node, *btree;
void CreateBtree(btree *root)
{
ElemType c;
scanf("%c", &c);
if(c == '#'){
*root = NULL;
}else{
*root = (btree)malloc(sizeof(node));
(*root)->data = c;
CreateBtree(&(*root)->lchild);
CreateBtree(&(*root)->rchild);
}
}
void CreateBtree2(node head[], int n, ElemType c[])
{
int i;
btree ptr;
for(i=1; i<n; i++)
{
ptr = &head[i];
ptr->data = c[i-1];
ptr->lchild = NULL;
ptr->rchild = NULL;
}
for(i=1; i<n; i++)
{
ptr = &head[i];
//printf("==== %c\n", ptr->data);
if(2*i+1 < n)
{
btree l = ptr->lchild = &head[2*i];
//printf("L %c \n", l->data);
btree r = ptr->rchild = &head[2*i+1];
//printf("R %c \n", r->data);
}
}
}
void in(btree root)
{
if(root)
{
in(root->lchild);
printf("[%c] ", root->data);
in(root->rchild);
}
}
void pos(btree root)
{
if(root)
{
in(root->lchild);
in(root->rchild);
printf("[%c] ", root->data);
}
}
int main(void)
{
node head[8];
btree ptr, newnode;
int i;
ElemType c[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'};
CreateBtree2(head, 8, c);
in(&head[1]);
putchar('\n');
pos(&head[1]);
putchar('\n');
return 0;
}
我相信你不相信这个结果
调用错函数了 ^_^
void pos(btree root)
{
if(root)
{
/*in(root->lchild);
in(root->rchild);*/
pos(root->lchild);
pos(root->rchild);
printf("[%c] ", root->data);
}
}
|