有关二叉树的层序遍历这块有问题,大家帮忙看看
之前层序遍历这块还可以正确输出,只是说会弹出调试窗口。修改之后越来越乱,索性连正确结果都没有了,我想应该是指针出问题了,大家帮帮忙程序代码:
#include <iostream> using namespace std; struct node { node * lchild; node * rchild; char data; }; typedef node * BTREE; typedef int datatype; typedef struct queue { BTREE bt; struct queue * next; }QUEUE; struct LinkQueue { QUEUE *front; QUEUE *rear; }; BTREE CreateTree(BTREE bt); bool IsEmpty(BTREE bt); void VisitData(BTREE bt); void PreOrder(BTREE bt); void InOrder(BTREE bt); void PostOrder(BTREE bt); void LayerOrder(BTREE bt); void InitialQueue(LinkQueue *lq); BTREE FrontQueue(LinkQueue *lq); void EnQueue(LinkQueue *lq, BTREE bt); void DelQueue(LinkQueue *lq); void LayerOrder(LinkQueue *lq, BTREE bt); bool QueueEmpty(LinkQueue *lq); void DeleteQueue(LinkQueue *lq); int main() { BTREE bt = NULL; bt = CreateTree(bt); cout<<"pre order:"<<endl; PreOrder(bt); cout<<"\nIn order:"<<endl; InOrder(bt); cout<<"\nPost order:"<<endl; PostOrder(bt); cout<<"\nLayer order:"<<endl; LayerOrder(bt); return 0; } //函数功能:建立一个树 BTREE CreateTree(BTREE bt) { char ch; cout<<"data:"; cin>>ch; if(ch != '#') { bt = new node; bt->data = ch; bt->lchild = CreateTree(bt->lchild); bt->rchild = CreateTree(bt->rchild); } else { bt = NULL; } return bt; } //判断树是否为空 bool IsEmpty(BTREE bt) { if(bt != NULL) { return false; } else { return true; } } //访问数节点信息 void VisitData(BTREE bt) { if(bt != NULL) { cout<<(bt->data)<<" "; } } //对树进行先序遍历 void PreOrder(BTREE bt) { if(!IsEmpty(bt)) { VisitData(bt); PreOrder(bt->lchild); PreOrder(bt->rchild); } } //对树进行中序遍历 void InOrder(BTREE bt) { if(!IsEmpty(bt)) { InOrder(bt->lchild); VisitData(bt); InOrder(bt->rchild); } } //对树进行后序遍历 void PostOrder(BTREE bt) { if(!IsEmpty(bt)) { PostOrder(bt->lchild); PostOrder(bt->rchild); VisitData(bt); } } //初始化队列 void InitialQueue(LinkQueue *lq) { lq->front = new QUEUE; lq->front->bt = NULL; lq->front->next = NULL; lq->rear = lq->front; } //队列的第一个 BTREE FrontQueue(LinkQueue *lq) { if(lq->front != NULL) { return lq->front->bt; } return NULL; } //将元素加入到队列尾部 void EnQueue(LinkQueue *lq, BTREE bt) { if(lq->front == NULL) { lq->rear->next = new QUEUE; lq->rear = lq->rear->next; lq->rear->bt = bt; lq->rear->next = NULL; } else { lq->front = new QUEUE; lq->front->bt = bt; lq->front->next = NULL; lq->rear = lq->front; } } //将队列最前面的元素从队列中删除 void DelQueue(LinkQueue *lq) { if(lq->front != NULL) { if(lq->front->next != NULL) { lq->front = lq->front->next; } else { lq->front = NULL; lq->rear = NULL; } } } //对树进行层序遍历 void LayerOrder(BTREE bt) { LinkQueue *lq = new LinkQueue; BTREE tmp = new node; InitialQueue(lq); if(bt == NULL) return; VisitData(bt); if(bt->lchild != NULL) { EnQueue(lq, bt->lchild); } if(bt->rchild != NULL) { EnQueue(lq, bt->rchild); } while(!QueueEmpty(lq)) { DelQueue(lq); tmp = FrontQueue(lq); VisitData(tmp); if(tmp->lchild != NULL) { EnQueue(lq, tmp->lchild); } if(tmp->rchild != NULL) { EnQueue(lq, tmp->rchild); } } DeleteQueue(lq); } bool QueueEmpty(LinkQueue *lq) { if(lq->front == NULL) return true; else return false; } void DeleteQueue(LinkQueue *lq) { QUEUE *pr = lq->front, *tmp = NULL; while(pr != NULL) { tmp = pr; pr = pr->next; delete tmp; } }