[Learn Little bit by bit-Data Structure-binary tree] binary tree traversal (full)

Source: Internet
Author: User
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>#include <stack> #include <iostream> #include <malloc.h>#define LEAF -1using namespace std;typedef struct BTreeNode{     BTreeNode* lchild;     BTreeNode* rchild;     int value;               }BTreeNode,*Btree;BTreeNode* createTree(){     BTreeNode* T;     int t;           scanf("%d",&t);     if(t == LEAF){          T = NULL;     }else{          T = (BTreeNode *) malloc(sizeof(BTreeNode));          T->value = t;          T->lchild = createTree();          T->rchild = createTree();         }     return T;}void visit(BTreeNode * root){     printf("%d ",root->value); } void preOrder(BTreeNode* root){     if(root != NULL){         visit(root);     }     if(root->lchild != NULL){         preOrder(root->lchild);                 }     if(root->rchild !=NULL){         preOrder(root->rchild);     }}void inOrder(BTreeNode* root){     if(root->lchild != NULL){         inOrder(root->lchild);                 }     if(root != NULL){         visit(root);     }     if(root->rchild !=NULL){         inOrder(root->rchild);     }}void postOrder(BTreeNode* root){     if(root->lchild != NULL){         postOrder(root->lchild);                 }     if(root->rchild !=NULL){         postOrder(root->rchild);     }     if(root != NULL){         visit(root);      }}void BFSVisit(BTreeNode* root){    if(root == NULL){         return ;             }      queue<BTreeNode*> que;     que.push(root);     while(!que.empty()){          BTreeNode *tmp = que.front();          que.pop();           visit(tmp);           if(tmp->lchild != NULL){              que.push(tmp->lchild);                         }          if(tmp->rchild != NULL){              que.push(tmp->rchild);           }                         } } void non_recur_preOrder(BTreeNode * root){     stack<BTreeNode *> s;     BTreeNode * tmp = root;      while(tmp != NULL|| !(s.empty())){         while(tmp != NULL){             visit(tmp);             s.push(tmp);             tmp = tmp->lchild;                   }                  while(!(s.empty())){             tmp = s.top();             s.pop();             tmp = tmp->rchild;             }               } } void non_recur_inOrder(BTreeNode * root){     stack<BTreeNode *> s;     BTreeNode * tmp = root;      while(tmp != NULL|| !(s.empty())){         while(tmp != NULL){             s.push(tmp);             tmp = tmp->lchild;                   }                  while(!(s.empty())){             tmp = s.top();             visit(tmp);              s.pop();             tmp = tmp->rchild;             }               } } void non_recur_postOrder(BTreeNode *root){     stack<int> tag;     stack<BTreeNode *> s;     BTreeNode* tmp = root;     do{         while(tmp != NULL){             s.push(tmp);             tag.push(0);             tmp = tmp->lchild;                }         while(!s.empty() && tag.top()==1){             tmp = s.top();             s.pop();             tag.pop();             visit(tmp);             }         if(!s.empty()){             tag.pop();             tag.push(1);             tmp = (s.top())->rchild;          }        }while(!(s.empty()));} main(){    BTreeNode * root;    root = createTree();    printf("preOrder :");    preOrder(root);    printf("\n");    printf("preOrder :");    non_recur_preOrder(root);    printf("\n");    printf("inOrder :");    inOrder(root);    printf("\n");    printf("inOrder :");    non_recur_inOrder(root);    printf("\n");    printf("postOrder :");    postOrder(root);    printf("\n");    printf("postOrder :");    non_recur_postOrder(root);    printf("\n");    system("pause");    return 0;       }

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.