当前位置: 代码迷 >> C语言 >> 帮忙看一下我的程序哪里有问题?
  详细解决方案

帮忙看一下我的程序哪里有问题?

热度:145   发布时间:2007-12-01 11:36:22.0
帮忙看一下我的程序哪里有问题?
下面是我同学编的一个关于二叉树的基本操作的程序,我改了很长时间都没改出来,我觉得前序。中序,后序,高度等子函数没问题,主要是层次遍历有问题,麻烦大家帮帮忙,谢谢。
#include <stdio.h>
#define ERROR 0
#define  OK   1
#define OVERFLOW  -1

typedef struct BiTNode
{char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;



typedef struct Qnode
{BiTNode* data; /*char改为BiTNode*/
struct Qnode *next;
}QNode;


typedef struct
{QNode *front;
QNode *rear;
}queue;


int initqueue(queue *Q)
{Q->front=Q->rear=(QNode*)malloc(sizeof(QNode));
if(!Q->front)
   exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}

enqueue(queue *Q,BiTNode *x)
{QNode *p;
p=(QNode*)malloc(sizeof(QNode));
p->data=x;p->next=NULL;Q->rear->next=p;Q->rear=p;
}

outqueue(queue *Q,BiTNode *x) /*该函数有改动*/
{QNode *p;
p=Q->front->next;
Q->front->next=p->next;
x=p->data;
free(p);
}

int emptyqueue(queue *Q)
{if(Q->front==Q->rear)
return OK;
return ERROR;
}



BiTNode *createBinTree(BiTNode *root)
{char c;
scanf("%c",&c);
if(c=='@')
    root=NULL;
else
    {root=(BiTNode *)malloc(sizeof(BiTNode));
     root->data=c;
     root->lchild=createBinTree(root->lchild);
     root->rchild=createBinTree(root->rchild);
    }
return root;
}


int preordertraverse(BiTNode *T)
{if(T!=NULL)
  {printf("%c",T->data);
   preordertraverse(T->lchild);
   preordertraverse(T->rchild);
  }
}


void  inordertraverse(BiTNode *T)
{if(T!=NULL)
  {inordertraverse(T->lchild);
   printf("%c",T->data);
   inordertraverse(T->rchild);
  }
}


void postordertraverse(BiTNode *T)
{if(T!=NULL)
   {postordertraverse(T->lchild);
    postordertraverse(T->rchild);
    printf("%c",T->data);
    }
}


int height(BiTNode *T)
{int m,n;
if(T==NULL)
   return 0;
else
   {m=height(T->lchild);
    n=height(T->rchild);
    return(m>n)?m+1:n+1;
    }
}


/*void leveltraveltree(BiTNode *root) /*该函数有改动*/
{queue Q;BiTNode *p,*x;p=root;
initqueue(&Q);
if(p!=NULL)
  { enqueue(&Q,p);
while(!emptyqueue(&Q))
   {outqueue(&Q,x);
    if(x!=NULL)
     {printf("%c",x->data);
       enqueue(&Q,x->lchild);
       enqueue(&Q,x->rchild);
     }
    }
  }
}*/


main()
{BiTNode *root=NULL;
printf("Please input er cha shu de jie dian zi fu:");
root=createBinTree(root);
printf("xian xu bian li xu lie shi:\n");
preordertraverse(root);
printf("zhong xu bian li xu lie shi:\n");
inordertraverse(root);
printf("hou xu bian li xu lie shi:\n");
postordertraverse(root);
printf("The height is:");
printf("%d",height(root));
printf("an ceng ci bian li xu lie shi:\n");
leveltraveltree(root);
}
----------------解决方案--------------------------------------------------------

程序貌似是错的
----------------解决方案--------------------------------------------------------
  相关解决方案