<span style= "FONT-SIZE:14PX;" >/* two cross-tree traversal */#include <iostream> #include <cstring> #include <stack>using namespace Std;typedef struct node{char data; struct node *lchild,*rchild;} bintree;typedef struct node1{bintree *btnode; BOOL IsFirst;} Btnode;void creatBinTree1 (bintree* &root)//Pre-sequence traversal create tree, shaped like a,b,c,#,#,d,#,#,e,#,#{char value; cin>>value; if (value = = ' # ') root = NULL; Recursive end condition Else {root = (Bintree *) malloc (sizeof (bintree)); Root->data = value; CreatBinTree1 (Root->lchild); CreatBinTree1 (Root->rchild); }}void creatBinTree2 (char *s,bintree *&root)//Create a two-fork tree, s as a string {int I, shaped like a (b,c)); BOOL Isright=false; stack<bintree*> S1; Storage node stack<char> s2; Store delimiter Bintree *p,*temp; root->data=s[0]; root->lchild=null; root->rchild=null; S1.push (root); I=1; while (I<strlen (s)) {if (s[i]== ' (') { S2.push (S[i]); Isright=false; } else if (s[i]== ', ') {isright=true; } else if (s[i]== ') ') {S1.pop (); S2.pop (); } else if (Isalpha (s[i))) {p= (Bintree *) malloc (sizeof (bintree)); p->data=s[i]; p->lchild=null; p->rchild=null; Temp=s1.top (); if (isright==true) {temp->rchild=p; cout<<temp->data<< "The right child is" <<s[i]<<endl; } else {temp->lchild=p; cout<<temp->data<< "The left child is" <<s[i]<<endl; } if (s[i+1]== ' (') S1.push (p); } i++; }}void display (Bintree *root)//show tree structure {if (root!=null) {cout<<root->data; if (root->lchild!=null) {cout<< ' ('; DiSplay (Root->lchild); } if (root->rchild!=null) {cout<< ', '; Display (root->rchild); cout<< ') '; }}}void PreOrder1 (Bintree *root)//recursive pre-order traversal {if (root!=null) {cout<<root->data<< ""; PreOrder1 (Root->lchild); PreOrder1 (Root->rchild); }}void inOrder1 (Bintree *root)//recursive middle order traversal {if (root!=null) {inOrder1 (root->lchild); cout<<root->data<< ""; InOrder1 (Root->rchild); }}void postOrder1 (Bintree *root)//recursive post-traversal {if (root!=null) {postOrder1 (root->lchild); PostOrder1 (Root->rchild); cout<<root->data<< ""; }}void PreOrder2 (Bintree *root)//non-recursive pre-order traversal {stack<bintree*> S; Bintree *p=root; while (p!=null| |! S.empty ()) {while (p!=null) {cout<<p->data<< ""; S.push (P); p=p->lchild; } if (!s.empty ()) {p=s.top (); S.pop (); p=p->rchild; }}}void InOrder2 (Bintree *root)//non-recursive middle order traversal {stack<bintree*> S; Bintree *p=root; while (p!=null| |! S.empty ()) {while (p!=null) {s.push (P); p=p->lchild; } if (!s.empty ()) {p=s.top (); cout<<p->data<< ""; S.pop (); p=p->rchild; }}}void PostOrder2 (Bintree *root)//Non-recursive post-traversal {stack<btnode*> s; Bintree *p=root; Btnode *temp; while (p!=null| |! S.empty ()) {while (p!=null)//Search down along Zuozi until a node with no left subtree is present {Btnode *btn= (Btnode *) ma Lloc (sizeof (Btnode)); btn->btnode=p; btn->isfirst=true; S.push (BTN); p=p->lchild; } if (!s.empty ()) {temp=s.top (); S.pop (); if (temp->isfirst==tRue)//Represents the first time it appears at the top of the stack {temp->isfirst=false; S.push (temp); p=temp->btnode->rchild; } else//second appears at the top of the stack {cout<<temp->btnode->data<& lt; " "; P=null; }}}}void PostOrder3 (Bintree *root)//Non-recursive post-traversal {stack<bintree*> s; Bintree *cur; The current node Bintree *pre=null; Node S.push (root) of the previous visit; while (!s.empty ()) {cur=s.top (); if ((cur->lchild==null&&cur->rchild==null) | | | (pre!=null&& (pre==cur->lchild| | Pre==cur->rchild)) {cout<<cur->data<< ""; If the current node has no child nodes, or if the child has been visited S.pop (); Pre=cur; } else {if (cur->rchild!=null) S.push (cur->rchild); if (cur->lchild!=null) S.pusH (cur->lchild); }}}void postOrder4 (Bintree *root)//Non-recursive post-traversal {stack<bintree *> stack_tree; Bintree *t = root; Bintree *t_1 = null;while (T | |!stack_tree.empty ()) {while (t) {Stack_tree.push (t); t = T->lchild;} if (!stack_tree.empty ()) {t = Stack_tree.top (); if (t_1! = T && t->rchild) {//t_1 = = T, describes the element in the second access stack t_1 = T;t = T->rchild;} Else {cout<< "val =" <<stack_tree.top ()->data<<endl;stack_tree.pop (); t = NULL;}}} int main (int argc, char *argv[]) {char s[100]; while (scanf ("%s", s) ==1) {Bintree *root= (Bintree *) malloc (sizeof (bintree)); CreatBinTree2 (S,root); display (root); cout<<endl; PreOrder2 (root); cout<<endl; InOrder2 (root); cout<<endl; PostOrder2 (root); cout<<endl; PostOrder3 (root); cout<<endl; } return 0;} </span>
Several traversal algorithms of binary tree