Data structure-two fork tree of various traversal (first in the back of the sequence!!) )

Source: Internet
Author: User


Recently in the writing data structure of the binary tree traversal, here to summarize:


First Order recursive traversal:

void Pretravel (Bitree t) {///pre-order recursive traversal if (t) {  printf ("%c", t->data); Pretravel (T->lchild); Pretravel (T->rchild);}}


Middle order recursive Traversal:

void Midtravel (Bitree t) {//middle order recursive traversal if (t) {  midtravel (t->lchild);p rintf ("%c", t->data); Midtravel (T->rchild);}}


Post-sequential recursive traversal:

void Posttravel (Bitree t) {//post-post recursion traversal if (t) {  posttravel (t->lchild); Posttravel (T->rchild);p rintf ("%c", T->data);}}


First order non-recursive traversal:



In-sequence non-recursive traversal:



Post-post non-recursive traversal:

void Postorder (Bitree &t) {//post-sequential non-recursive traversal (adding a flag variable visit, judging whether the left and right subtree is in the stack) stack<bitree> s; Bitree p = t;p->visit = false;if (t) s.push (T), while (!s.empty ()) {p = s.top (); if (p->visit) {printf ("%c", P->data) ; S.pop ();} else {if (p->rchild) {p->rchild->visit = False;s.push (p->rchild);} if (p->lchild) {p->lchild->visit = False;s.push (p->lchild);} P->visit = True;}}}




Sequence Traversal:

void Levelorder (Bitree &t) {//Sequence traversal queue<bitree> Q;q.push (T); Bitree P;while (!q.empty ()) {p = Q.front ();p rintf ("%c", P->data); Q.pop (); if (p->lchild) Q.push (p->lchild); if ( P->rchild) Q.push (P->rchild);}}


Main function Call:


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cstdlib> #include <stack> #include <queue> #include "constant.h" using namespace std;typedef struct     Bitnode{char Data;bool visit; struct Bitnode *lchild,*rchild;} Bitnode,*bitree;void Print (char a) {printf ("%c", a); return;} void Creatbitree (Bitree &t) {//Pre-order method creates a two fork tree char ch;if ((Ch=getchar ()) = = ' # ') t=null;else{t= (bitnode*) malloc (sizeof ( Bitnode)); if (! T) exit (1); t->data=ch; Creatbitree (T->lchild); Creatbitree (T->rchild);}} void Pretravel (Bitree t) {///pre-order recursive traversal if (t) {printf ("%c", t->data); Pretravel (T->lchild); Pretravel (T->rchild);}} void Midtravel (Bitree t) {//middle order recursive traversal if (t) {midtravel (t->lchild);p rintf ("%c", t->data); Midtravel (T->rchild);}} void Posttravel (Bitree t) {//post-post recursion traversal if (t) {posttravel (t->lchild); Posttravel (T->rchild);p rintf ("%c", T->data);}} void preorder (Bitree &t) {///first-order non-recursive traversal stack<bitree> s; Bitree p = t;while (P | |!s.empty ()) {if (p){printf ("%c", P->data), S.push (p);p = P->lchild;} Else{p = S.top (); S.pop ();p = P->rchild;}}} void Inorder (Bitree T) {//middle order non-recursive traversal stack<bitree> s; Bitree p = t;while (P | |!s.empty ()) {if (p) {S.push (p);p = P->lchild;} else {p = s.top ();p rintf ("%c", P->data), s.pop ();p = P->rchild;}}} void Postorder (Bitree &t) {//post-sequential non-recursive traversal (adding a flag variable visit, judging whether the left and right subtree is in the stack) stack<bitree> s; Bitree p = t;p->visit = false;if (t) s.push (T), while (!s.empty ()) {p = s.top (); if (p->visit) {printf ("%c", P->data) ; S.pop ();} else {if (p->rchild) {p->rchild->visit = False;s.push (p->rchild);} if (p->lchild) {p->lchild->visit = False;s.push (p->lchild);} P->visit = True;}}} void PostOrder1 (Bitree &t) {//post-sequential non-recursive traversal (destruction of the structure of the tree, not fetching) stack<bitree> s; Bitree Q, p = t;while (P | |!s.empty ()) {if (p) {S.push (p); q = p;p = Q->lchild;q->lchild = NULL;} else {q = s.top ();p = Q->rchild;q->rchild = Null;if (p) {S.push (p); q = p;p = Q->lchild;q->lchild = NULL;} else {p = s.top ();p rintf ("%C ", P->data); S.pop (); if (!s.empty ()) {p = s.top ();p = P->lchild;} else return;}}} void Levelorder (Bitree &t) {//Sequence traversal queue<bitree> Q;q.push (T); Bitree P;while (!q.empty ()) {p = Q.front ();p rintf ("%c", P->data); Q.pop (); if (p->lchild) Q.push (p->lchild); if ( P->rchild) Q.push (P->rchild);}}    int main () {Bitree t;printf ("Please input the bitree:\n"); Creatbitree (T);p rintf ("The Pretravel is:\n"); Pretravel (T);p rintf ("\ n"); Preorder (T);p rintf ("\ n");p rintf ("The Midtravel is:\n"); Midtravel (t);p rintf ("\ n"), Inorder (t);p rintf ("\ n");p rintf ("The Posttravel is:\n"); Posttravel (T);p rintf ("\ n"); Postorder (T);p rintf ("\ n");p rintf ("The Leveltravel is:\n"); Levelorder (T);p rintf ("\ n"); PostOrder1 (T);p rintf ("\ n"); return 0;}


Data structure-two fork tree of various traversal (first in the back of the sequence!!) )

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.