A non-recursive algorithm for sequential traversal of binary tree (C language)

Source: Internet
Author: User

First of all, thank you for the ' Hicjiajia ' blog post: two fork tree sequential traversal (non-recursive)

This essay opens up my blogging process and becomes a member of thousands of programmers, and insists on going farther!

Tortured me for the afternoon of the post-order traverse Noon to be resolved, the key is to mark the right subtree is visited, consider modifying the binary tree node data structure, add a visit domain, or build a stack to store the visited node. are more troublesome without debugging success. If the right subtree is also in the stack, if there is no access to the tag, it will change the order of access, or even a dead loop, this is a more dangerous situation. From the reference blog, excerpt and rewrite the code as C, basically no changes. Follow the questions and try to write your own original code.

Binary tree stores the data type int, with the number 0 indicating that the subtree is empty

Input: 1 2 3 0 8 0 0 4 0 0 5 6 0 0 7 0 0

Get post-post traversal results: 83426751

1#include <stdio.h>2#include <stdlib.h>3 4 #defineOK 15 #defineERROR 06 #defineMaxSize 1007 8typedefintElemtype;9typedefintStatus;Ten  Onetypedefstructbtnode{ A elemtype data; -     structBtnode *lchild,*Rchild; - }btree; the  -typedefstructst{ -     structbtnode*Data[maxsize]; -     inttop; + }stack;  - //1. Generate two fork trees in order of precedence +btree*Createt () { ABTree *BT; at Elemtype ch; -scanf"%d",&ch); -     if(ch==0) -bt=NULL; -     Else{ -bt= (btree*) malloc (sizeof(BTree)); in         if(!BT)        {exit (OVERFLOW);}  -Bt->data=ch; toBt->lchild=createt (); +Bt->rchild=createt (); -     } the     returnBT; * }  $ Panax Notoginseng //4. Post-Traversal -Status Postorder (BTree *BT) { the     if(BT) { +Postorder (bt->lchild); APostorder (bt->rchild); theprintf"%3d",bt->data); +         returnOK; -     } $     Else returnERROR; $ } - //4. Post-traversal-non-recursive -Status PostOrder2 (BTree *BT) { the Stack s,s2; -BTree *T;Wuyi     intFlag[maxsize]; thes.top=0; -t=BT; Wu      while(s.top!=0||T) { -          while(T) About         { $s.data[s.top++]=T; -flag[s.top-1]=0; -T=t->Lchild; -          }  A           while(s.top!=0&&flag[s.top-1]==1){ +t=s.data[--S.top]; theprintf"%3d",t->data); -          } $          if(s.top!=0){ theflag[s.top-1]=1; thet=s.data[s.top-1]; theT=t->Rchild; the          } -          Else    Break; in     } the     returnOK; the } About intMain () { theBTree *BT; thebt=createt (); the     if(Postorder (BT)) { +printf"\ nthe post-traversal is complete! \ n"); -     }     the     if(PostOrder2 (BT)) {Bayiprintf"\ n Non-recursive post-traversal completion! \ n"); the     } the}

A non-recursive algorithm for sequential traversal of binary tree (C language)

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.