数据结构实验之求二叉树后序遍历和层次遍历

本文介绍了一种根据二叉树的前序遍历和中序遍历序列,求得后序遍历和层次遍历的方法。通过示例演示了如何使用C++实现这一过程,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构实验之求二叉树后序遍历和层次遍历

Time Limit: 1000MS Memory limit: 65536K

题目描述

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

输入

 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。

输出

每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列

示例输入

2
abdegcf
dbgeafc
xnliu
lnixu

示例输出

dgebfca
abcdefg
linux
xnuli

提示

这段代码中包含大部分二叉树题的函数,读者可以根据题目变换主函数来做不同的二叉树的题


#include<bits/stdc++.h>
using namespace std;
struct node
{
    char data;
    struct node *l;
    struct node *r;
};
int p;
string c;
struct node *creat(int n,char a[],char b[])   //根据前序中序来还原二叉树的函数
{
    struct node *root;
    char *p;
    if(n==0)
        return NULL;
    root=(struct node *)malloc(sizeof(struct node));
    root->data=a[0];
    for(p=b;p!='\0';p++)
        if(*p==a[0])
        break;
    int t;
    t=p-b;
    root->l=creat(t,a+1,b);
    root->r=creat(n-t-1,a+t+1,p+1);
    return root;
};
void leavel1(struct node *root)  //求叶子的函数
{
    p=0;
    queue<node *>s;
    if(root!=NULL)
        s.push(root);
    while(!s.empty())
    {
        root = s.front();
        if(!root->r&&!root->l)
        {
            cout<<root->data;
        }
        s.pop();
        if(root->l)
            s.push(root->l);
        if(root->r)
            s.push(root->r);
    }
}
void leavel(struct node *root)      //求叶子数的函数
{
    p=0;
    queue<node *>s;
    if(root!=NULL)
        s.push(root);
    while(!s.empty())
    {
        root = s.front();
        if(!root->r&&!root->l)
        {
            p++;
        }
        s.pop();
        if(root->l)
            s.push(root->l);
        if(root->r)
            s.push(root->r);
    }
}
struct node *creat1()    //根据含有逗号的序列求二叉树的函数
{
    int i;
    struct node *root;
    if(c[i++] == ',')
        root = NULL;
    else
    {
        root = (struct node *)malloc(sizeof(struct node));
        root->data = c[i - 1];
        root->l = creat1();
        root->r = creat1();
    }
    return root;
}
void middle(struct node *root)      //已知二叉树求中序的函数
{
    if(root)
    {
        middle(root->l);
        printf("%c",root->data);
        middle(root->r);
    }
}
void visit(struct node *root)            //已知二叉树求层次遍历的函数
{
    p=0;
    queue<node *>s;
    if(root!=NULL)
        s.push(root);
    while(!s.empty())
    {
        root = s.front();
        cout<<root->data;
        s.pop();
        if(root->l)
            s.push(root->l);
        if(root->r)
            s.push(root->r);
    }
}
void last(struct node *root)        //已知二叉树,求后序遍历的函数
{
    if(root)
    {
        last(root->l);
        last(root->r);
        printf("%c",root->data);
    }
}
 int depth(struct node *t)          //求深度(高度)的函数
{
    int ld, rd;
    if(!t) return 0;
    else
    {
        ld=depth(t->l);
        rd=depth(t->r);
        if(ld>rd)
            return ld+1;
        else
            return rd+1;
    }


}
int first(struct node *root)       //求前(先)序遍历的函数
{
    int h,l,r;
    if(!root)
        return 0;
    else
    {
        l=first(root->l);
        r=first(root->r);
        if(l>r)
            return l+1;
        else
            return r+1;
    }
}
int main()
{
    char s1[1000],s2[1000];
    int n;
    struct node *root;
    root=new node;
    cin>>n;
    while(n--)
    {
        cin>>s1;
        cin>>s2;
        int len1=strlen(s1);
        root=creat(len1,s1,s2);
        last(root);
        cout<<endl;
        visit(root);
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值