二叉树

来源:互联网 发布:快刀抢票软件 编辑:程序博客网 时间:2024/05/16 14:34

我的心愿是世界和平!

二叉树

根据先序遍历建树,输出其先序遍历(根左右)、中序遍历(左根右)、后序遍历(左右根)、层次遍历、叶子结点(没有子孩子即度为0的结点)、高度。

#include<stdio.h>#include<iostream>#include<string.h>#include<queue>using namespace std;int num;typedef struct shu{    char ch;    struct shu *left;    struct shu *right;} Tree,*tree;void jianshu(tree &T){    char c;    cin>>c;    if(c=='\n')        return ;    else if(c=='#')        T=NULL;    else    {        T=new Tree;        T->ch=c;        jianshu(T->left);        jianshu(T->right);    }}void xianxu(tree &T){    if(T!=NULL)    {        cout<<T->ch;        xianxu(T->left);        xianxu(T->right);    }}void zhongxu(tree &T){    if(T!=NULL)    {        zhongxu(T->left);        cout<<T->ch;        zhongxu(T->right);    }}void houxu(tree &T){    if(T!=NULL)    {        houxu(T->left);        houxu(T->right);        cout<<T->ch;    }}void cengci(tree &T){    if(T!=NULL)    {        tree p=T;        queue<tree>q;        q.push(p);        while(!q.empty())        {            p=q.front();            cout<<p->ch;            q.pop();            if(p->left!=NULL)                q.push(p->left);            if(p->right!=NULL)                q.push(p->right);        }    }}int yezijiedian1(tree &T){    if(T==NULL)        return 0;    if(T->left==NULL&&T->right==NULL)        return 1;    return yezijiedian1(T->left)+yezijiedian1(T->right);}void yezijiedian2(tree &T){    if(T!=NULL)    {        if(T->left==NULL&&T->right==NULL)            num++;        yezijiedian2(T->left);        yezijiedian2(T->right);    }}int gaodu(tree &T){    if(T==NULL)        return 0;    else    {        int l=gaodu(T->left);        int r=gaodu(T->right);        return 1+max(l,r);    }}int main(){    tree T;    num=0;    jianshu(T);    xianxu(T);    cout<<endl;    zhongxu(T);    cout<<endl;    houxu(T);    cout<<endl;    cengci(T);    cout<<endl;    cout<<yezijiedian1(T)<<endl;    yezijiedian2(T);    cout<<num<<endl;    cout<<gaodu(T)<<endl;    return 0;}

已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 。

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;typedef struct shu{    char ch;    struct shu *left;    struct shu *right;}tree;tree *jianshu(char *xx,char *hx,int l){    if(l<=0)        return NULL;    tree *T=new tree;    T->ch=*hx;    int i=0;    while(i<l)    {        if(*hx==*(xx+i))            break;        i++;    }    T->right=jianshu(xx+i+1,hx-1,l-i-1);    T->left=jianshu(xx,hx-l+i,i);    return T;}void xianxu(tree *T){    if(T!=NULL)    {        cout<<T->ch;        xianxu(T->left);        xianxu(T->right);    }}int main(){    int m;    char xx[57],hx[57];    cin>>m;    while(m--)    {        cin>>xx>>hx;        tree *T=jianshu(xx,hx+strlen(hx)-1,strlen(xx));        xianxu(T);        if(m>=1)            cout<<endl;    }    return 0;}

已知一棵二叉树的先序遍历序列和中序遍历序列,求二叉树的后序遍历 。

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;typedef struct shu{    char ch;    struct shu *left;    struct shu *right;}tree;tree* jianshu(char *xx,char *zx,int l){    if(l<=0)        return NULL;    tree *T=new tree;    T->ch=*xx;    int i=0;    while(i<l)    {        if(*xx==*(zx+i))            break;        i++;    }    T->right=jianshu(xx+i+1,zx+i+1,l-i-1);    T->left=jianshu(xx+1,zx,i);    return T;}void houxu(tree *T){    if(T!=NULL)    {        houxu(T->left);        houxu(T->right);        cout<<T->ch;    }}int main(){    int l;    char xx[57],zx[57];    while(cin>>l)    {        int g;        cin>>xx>>zx;        tree *T=jianshu(xx,zx,l);        houxu(T);    }    return 0;}
原创粉丝点击