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

求大侠帮忙:先序非递归建立二叉树,然后中序非递归输出各节点。

罗马龙骑士 发布于 2012-11-14 13:53, 413 次点击
#include<iostream>
using namespace std;
struct binnode
{
char data;
binnode *lchild,*rchild;
};
typedef binnode *bintree;
struct seqstack
{
bintree data[100];
int top;
}s;
seqstack push(seqstack s,bintree x)
{
s.top++;
s.data [s.top ]=x;
return s;
}
seqstack pop(seqstack s)
{
s.top --;
return s;
}

bintree createtree()        //先序输入                                            

                                                
{
    bool right = false;                  
    bintree p,root;
    seqstack s;
    s.top=0;
    char ch;
    cin>>ch;
    if(ch=='#')
         root=NULL;
    else
    {
        root=(binnode*)malloc(sizeof(bintree));
        root->data=ch;
        root->lchild=root->rchild=NULL;
        push( s, root);
    }
    cin>>ch;
    while(!(s.top==0)||ch!='#')                                
     {
        if(ch!='#'&& right == false)                    
        {    p=root;
             p=(binnode*)malloc(sizeof(bintree));
             p->data=ch;
             p->lchild=p->rchild=NULL;
             root->lchild=p;
             root=p;
             push( s, root);
         }
         if (ch =='#')
         {
             pop( s);
             right = true;
         }
        if (ch!='#'&& right == true)
         {
             p=(binnode *)malloc(sizeof(bintree));
             p->data=ch;
             p->lchild=p->rchild=NULL;
             root->rchild=p;
             root=p;
             push( s,  root);
             right = false;
         }
         cin>>ch;
    }
     return root;
}

void inordertree(bintree t)
{
    bintree p=t;
    seqstack s;
    s.top =0;
    cout<<"output inorder root:"<<endl;
    while(p||s.top >0)
        {
            if(p)
                {
                    s.data [s.top ++]=p;
                    p=p->lchild ;
                }
            else
                {
                    p=s.data [--s.top ];
                    cout<<p->data ;
                    p=p->rchild ;
                }
        }
        cout<<endl;
}

void main()
{
    bintree root;
    cout<<"请先序输入各结点以建立二叉树:"<<endl;
    root=createtree();
    inordertree(root);

}
5 回复
#2
寒风中的细雨2012-11-14 16:47
抛出问题    给出    执行案列
#3
罗马龙骑士2012-11-15 11:40
求大侠帮忙:先序非递归建立二叉树,然后中序非递归输出各节点时,只输岀了一个结点就结束了,不知建立二叉树时错在哪儿?
求大侠帮忙:先序非递归建立二叉树,然后中序非递归输出各节点时,只输岀了一个结点就结束了,不知建立二叉树时错在哪儿?
#4
寒风中的细雨2012-11-15 11:45
回复 3楼 罗马龙骑士
希望你给出一组数据   在建树的时候  是怎么操作的

#5
罗马龙骑士2012-11-15 21:22
例如输入:AB##C##时,只输出B 程序就结束了。
#6
寒风中的细雨2012-11-16 15:43
程序代码:
  while(!(s.top==0)||ch!='#')                              
      {
         if(ch!='#'&& right == false)                  
         {    p=root;
              p=(binnode*)malloc(sizeof(bintree));
              p->data=ch;
              p->lchild=p->rchild=NULL;
              root->lchild=p;
              root=p;
              push( s, root);
          }
          if (ch =='#')
          {
              pop( s);
              right = true;
          }
         if (ch!='#'&& right == true)
          {
              p=(binnode *)malloc(sizeof(bintree));
              p->data=ch;
              p->lchild=p->rchild=NULL;
              root->rchild=p;
              root=p;
              push( s,  root);
              right = false;
          }
          cin>>ch;
     }

这块 逻辑有问题     最好还是仿照 书本上的来写  虽然难看懂点......
1