The code is tested, and the value paste is available
#include <iostream>#include<stdio.h>#include<stack>#include<queue>#include<malloc.h>using namespacestd;//two fork tree nodestypedefstructbtnode{Chardata; structBtnode *Lchild; structBtnode *Rchild;} Btnode;//TemplatesFirst 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}//Hierarchical TraversalvoidLevelorder (Btnode *T) { //queue<btnode> queue; big bug hidden in this place; notice what's in this container of the queue .Queue<btnode *> queue;
Queue.push (T);//algorithm: root node into queue while(!queue.empty ()) {//if the queue is not empty, loop through the following 3 stepsT= Queue.front ();//Step 1: The enemy elements out of the team, the pointer from the new point, Front () method is to return the team head elementprintf"%c", T->data);//team head elements out of the team and then the team head elements of the left and right children in the queueQueue.pop ();//Pop is out of the team if(T->lchild! = NULL) {//Step 2: Left dial hand tree is not empty, the left sub-tree is enqueuedQueue.push (T->lchild);//the queue is an address element. } if(T->rchild! = NULL) {//Step 3: Right subtree is not empty, the right subtree is enqueuedQueue.push (t->rchild); } }}intMain () {Btnode*T; T= Createbitree ();//EstablishLevelorder (T); return 0;}
I graduated. C + + two fork tree hierarchy traversal