Various traversal algorithms of binary tree

Source: Internet
Author: User

#include <iostream>#include <stack>#include <queue>using namespace STD;//Two fork tree nodetypedef structbitnode{//Data    CharData//Left child hands    structBitnode *lchild,*rchild; }bitnode,*bitree;//Create a two-fork tree by ordinal sequenceintCreatebitree (Bitree &t) {CharData///Enter the value (one character) of the node in the binary tree in order of precedence, ' # ' indicates an empty tree    scanf("%c", &data);if(Data = =' # ') {T = NULL; }Else{T = (bitree)malloc(sizeof(Bitnode));//Generate root nodeT->data = data;//Construct left sub-treeCreatebitree (T->lchild);//Construct right sub-treeCreatebitree (T->rchild); }return 0; }//OutputvoidVisit (Bitree T) {if(T->data! =' # '){printf("%c", T->data); }  }//First Order traversalvoidPreorder (Bitree T) {if(T! = NULL) {//Access root nodeVisit (T);//access to left dial hand nodesPreorder (T->lchild);//Access right sub-nodePreorder (T->rchild); }  }//Middle sequence traversalvoidInorder (Bitree T) {if(T! = NULL) {//access to left dial hand nodesInorder (T->lchild);//Access root nodeVisit (T);//Access right sub-nodeInorder (T->rchild); }    }// post-traversalvoidPostorder (Bitree T) {if(T! = NULL) {//access to left dial hand nodesPostorder (T->lchild);//Access right sub-nodePostorder (T->rchild);//Access root nodeVisit (T); }  }/ * First-order traversal (non-recursive) idea: After accessing T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then the first traversal of the right sub-tree of T. */  voidPreOrder2 (Bitree T) { Stack<BiTree> Stack;//p is a traversal pointerBitree p = T;//stack is not empty or p is not empty when the loop     while(P | |!Stack. empty ()) {if(P! = NULL) {//deposit in stack            Stack. push (P);//Access root node            printf("%c", P->data);//traverse the left subtreep = p->lchild; }Else{//Stack backp =Stack. Top ();Stack. Pop ();//access to right sub-treep = p->rchild; }      }//while}/ * Sequence traversal (non-recursive) idea: T is to traverse the root of the tree pointer, the middle order traversal requires that after traversing the left dial hand tree, access to the root, and then traverse the right subtree. First, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, access to T->data, and then the right subtree of t traversal. */  voidInOrder2 (Bitree T) { Stack<BiTree> Stack;//p is a traversal pointerBitree p = T;//stack is not empty or p is not empty when the loop     while(P | |!Stack. empty ()) {if(P! = NULL) {//deposit in stack            Stack. push (P);//traverse the left subtreep = p->lchild; }Else{//fallback, Access root nodep =Stack. Top ();printf("%c", P->data);Stack. Pop ();//access to right sub-treep = p->rchild; }      }//while}//post-traversal (non-recursive)typedef structbitnodepost{Bitree Bitree;CharTag }bitnodepost,*bitreepost;voidPostOrder2 (Bitree T) { Stack<BiTreePost> Stack;//p is a traversal pointerBitree p = T; Bitreepost BT;//stack is not empty or p is not empty when the loop     while(P! = NULL | |!Stack. empty ()) {//traverse the left subtree         while(P! = NULL) {BT = (bitreepost)malloc(sizeof(Bitnodepost)); Bt->bitree = p;//Visited left dial hand treeBt->tag =' L ';Stack. push (BT);          p = p->lchild; }///left/right sub-tree access complete access to root node         while(!Stack. Empty () && (Stack. Top ())->tag = =' R ') {BT =Stack. Top ();//Stack back            Stack. Pop ();//bt->bitree;             printf("%c", Bt->bitree->data); }//Traverse Right sub-tree        if(!Stack. empty ()) {BT =Stack. Top ();//access to right sub-treeBt->tag =' R ';              p = bt->bitree;          p = p->rchild; }      }//while}//Hierarchical TraversalvoidLevelorder (Bitree t) {bitree p = t;//Queue     queue<BiTree> Queue;//The root node is enqueued    Queue. push (P);//Queue not empty loop     while(!Queue. empty ()) {//Enemy elements out of the teamp =Queue. Front ();//Access node of P point        printf("%c", P->data);//Exit queue        Queue. Pop ();//Left dial hand tree is not empty, the left sub-tree is enqueued        if(P->lchild! = NULL) {Queue. push (P->lchild); }//Right subtree is not empty, the right subtree is enqueued        if(P->rchild! = NULL) {Queue. push (P->rchild); }      }  }intMain () {Bitree T; Createbitree (T);printf("First order traversal: \ n"); Preorder (T);printf("\ n");printf("First order traversal (non-recursive): \ n"); PreOrder2 (T);printf("\ n");printf("middle order traversal: \ n"); Inorder (T);printf("\ n");printf("Middle sequence traversal (non-recursive): \ n"); InOrder2 (T);printf("\ n");printf("Post-post traversal: \ n"); Postorder (T);printf("\ n");printf("post-post traversal (non-recursive): \ n"); PostOrder2 (T);printf("\ n");printf("Hierarchical traversal: \ n"); Levelorder (T);printf("\ n");return 0; }


Various traversal algorithms of binary tree

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.