#include<bits/stdc++.h>
using namespace std;
typedef struct tree{
int date;
struct tree * l;
struct tree * r;
}tree;
tree * create()
{
int date;
tree * t;
cin>>date;
if(date==0)
return NULL;
else
{
t=new tree;
t->date=date;
printf("请输入%d的左子树: ",date);
t->l=create();
printf("请输入%d的右子树: ",date);
t->r=create();
}
}
void xian(tree *t)
{
if(t==NULL)
{
return;
}
xian(t->l);
xian(t->r);
cout<<t->date<<' ';
}
int FindHeight( tree* root)
{if(root == NULL )
return 0;
else
{int leftH = FindHeight ( root->l );
int rightH = FindHeight(root->r );
return max(leftH,rightH)+1;}
}
int main()
{
tree * s;
printf("diyigeui");
s=create();
xian(s);
int n= FindHeight(s);
cout<<endl;
cout<<n;
}
//用前序和中序创建二叉树
BtNode* CreateTreePI(char ps[],char is[],int n )
{
if(n<=0)
{
return NULL;
}
BtNode* p=(BtNode*)malloc(sizeof(BtNode));
int i=0;
int m;
while(i<n)
{
if(ps[0]==is[i])
{
m=i;
break;
}
++i;
}
if(i>=n)
{
return NULL;
}
p->data=ps[0];
p->leftchild=CreateTreePI(ps+1,is,m);
p->rightchild=CreateTreePI(ps+m+1,is+m+1,n-m-1);
return p;
}
int main()
{
char ps[]="ABDGHCEIF";
char is[]="GDHBAEICF";
int len=strlen(ps);
CreateTreePI(ps,is,len);
}
//用中序和后序创建二叉树
BtNode* CreateTreeIL(char is[],char ls[],int n)
{
if(n<=0)
{
return NULL;
}
BtNode *p=(BtNode*)malloc(sizeof(BtNode));
int i=0;
int m;
while(i<n)
{
if(ls[n-1]==is[i])
{
m=i;
break;
}
++i;
}
if(i>=n)
{
return NULL;
}
p->data=ls[n-1];
p->leftchild= CreateTreeIL(is,ls,m);
p->rightchild= CreateTreeIL(is+m+1,ls+m,n-m-1);
return p;
}
int main()
{
char is[]="GDHBAEICF";
char ls[]="GHDBIEFCA";
int len=strlen(is);
CreateTreeIL(is,ls,len);
}