| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 521 人关注过本帖
标题:帮忙看一下我的程序哪里有问题?
只看楼主 加入收藏
hexianwei
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-11-18
收藏
 问题点数:0 回复次数:1 
帮忙看一下我的程序哪里有问题?
下面是我同学编的一个关于二叉树的基本操作的程序,我改了很长时间都没改出来,我觉得前序。中序,后序,高度等子函数没问题,主要是层次遍历有问题,麻烦大家帮帮忙,谢谢。
#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);
}
搜索更多相关主题的帖子: include 二叉树 
2007-12-01 11:36
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
收藏
得分:0 

程序貌似是错的
2007-12-01 15:45
快速回复:帮忙看一下我的程序哪里有问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011898 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved