Graduated.-Two fork tree hierarchy traversal algorithm-with circular queue this data structure to realize, understanding: data structure is used to implement the algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.