免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 1881 | 回复: 10
打印 上一主题 下一主题

帮忙看看这个二叉树的问题出在哪里 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-29 14:15 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>

typedef int elemtype;
typedef struct node {
        elemtype data;
        struct node *lchild, *rchild;
}BinTNode, *BinTree;
void CreatBinTree (BinTree t)
{
        char ch;
        int ap = 64;
        printf ("Enter %c :", ap ++);
        scanf ("%c", &ch);
        if (ch == ' ') t = 0;
        else {
                t = (BinTree) malloc (sizeof (BinTNode));
                CreatBinTree (t -> lchild);
                CreatBinTree (t -> rchild);
        }       
}

void preorder (BinTree t)
{
        if (t){
                printf (" %d ", t -> data);
                preorder (t -> lchild);
                preorder (t -> rchild);
        }
       
}
void main ()
{
        BinTree t;
        CreatBinTree(&t);
       
        printf ("Print Previous Order:\n");
        preorder (t);
}


有这样的提示:
Warning bintree.c: 35 assignment of pointer to pointer to struct node to pointer to struct node
0 errors, 1 warning

我是用LCC3.8运行的。
再帮我看看,想要在输入数据的时候有提示的话,如何实现,好一些啊。
多谢。

论坛徽章:
0
2 [报告]
发表于 2006-11-29 14:22 |只看该作者
CreatBinTree(&t); -〉 CreatBinTree(t);

论坛徽章:
0
3 [报告]
发表于 2006-11-29 14:40 |只看该作者
已经可以运行了,但是如果提示,这个问题,还没有解决,能不能帮我看看啊

论坛徽章:
0
4 [报告]
发表于 2006-11-29 15:48 |只看该作者
原帖由 liec 于 2006-11-29 14:40 发表
已经可以运行了,但是如果提示,这个问题,还没有解决,能不能帮我看看啊

没有看太明白?你的data用了吗?自己仔细的debug一下,就知道了!

论坛徽章:
0
5 [报告]
发表于 2006-11-29 16:10 |只看该作者

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef int elemtype;
  4. typedef struct node {
  5.         elemtype data;
  6.         struct node *lchild, *rchild;
  7. }BinTNode, * BinTree;

  8. void CreatBinTree (BinTree t)
  9. {
  10.         char ch;
  11.         scanf ("%c", &ch);
  12.         if (ch == ' ') t = 0;
  13.         else {
  14.                 t = (BinTree) malloc (sizeof (BinTNode));
  15.                 t -> data = ch;
  16.                 CreatBinTree (t -> lchild);
  17.                 CreatBinTree (t -> rchild);
  18.         }       
  19. }

  20. void preorder (BinTree t)
  21. {
  22.         if (t){
  23.                 printf (" %c ", t -> data);
  24.                 preorder (t -> lchild);
  25.                 preorder (t -> rchild);
  26.         }
  27. }
  28. void main ()
  29. {
  30.         BinTNode t;
  31.         CreatBinTree(&t);
  32.        
  33.         printf ("Print Previous Order:\n");
  34.         preorder (&t);
  35.         printf ("\n");
  36. }
复制代码


这里又改过的,但还是不行啊。如果方便的话,请大家运行一下吧,好吗?看看是哪里问题。

[ 本帖最后由 liec 于 2006-11-29 17:25 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-11-29 19:41 |只看该作者
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;

  5. #define CPP_V

  6. typedef int elemtype;
  7. typedef struct node {
  8.         elemtype data;
  9.         struct node *lchild, *rchild;
  10. }BinTNode, * BinTree;

  11. void CreatBinTree (BinTree& t/*BinTree t*/)
  12. {
  13.         char ch;
  14.         //scanf ("%c", &ch);
  15. #ifdef CPP_V
  16.                 cout << "input the ch: "; //add
  17.         cin >> ch; //add
  18. #endif
  19.                 if (ch == '0'/*ch == ' '*/) t = 0;
  20.         else {
  21.                 t = (BinTree) malloc (sizeof (BinTNode));
  22.                 t -> data = ch;
  23.                 CreatBinTree (t -> lchild);
  24.                 CreatBinTree (t -> rchild);
  25.         }        
  26. }

  27. void preorder (BinTree t)
  28. {
  29.         if (t){
  30.                 printf (" %c ", t -> data);
  31.                 preorder (t -> lchild);
  32.                 preorder (t -> rchild);
  33.         }
  34. }
  35. void main ()
  36. {
  37.         BinTree t;      //BinTNode t;
  38.                
  39.         CreatBinTree(t);
  40.         
  41.         printf ("Print Previous Order:\n");
  42.         preorder (t);
  43.         printf ("\n");
  44. }
复制代码


这样改一下,我测试了,可以正常执行,注意结束符为'0'不是' '

[ 本帖最后由 hawk2012 于 2006-11-29 19:45 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-11-29 20:14 |只看该作者
多谢了。
我的这个是用C编的啊,hawk2012的程序,我编译就报很多错啊。

论坛徽章:
0
8 [报告]
发表于 2006-11-29 21:14 |只看该作者

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef int elemtype;
  4. typedef struct node {
  5.         elemtype data;
  6.         struct node *lchild, *rchild;
  7. }BinTNode, * BinTree;

  8. BinTree CreatBinTree ()
  9. {
  10.         BinTree t;
  11.         char ch;
  12.         if ((ch = getchar())== '\n') t = 0;
  13.         else {
  14.                 t = (BinTree) malloc (sizeof (BinTNode));
  15.                 t -> data = ch;
  16.                 t -> lchild = CreatBinTree();
  17.                 t -> rchild = CreatBinTree();
  18.         }
  19.         return t;
  20. }

  21. void preorder (BinTree t)
  22. {
  23.         if (t){
  24.                 printf (" %c ", t -> data);
  25.                 preorder (t -> lchild);
  26.                 preorder (t -> rchild);
  27.         }
  28. }
  29. void main ()
  30. {
  31.         BinTree t = CreatBinTree();
  32.                
  33.         printf ("Print Previous Order:\n");
  34.         preorder (t);
  35.         printf ("\n");
  36. }
复制代码


多谢大家的帮助,现在上面的程序已经可以用了。
但是还有一点,就是如何提示才好呢。
请大家再帮帮忙!!

刚才又运行了一次,发现好像有点不对啊。它还不能建立一个完整的二叉树啊!

[ 本帖最后由 liec 于 2006-11-29 21:20 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2006-11-29 23:13 |只看该作者

程序

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

typedef int elemtype;

typedef struct node {
        elemtype data;
        struct node *lchild;
       struct node  *rchild;
}BinTNode;

typedef   BinTNode   *BinTree;

void CreatBinTree (BinTree t);
void preorder (BinTree t);


void CreatBinTree (BinTree *t)
{
        char ch;
        int ap = 64;
        printf ("Enter %c :", ap ++);
        scanf ("%c", &ch);
        if (ch == ' ') t = 0;
        else {
               *t = malloc (sizeof (BinTNode));
                CreatBinTree (&(t -> lchild));
                CreatBinTree (&(t -> rchild));
        }        
}

void preorder (BinTree t)
{
        if (t){
                printf (" %d ", t -> data);
                preorder (t -> lchild);
                preorder (t -> rchild);
        }
        
}
  


BinTree  t;/*声明为全局变量*/



void main ()
{
     
        CreatBinTree(&t);
        
        printf ("Print Previous Order:\n");
        preorder (t);
}

论坛徽章:
0
10 [报告]
发表于 2006-11-29 23:16 |只看该作者

你好

请将程序在turbo c 2.0 中运行.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP