heliujin 发表于 2008-3-8 07:34

二叉树的前序创建问题的求助

我的代码如下:(我估计我是逻辑上的错误,请大家指教啊)(用C++写的)
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
struct btree
{
        char c;
    btree *left;
        btree *right;
};

btree *b=NULL;

void create(btree *p)
{
        if(p==NULL)
        {
                char m;

                cout<<"输入一个字符:";

                cin>>m;

                cout<<endl;

                if(m!='#')
                {

                p=new (btree);

                p->c=m;

                p->left=NULL;

                p->right=NULL;
               
            create(p->left);

                create(p->right);
                }
        }


}

void print(btree *p)
{
        if(p!=NULL)
        {
                cout<<p->c<<" ";

                print(p->left);

                print(p->right);
     
        }
}

int main()
{
        create(b);

        print(b);

        return 0;
}

heliujin 发表于 2008-3-8 09:23

自己顶一个吧 大家帮帮我吧。

heliujin 发表于 2008-3-8 14:50

怎么还没有人呢  我自己再顶一个

heliujin 发表于 2008-3-8 15:50

磕头了

不能吧  我再顶  帮帮我吧 小弟刚开始学 大家指点指点吧 磕头了

sunkaidong 发表于 2008-3-8 21:36

树的创建是用到递归的你的问题出现在没有说明树的度。。。而且要递归 问题create(p->left); create(p->right);在这。。。没有结束了

leeco 发表于 2008-3-8 22:10

我想你应该在create函数的参数上使用引用,否则print的将永远是“空树”。

sunkaidong 发表于 2008-3-8 23:01

#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include"malloc.h"
struct btree
{
    char c;
    btree *left;
    btree *right;
};

btree b;
int b1;
void create(btree &p,int a)
{   
        printf("你已经到了%d层",b1-a);
    if(a!=0)
    {
        char m;
        cout<<"输入一个字符:";
        cin>>m;
        cout<<endl;      
        p.left=(struct btree*)malloc(sizeof(struct btree));
                  p.right=(struct btree*)malloc(sizeof(struct btree));
                 p.c=m;
                if(a-1!=0)
                {
                   create(*p.left,a-1);
           create(*p.right,a-1);
                }  
    }
}

void print(btree &p,int a)
{
    if(a!=0)
    {   printf("你已经到了%d层\t%c\n",b1-a,p.c);
        //cout<<p.c<<" ";
        if(a-1!=0)
        {
                        print(*p.left,a-1);

            print(*p.right,a-1);
                }
     
    }
}


int main()
{   int a;
        cout<<"请输入树的度:"<<endl;
    cin>>a;
        b1=a;
    create(b,a);
    print(b,a);
    return 0;
}

[[it] 本帖最后由 sunkaidong 于 2008-3-9 09:14 编辑 [/it]]

sunkaidong 发表于 2008-3-8 23:13

写的很粗糙。。。不好意思。。。。自己可以看看啊。。用的是引用.....

[[it] 本帖最后由 sunkaidong 于 2008-3-9 10:53 编辑 [/it]]

sunkaidong 发表于 2008-3-9 10:23

#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include"malloc.h"
struct btree
{
    char c;
    btree *left;
    btree *right;
};

btree* b=NULL;
int b1;
void create(btree *p,int a)
{   
    printf("你已经到了%d层",b1-a);
    if(a!=0)
    {
        char m;
        cout<<"输入一个字符:";
        cin>>m;
        cout<<endl;      
          p->left=(struct btree*)malloc(sizeof(struct btree));
          p->right=(struct btree*)malloc(sizeof(struct btree));
         p->c=m;
        if(a-1!=0)
        {
           create(p->left,a-1);
           create(p->right,a-1);
        }  
    }
}

void print(btree *p,int a)
{
    if(a!=0)
    {   printf("你已经到了%d层\t%c\n",b1-a,p->c);
        //cout<<p.c<<" ";
        if(a-1!=0)
        {
            print(p->left,a-1);

            print(p->right,a-1);
        }
     
    }
}


int main()
{   int a;
    cout<<"请输入树的度:"<<endl;
    cin>>a;
    b1=a;
        b=(struct btree*)malloc(sizeof(struct btree));
    create(b,a);
    print(b,a);
    return 0;
}

sunkaidong 发表于 2008-3-9 10:24

上面是指针做的...前面有一个是引用做的

heliujin 发表于 2008-3-11 08:48

谢谢楼上的大哥了,您的这是层次顺序建立二叉树吧,我的那个应该改成指针的指针,你的程序写的很明了啊  谢谢了

sunkaidong 发表于 2008-3-11 09:59

是先序树...

heliujin 发表于 2008-3-12 08:43

哦  我基础太差了  是先序树,谢谢了

页: [1]

编程论坛