二叉树(模板)

前序:根->左子树->右子树  中序:左子树->根->右子树  后序:左子树->右子树->根.

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



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值