先序赋值中序遍历二叉树
运行结果截图:
源代码:
#include<stdio.h>
#include<stdlib.h>
typedef char TEleType;
typedef struct BiTNode
{
TEleType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//中序遍历
void InoderTraverse(BiTree T)
{
if(T)
{
InoderTraverse(T->lchild);
printf("%c",T->data);
InoderTraverse(T->rchild);
}
}
//先序赋值
void CreateTree(BiTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
{
T=NULL;
}
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
CreateTree(T->lchild);
CreateTree(T->rchild);
}
}
//主函数
int main()
{
BiTree T;
printf("通过先序赋值并以#结束:\n");
CreateTree(T);
printf("中序遍历:");
InoderTraverse(T);
printf("\n");
return 0;
}