【无标题】

#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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值