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

本文介绍了一种基于前序和中序遍历序列构造二叉树,并实现后序遍历与层次遍历的方法。提供了两种语言版本的代码:C 和 C++,帮助读者理解如何从给定的遍历序列构建二叉树并进行遍历。

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

Time Limit: 1000MSMemory Limit: 65536KB

Problem Description

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

Input

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

Output

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

Example Input
2
abdegcf
dbgeafc
xnliu
lnixu
Example Output
dgebfca
abcdefg
linux
xnuli

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    char data;
    struct node *left,*right;
};

struct node * creat(char *pre,char *mid,int len)  //通过前序遍历和后序遍历构造二叉树
{
    char *i;
    int k;
    struct node *root1;
    if(len==0)
        return NULL;
    root1=(struct node *)malloc(sizeof(struct node));
    root1->data=*pre;
    for(i=&mid[0],k=0; i<mid+len; i++,k++)
    {
        if(*i== *pre)
            break;
    }
    root1->left=creat(pre+1,mid,k);
    root1->right=creat(pre+1+k,i+1,len-1-k);
    return root1;
}

void Lastput(struct node  * root)  //后序遍历
{
    if(root)
    {
        Lastput(root->left);
        Lastput(root->right);
        printf("%c",root->data);
    }
}

void sort(struct node *root)  //层次遍历
{
    struct node *q[1005];
    int head=0,tail=0;
    if(!root)
        return;
    q[tail++]=root;
    while(head<tail)
    {
        printf("%c",q[head]->data);
        if(q[head]->left)
            q[tail++]=q[head]->left;
        if(q[head]->right)
            q[tail++]=q[head]->right;
        head++;
    }
}
int main()
{
    int t,n;
    char pre[55];
    char mid[55];
    struct node *root;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            root=(struct node *)malloc(sizeof(struct node));
            n=0;
            scanf("%s%s",pre,mid);
            n=strlen(mid);
            root=creat(pre,mid,n);
            Lastput(root);
            printf("\n");
            sort(root);
            printf("\n");
        }
    }
    return 0;
}



c++版

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

char a[55],b[55];
struct node
{
    char data;
    struct node *left,*right;
};

struct node *creat(char *pre,char *mid,int len)
{
    node *p=new node;
    char c;
    int i;
    if(len==0)  return NULL;
    p->data=pre[0];
    c=pre[0];
    for(i=0; i<len; i++)
    {
        if(c==mid[i])
            break;
    }
    p->left=creat(pre+1,mid,i);
    p->right=creat(pre+i+1,mid+1+i,len-i-1);
    return p;
};
void lastout(node *root)
{
    if(root)
    {
        lastout(root->left);
        lastout(root->right);
        cout<<root->data;
    }
}
void put(node *root)
{
    queue<node *>q;
    if(!root)  return;
    q.push(root);
    while(!q.empty())
    {
        cout<<q.front()->data;
        if(q.front()->left)
            q.push(q.front()->left);
        if(q.front()->right)
            q.push(q.front()->right);
        q.pop();
    }
}
int main()
{
    int t;
    while(cin>>t)
    {
        while(t--)
        {
            cin>>a>>b;
            node *root;
            root=creat(a,b,strlen(a));
            lastout(root);
            cout<<endl;
            put(root);
            cout<<endl;
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值