首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

二叉树的有关问题,紧急

2012-02-10 
二叉树的问题,紧急求救!小弟的二叉树试验要交源程序给老师看,但是现在出了问题怎么也解决不了。建立二叉树

二叉树的问题,紧急求救!
小弟的二叉树试验要交源程序给老师看,但是现在出了问题怎么也解决不了。建立二叉树之后遍历的时候会死循环,单独看建立和遍历的部分都没有问题,不知道哪里出错了。程序在下面,哪位大虾能第一个把问题指出来帮我改错的,40分全部奉上!谢谢大家!

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

typedef   struct   bitree
{
  char   bt;
  struct   bitree   *lc,*rc;
};
struct   bitree   bt,*tree;

creatree(struct   bitree   *T)
{
  char   temp;
  temp=getche();
  if(temp== '   ')
      T=NULL;
  else
      {
        T-> bt=temp;
        T-> lc=(struct   bitree   *)malloc(sizeof(struct   bitree));
        creatree(T-> lc);
        T-> rc=(struct   bitree   *)malloc(sizeof(struct   bitree));
        creatree(T-> rc);
      }
  return   1;
}

preorder(struct   bitree   *T)
{
  if(T)
      {
        printf( "%c ",T-> bt);
        getch();
        preorder(T-> lc);
        preorder(T-> rc);
      }
}

inorder(struct   bitree   *tree)
{
  if(tree)
      {
        inorder(tree-> lc);
        printf( "%c ",tree-> bt);
        inorder(tree-> rc);
      }
}

postorder(struct   bitree   *tree)
{
  if(tree)
      {
        postorder(tree-> lc);
        postorder(tree-> rc);
        printf( "%c ",tree-> bt);
      }
}

main()
{
  int   ch=0;
  printf( "                                                                       Bitree   Demo\n\n                               1.Creat   2.Travel   3.Leaves   4.Display   5.Clear   6.Exit ");
  window(1,4,80,25);
  while(ch!=6)
      {
        printf( "\nPlease   input   your   choice: ");
        ch=(int)(getch())-48;
        printf( "\n ");
        switch(ch)
            {
              case   1   :   printf( "Please   input   the   content: ");
creatree(&bt);
printf( "\nThe   Bitree   has   been   created.   Please   press   any   key. ");
getch();
clrscr();
break;
              case   2   :   printf( "Preorder: ");
preorder(&bt);
                                printf( "\nInorder: ");


inorder(tree);
printf( "\nPostorder: ");
postorder(tree);
printf( "\nPlease   press   any   key. ");
getch();
                                clrscr();
break;
              case   3   :   break;
            }
      }
}

[解决办法]
creatree(struct bitree *T)
{
char temp;
temp=getche();
if(temp== ' ')
T=NULL;
else
{
T-> bt=temp;
T-> lc=(struct bitree *)malloc(sizeof(struct bitree));
creatree(T-> lc);
T-> rc=(struct bitree *)malloc(sizeof(struct bitree));
creatree(T-> rc);
}
return 1;
}


刚看到这里,明显错误,先malloc 个空间,然后createtree,如果空格,那么又强行把指针弄成null (其实那个也是指针的copy,那个t-> lr 永远都不会为空的,这样你的遍历不会停止)。这样写逻辑有点问题。


其他扫了一眼,ms 没看到delete tree 的地方 ……


好多都写的不规范,不知道是随便敲上来还是这样写的,换个编译器吧~
[解决办法]
举例个create tree

Node* CreateTree()
{
char a;
cin > > a ;
if (a == ' ') return NULL ;
Node* pNode = (Node* )malloc(sizeof(Node));
assert(pNode != NULL);
pNode-> bt = a ;
cout < < "Enter " < < a < < "-> lc : \n ";
pNode-> lc = CreateTree();

cout < < "Enter " < < a < < "-> rc : \n ";
pNode-> rc = CreateTree();
return pNode ;
}

热点排行