二叉树1


#include<iostream.h>
typedef struct BiTree{
char data;
struct BiTree* lchild;
struct BiTree* rchild;
}BiTree;
void CreatBitree(BiTree **temp)
{
char ch;
cin>>ch;
if (ch == '#')
*temp = NULL;
else
{
*temp = new BiTree;
if (!*temp)
{
cout<<"OVERFLOW"<<endl;
}
(*temp)->data=ch;
CreatBitree(&(*temp)->lchild);
CreatBitree(&(*temp)->rchild);
}
}
void PreOrder(BiTree *temp)
{
if  (temp == NULL)
return;
cout<<temp->data<< " ";
PreOrder(temp->lchild);
PreOrder(temp->rchild);
}
void InOrder(BiTree *temp)
{
if (temp == NULL)
return;
InOrder(temp->lchild);
cout<<temp->data<< " ";
InOrder(temp->rchild);
}
void PostOrder(BiTree * temp)
{
if (temp == NULL)
return;
PostOrder(temp->lchild);
PostOrder(temp->rchild);
cout<<temp->data<< " ";
}
void main()
{
BiTree * p;
CreatBitree(&p);
PreOrder(p);
cout<<endl;
InOrder(p);
cout<<endl;
PostOrder(p);
cout<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_41939072/article/details/80736926