作者:过客 | 来源:互联网 | 2023-09-23 13:10
#include#includeusingnamespacestd;typedefstructBiTNode{chardata;structBiTNode*lchi
#include
#include
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
int Create(BiTree &T)
{
char data;
cin>>data;
if(data==','){ T=NULL;}
else
{
T=new BiTNode;
T->data=data;
Create(T->lchild);
Create(T->rchild);
}
return 0;
}
void visit(BiTree T)
{
if(T->data!=',')
cout<data;
}
void InOrder(BiTree T)
{
if(T!=NULL)
{
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BiTree T)
{
if(T!=NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
int count_tree(BiTree T,int &n)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
n++;
count_tree(T->lchild,n);
count_tree(T->rchild,n);
}
return n;
}
int high_tree(BiTree T)
{
int h,left,right;
if(!T)
{ return 0;}
left=high_tree(T->lchild);
right=high_tree(T->rchild);
h=(left>right?left:right)+1;
return h;
}
int main()
{
int n,a,b=0;
BiTree T;
Create(T);
InOrder(T);
cout<<"\n";
PostOrder(T);
cout<<"\n";
a=count_tree(T,b);
cout<
SDUTOJ 2136 数据结构实验之二叉树的建立与遍历