注册 登录
编程论坛 数据结构与算法

根据先序遍历和中序遍历构造二叉树并实现后序遍历,那里错了,求大神指教

connectify 发布于 2013-11-10 16:23, 640 次点击
#include<iostream.h>
#include<fstream.h>
#include<math.h>
#include<stdlib.h>

ifstream fin("input.txt",ios::in);  
ofstream fout("output.txt",ios::out);

struct Lnode
{
   char  data;     
   Lnode  *lchild;
   Lnode  *rchild;
};

void postorder(Lnode *bt)
{
    if(bt)
    {
        postorder(bt->lchild);
        postorder(bt->rchild);
        fout<<bt->data;
    }
}

void InitLnode(Lnode *&bt,int i,int len,int j,char *a,char *b)
{
    int k;
    if(i<len)
    {
        bt=new Lnode;
        bt->data=a[j++];
        bt->lchild=NULL;
        bt->rchild=NULL;
    }
    for(k=i;k<len;++k)
    {
        if(b[k]==bt->data)
        {
//            break;
        InitLnode(bt->lchild,i,k,j,a,b);
        InitLnode(bt->rchild,k+1,len,j,a,b);
        }
    }
}

void main()
{
    int m,l,i=0,j=0;
    char *a,*b;
    fin>>m;
    a=new char[m];
    b=new char[m];
    for(l=0;l<m;++l)
        fin>>a[l];
    for(l=0;l<m;++l)
        fin>>b[l];
    Lnode *bt;
    InitLnode(bt,i,m,j,a,b);
    postorder(bt);

    delete []a;
    delete []b;
    delete bt;

    fin.close();
    fout.close();
}
5 回复
#2
yuccn2013-11-11 15:14
编译还是运行?具体什么问题?楼主想明确你提问什么
#3
wp2319572013-11-11 15:16
还好  前面写了一大堆的东东  没直接写  哪里错了
#4
connectify2013-11-13 21:28
回复 2楼 yuccn
已经调出来了
#5
connectify2013-11-13 21:28
回复 3楼 wp231957
已经弄好了,谢谢
#6
付得福2013-11-13 23:06
哪里有问题呢?
1