二叉樹(模板)

前序:根->左子樹->右子樹  中序:左子樹->根->右子樹  後序:左子樹->右子樹->根.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
typedef struct node
{
    char date;
    node *lch,*rch;//在結構體中定義指針型的左子樹和右子樹
}Bn,*Bt;//Bn代表節點,*Bt根節點
void cbtree(Bt &T)//先序建二叉樹
{
    char c;
    scanf("%c",&c);
    if(c==',')
        T=NULL;
    else{
        T=new Bn;
        T->date=c;
        cbtree(T->lch);
        cbtree(T->rch);
    }
    return ;
}
void pre(Bt T)//先序遍歷二叉樹
{
    if(T)
    {
        printf("%c",T->date);
        pre(T->lch);
        pre(T->rch);
    }
}
void in(Bt T)//中序遍歷二叉樹
{
    if(T)
    {
        in(T->lch);
        printf("%c",T->date);
        in(T->rch);
    }
}
void post(Bt T)//後序遍歷二叉樹
{
    if(T)
    {
        post(T->lch);
        post(T->rch);
        printf("%c",T->date);
    }
}
void level(Bt T)//層次遍歷二叉樹
{
    queue<Bn>Q;
    Q.push(*T);
    while(!Q.empty())
    {
        Bn next=Q.front();
        Q.pop();
        printf("%c",next.date);
        if(next.lch)Q.push(*(next.lch));
        if(next.rch)Q.push(*(next.rch));
    }
}
int cnt=0;
void cntleaf1(Bt T)//求葉子節點
{
    if(T)
    {
        if(!T->lch&&!T->rch)
        {
            cnt++;
            return;
        }
        cntleaf1(T->lch);
        cntleaf1(T->rch);
    }
}
int cntleaf2(Bt T)//求葉子節點
{
    if(!T)return 0;
    if(!T->lch&&!T->rch)return 1;
    else
    {
        int nl=cntleaf2(T->lch);
        int nr=cntleaf2(T->rch);
        return nl+nr;
    }
}
int dep(Bt T)//求二叉樹的深度
{
    int ddep=0;
    if(!T) return ddep;
    int nl=dep(T->lch);
    int nr=dep(T->rch);
    return (nl>nr?nl:nr)+1;
}
int main()
{
    Bt head;
    //先序建二叉樹
    cbtree(head);
    //先序遍歷二叉樹
    pre(head);
    printf("\n");
    //中序遍歷二叉樹
    in(head);
    printf("\n");
    //後序遍歷二叉樹
    post(head);
    printf("\n");
    //層次遍歷二叉樹
    level(head);
    printf("\n");
    //求二叉樹的葉子節點
    cntleaf1(head);
    printf("%d\n",cnt);
    printf("%d\n",cntleaf2(head));
    //求二叉樹深度
    printf("%d\n",dep(head));
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

typedef struct node
{
    char date;
    node *lch,*rch;
}Bn,*Bt;
char pre[1000],ins[1000],post[1000];
void cbtree1(Bt &T,char *pre,char *ins,int n)//根據先序和中序建立二叉樹,n是代表字符串的長度,可以用strlen函數求出;
{
    if(n<=0)T=NULL;
    else
    {
        int k=strchr(ins,pre[0])-ins;//找到pre[0]在ins字符串中的位置,找到就返回該位置的指針,找不到就返回空指針
        T=new Bn;//找一個新的節點,並賦值給T;
        T->date=pre[0];
        cbtree1(T->lch,pre+1,ins,k);
        cbtree1(T->rch,pre+k+1,ins+k+1,n-k-1);
    }
}
void cbtree2(Bt &T,char *ins,char *post,int n)//根據中序和後序建立二叉樹
{
    if(n<=0)T=NULL;
    else
    {
        int k=strchr(ins,post[n-1])-ins;
        T=new Bn;
        T->date=post[n-1];
        cbtree2(T->lch,ins,post,k);
        cbtree2(T->rch,ins+k+1,post+k,n-k-1);
    }
}
void preorder(Bt T)//先序遍歷二叉樹
{
    if(T)
    {
        printf("%c",T->date);
        preorder(T->lch);
        preorder(T->rch);
    }
}
void postorder(Bt T)//後序遍歷二叉樹
{
    if(T)
    {
        postorder(T->lch);
        postorder(T->rch);
        printf("%c",T->date);
    }
}
int main()
{
    Bt head;
    scanf("%s%s",pre,ins);//先序和中序
    cbtree1(head,pre,ins,strlen(pre));
    postorder(head);
    //scanf("%s%s",ins,post);//中序和後序
    //cbtree2(head,ins,post,strlen(post));
    //preorder(head);
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章