Binary tree creation and traversal

Source: Internet
Author: User

Disclaimer: This article is plagiarism http://blog.csdn.net/sjf0115/article/details/8645991 please view the original blog

#include <iostream>#include <stack>#include <queue>using namespace STD;//Two fork tree node data structuretypedef structbitnode{CharData//Data    structBitnode * LCHILD;//Left child hands    structBitnode * RCHILD;} Bitnode, *bitree;//post-traversal (non-recursive) needs to be usedtypedef structbitnodepost{Bitree Bitree;Chartag;} Bitnodepost, *bitreepost;intCreatebitree (Bitree & Root);//Create two-fork treevoidPreorder (Bitree root);//Sequential Traversal binary tree recursionvoidInorder (Bitree root);//middle sequence Traversal binary tree recursionvoidPostorder (Bitree root);//post-traversal binary tree recursionvoidPreOrder2 (Bitree root);//sequential traversal binary tree non-recursivevoidInOrder2 (Bitree root);//middle sequence traversal binary tree non-recursivevoidPostOrder2 (Bitree root);//middle sequence traversal binary tree non-recursivevoidLevelorder (Bitree root);//Level traversal binary treeintMain () {bitree root = NULL; Createbitree (root);cout<<"\ n First Order traversal---recursion"<<endl; Preorder (root);cout<<"\ n First Order traversal---non-recursive"<< Endl; PreOrder2 (root);cout<<"\ n Sequence Traversal---recursion"<< Endl; Inorder (root);cout<<"\ n Sequence traversal---non-recursive"<< Endl; InOrder2 (root);cout<<"\ nthe post-traversal---recursion"<< Endl; Postorder (root);cout<<"\ nthe post-traversal---non-recursive"<< Endl; PostOrder2 (root);cout<<"\ n Hierarchy Traversal"<< Endl; Levelorder (root);return 0;}/************************ function: First order Create two-fork tree * function parameter: two fork tree root node ************************/intCreatebitree (Bitree & Root) {CharDataCin>> data;if(' # '= = data) root = NULL;Else{root = (Bitree)malloc(sizeof(Bitnode));        Root->data = data;        Createbitree (Root->lchild);    Createbitree (Root->rchild); }return 0;}/************************ function: First order traversal binary tree (Recursive) * function parameter: two fork tree root node ************************/voidPreorder (Bitree root) {if(NULL = = root) {return; }Else{cout<< Root->data <<" ";        Preorder (Root->lchild);    Preorder (Root->rchild); }}/************************ function function: Middle sequence traversal binary tree (Recursive) * function parameter: two fork tree root node ************************/voidInorder (Bitree root) {if(NULL = = root) {return; }Else{inorder (root->lchild);cout<< Root->data <<" ";    Inorder (Root->rchild); }}/************************ function: Follow-up traversal of Binary tree (recursive) * function parameter: two fork tree root node ************************/voidPostorder (Bitree root) {if(NULL = = root) {return; }Else{postorder (root->lchild); Postorder (Root->rchild);cout<< Root->data <<" "; }}/************************ function: First order traversal binary tree (non-recursive) * function parameter: two fork tree root node * idea: After accessing Root->data, t into the stack, * traverse the left subtree; * When the Zuozi returns, the top element of the stack should be T , * out of the stack, and then sequence through the right subtree of T. ************************/voidPreOrder2 (Bitree root) { Stack<BiTree> Stack; Bitree p = root;//p to move the pointer     while(!Stack. Empty () | | P) {if(p) {cout<< P->data <<" ";Stack. push (P);        p = p->lchild; }Else{p =Stack. Top ();Stack. Pop ();        p = p->rchild; }    }}/************************ function function: Middle sequence traversal binary tree (non-recursive) * function parameter: two fork tree root node ************************/voidInOrder2 (Bitree root) { Stack<BiTree> Stack; Bitree p = root;//p to move the pointer     while(!Stack. Empty () | | P) {if(p) {Stack. push (P);        p = p->lchild; }Else{p =Stack. Top ();cout<< P->data <<" ";Stack. Pop ();        p = p->rchild; }    }}/**************************** function: Post-order traversal of binary tree (non-recursive) * function parameter: two fork tree root node * idea: T is to traverse the root of the tree pointer, the order traversal request after traversing the left and right subtree, then access the root. It is necessary to determine whether the left and right subtrees of the root node have been traversed. ****************************/voidPostOrder2 (Bitree root) { Stack<BiTreePost> Stack; Bitree p = root;//p is a traversal pointerBitreepost BT; while(P! = NULL | |!Stack. empty ())//stack is not empty or p is not empty when the loop{ while(P! = NULL)//traverse the left subtree{BT = (bitreepost)malloc(sizeof(Bitnodepost));            Bt->bitree = p; Bt->tag =' L ';//Visited left dial hand tree            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); }if(!Stack. empty ())//Traverse Right sub-tree{BT =Stack. Top ();//access to right sub-treeBt->tag =' R ';            p = bt->bitree;        p = p->rchild; }    }//while}/**************************** function: Hierarchical traversal of binary tree * function parameter: two fork tree root node * idea: To access each node hierarchically, from top to bottom, from left to right, and to use queues in the process of hierarchical traversal. ****************************/voidLevelorder (Bitree root) {Bitree p = root; queue<BiTree> Queue;Queue. push (P);//The root node is enqueued    //Queue not empty loop     while(!Queue. empty ()) {p =Queue. Front ();//Enemy elements out of the team        cout<< P->data <<" ";//Access node of P point        Queue. Pop ();//Exit queue        //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); }    }//while}


Test input: abc# #DE #g# #F # # #

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Binary tree creation and traversal

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.