The code has been tested and can be used directly.
The point is that it's not clear--the queue.
The key is one out of the queue, a team operation.
#include <iostream>#include<stdio.h>#include<stack>#include<queue>#include<malloc.h># define MaxSize -using namespacestd;//two fork tree nodestypedefstructbtnode{Chardata; structBtnode *Lchild; structBtnode *Rchild;} Btnode;//First Order build two fork tree, big talk p187, textbook bar versionBtnode *createbitree ()//only one function is required{ Charch; Btnode*u; scanf ("%c",&ch); if(ch=='#') T=NULL; Else{T= (Btnode *)malloc(sizeof(Btnode)); T->data =ch; T->lchild =Createbitree (); T->rchild =Createbitree (); } returnT//returns the root node}//The bottom layer uses the qu[maxsize] this loop array to implement the algorithmvoidLevelorder (Btnode *T) {Btnode*p;//define work pointer pBtnode *qu[maxsize];//define ring queue, hold node pointer intFront,rear;//define team head and tail pointersfront=rear=-1;//Queue is emptyRear++; Qu[rear]=t;//The root node pointer enters the queue while(front!=rear)//the queue is not empty{Front= (front+1)%MaxSize; P=qu[front];//team head out of the queueprintf"%c", P->data);//Access Node if(P->lchild!=null)//when you have a left child, enter the team .{Rear= (rear+1)%MaxSize; Qu[rear]=p->Lchild; } if(P->rchild!=null)//when there's a right child, put it in the team .{Rear= (rear+1)%MaxSize; Qu[rear]=p->Rchild; } }}intMain () {Btnode*u; T= Createbitree ();//EstablishLevelorder (T); return 0;}
Graduated.-Two fork tree hierarchy traversal algorithm-with circular queue this data structure to realize, understanding: data structure is used to implement the algorithm