#include <iostream>
#include <cctype>
using namespace std;
// A(B(#,D),C(E(#,F),#))
struct BiTree
{
char data;
struct BiTree *lchild,*rchild;
};
struct BiTree *root;
struct BiTree *CreateBiTree()
{
char ch;
struct BiTree *T;
ch=getchar();
if(ch=='#')
{
T=NULL;
}
else
{
if (!(T=(struct BiTree *)malloc(sizeof(struct BiTree))))
{
printf("Space allocated failed\n");
exit(1);
}
T->data=ch;
ch=getchar();
if(ch == ',')
{
putchar(ch);
}
else if(ch == '(')
{
T->lchild=CreateBiTree();
ch = getchar();
T->rchild=CreateBiTree();
}
else if(ch == ')'|| ch == '\n')
{
T->lchild = NULL;
T->rchild = NULL;
return T;
}
}
return T;
}
void Inorder(BiTree *T)
{
int i;
static int N=0;
if(T!=NULL)//按右根左遍历二叉树
{
N++;
Inorder(T->rchild);
printf("\t_________________\n\t");
for(i=1;i<=N;i++)
printf("| %c ",(i==N)?T->data:' ');
printf("|\n");
Inorder(T->lchild);
N--;
}
}
void main ()
{
struct BiTree *CreateBiTree();
printf("Please enter the node's to build a Binarry Tree\n");
printf("if you want to exit,please enter a single '#'\n");
root=CreateBiTree();
Inorder(root);
}
Bitree
最新推荐文章于 2022-09-09 19:48:07 发布