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

有错误,无法运行,请帮忙检查错误

xy2bl 发布于 2010-12-13 12:15, 547 次点击
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 215
typedef int Status;
typedef char TElemType;
typedef struct bitree                                                                                                                  
{
    TElemType data;
    struct bitree *lchild,*rchild;
}BiTNode, *BiTree;

BiTree Create_Tree()
{
    BiTree T;
    char a;
    scanf("%s",&a);
    if(a=='#')
        T=NULL;
    else
    {
        T= (BiTNode*) malloc (sizeof(BiTNode));
        T->data = a;
        printf("输入左孩子的值\n");
        T->lchild = Create_Tree();
        printf("输入右孩子的值\n");
        T->rchild = Create_Tree();
    }

    return T;
}

void PreorderTraverse(Bitree T)
{
   sqstack s;
   Empty(&s)
   Push(&s,root);
   Pop(&s,b);
   Bitree p,b;
   Inintstack(&s);
   while(Empty(&s))
   {
       if(root==NULL)printf("树为空树\n");
       while(Gettop(s,&p)&&p){printf("%c",p->data);Push(&s,p->Lchild);}
       Pop(&s,b);
       if(Empty(&s)){Pop(&s,b);Push(&s,b->Rchild);}
   }        
}
void InOrderTraverse(BiTree T)
{
    if(T!=NULL)
    {
        InOrderTraverse(T->lchild);
        printf("%c_  ",T->data);
        InOrderTraverse(T->rchild);
    }
}
void PostOrderTraverse(BiTree T)
{
    if(T!=NULL)
    {
        PostOrderTraverse(T->lchild);        
        PostOrderTraverse(T->rchild);
        printf("%c_  ",T->data);
    }  
}
void LevelOrder(BiTNode *bt) /*层次遍历二叉树bt*/   
{
    BiTNode* queue[MAXSIZE];
    int front,rear;
    if (bt==NULL)
        return;
     front=0;      /*非循环队列*/
     rear=0;
     queue[rear++]=bt;
     while(front!=rear)
     {
         printf("%c_",queue[front]->data);        /*访问队首结点的数据域*/
         if (queue[front]->lchild!=NULL)   /*将队首结点的左孩子指针入队列*/
         {  
             queue[rear]=queue[front]->lchild;
             rear++;
         }
         if (queue[front]->rchild!=NULL)   /*将队首结点的右孩子指针入队列*/
         {  
             queue[rear]=queue[front]->rchild; rear++;
         }
      front++;
   } /* while */
}/* LevelOrder*/
int main()
{
    BiTree root;
    root=Create_Tree();

    printf("\n先序遍历");
    PreOrderTraverse(root);

    printf("\n中序遍历");
    InOrderTraverse(root);

    printf("\n后序遍历");
    PostOrderTraverse(root);

    printf("\n层次遍历");
    LevelOrder(root);

    printf("\n");
   
   
}

有错误,无法运行,求改正
7 回复
#2
遮天云2010-12-13 14:42
m貌似楼主代码没贴完整啊,那个empty 和push pop 都没有,还有Bitree应该成BiTree ,还有堆栈也貌似没声明
#3
xy2bl2010-12-13 15:36
我将Bitree变成BiTree后,下面提示出现了很多错误
#4
xy2bl2010-12-13 15:39
错误太多,不会改
#5
xy2bl2010-12-14 12:45
唉,晕,没人帮我指点一下
#6
遮天云2010-12-14 17:58
我说楼主怎么就没看见你定义堆栈,怎么就进行了出栈进栈等操作了呢
#7
xy2bl2010-12-14 18:03
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 215
typedef int Status;
typedef char TElemType;
typedef struct bitree                                                                                                                  
{
    TElemType data;
    struct bitree *lchild,*rchild;
}BiTNode, *BiTree;
typedef struct{
     BiTNode**base;
     BiTNode**top;
    int stacksize;
}SqStack;
BiTree Create_Tree()
{
    BiTree T;
    char a;
    scanf("%s",&a);
    if(a=='#')
        T=NULL;
    else
    {
        T= (BiTNode*) malloc (sizeof(BiTNode));
        T->data = a;
        printf("输入左孩子的值\n");
        T->lchild = Create_Tree();
        printf("输入右孩子的值\n");
        T->rchild = Create_Tree();
    }

    return T;
}

void PreorderTraverse(Bitree T)
{
   sqstack s;
   Empty(&s)
   Push(&s,root);
   Pop(&s,b);
   Bitree p,b;
   Inintstack(&s);
   while(Empty(&s))
   {
       if(root==NULL)printf("树为空树\n");
       while(Gettop(s,&p)&&p){printf("%c",p->data);Push(&s,p->Lchild);}
       Pop(&s,b);
       if(Empty(&s)){Pop(&s,b);Push(&s,b->Rchild);}
   }        
}
void InOrderTraverse(BiTree T)
{
    if(T!=NULL)
    {
        InOrderTraverse(T->lchild);
        printf("%c_  ",T->data);
        InOrderTraverse(T->rchild);
    }
}
void PostOrderTraverse(BiTree T)
{
    if(T!=NULL)
    {
        PostOrderTraverse(T->lchild);        
        PostOrderTraverse(T->rchild);
        printf("%c_  ",T->data);
    }  
}
void LevelOrder(BiTNode *bt) /*层次遍历二叉树bt*/   
{
    BiTNode* queue[MAXSIZE];
    int front,rear;
    if (bt==NULL)
        return;
     front=0;      /*非循环队列*/
     rear=0;
     queue[rear++]=bt;
     while(front!=rear)
     {
         printf("%c_",queue[front]->data);        /*访问队首结点的数据域*/
         if (queue[front]->lchild!=NULL)   /*将队首结点的左孩子指针入队列*/
         {  
             queue[rear]=queue[front]->lchild;
             rear++;
         }
         if (queue[front]->rchild!=NULL)   /*将队首结点的右孩子指针入队列*/
         {  
             queue[rear]=queue[front]->rchild; rear++;
         }
      front++;
   } /* while */
}/* LevelOrder*/
int main()
{
    BiTree root;
    root=Create_Tree();

    printf("\n先序遍历");
    PreOrderTraverse(root);

    printf("\n中序遍历");
    InOrderTraverse(root);

    printf("\n后序遍历");
    PostOrderTraverse(root);

    printf("\n层次遍历");
    LevelOrder(root);

    printf("\n");
   
   
}

#8
xy2bl2010-12-14 18:04
typedef struct{
     BiTNode**base;
     BiTNode**top;
    int stacksize;
}SqStack;
1