#include <iostream>#include<stack>#include<stdio.h>#include<stdlib.h>#defineElemtype Charusing namespaceStd;typedefstructbitnode{elemtype data; structBitnode *lchild,*Rchild;} Bitnode,*Bitree;intCreatebitree (Bitree *t) { Charch; CH=GetChar (); if(ch==' ') (*T) =NULL; Else { (*t) = (bitnode*)malloc(sizeof(Bitnode)); if(! (*t))return 0; (*t)->data =ch; Createbitree (& (*t)lchild); Createbitree (& (*t)rchild); } return 1;}voidPreorderdisplay (Bitree t) {cout<<"*************preorderdisplay***********"<<Endl; Stack<BiTree>s; if(!t) {cout<<"Empty Tree"<<Endl; return; } s.push (t); while(!S.empty ()) {Bitree tmp=S.top (); cout<<tmp->data; S.pop (); if(tmp->rchild!=NULL) s.push (TMP-rchild); if(tmp->lchild!=NULL) s.push (TMP-lchild); } }voidInorderdisplay (Bitree t) {cout<<"\n************inorderdisplay************"<<Endl; if(!t) {cout<<"Empty Tree"<<Endl; return; } Stack<BiTree>s; Bitree Curr=T; while(curr!=null| |!S.empty ()) { while(Curr!=null)//put all the left child nodes of the node into the stack{s.push (curr); Curr= curr->Lchild; } //then edge out the stack edge output edge into the right node if(!S.empty ()) {Bitree tmp=S.top (); cout<<tmp->data; S.pop (); Curr= tmp->Rchild; } } }voidPostorderdisplay (Bitree t) {cout<<"\n**************postorderdisplay**************"<<Endl; if(!t)return; Stack<BiTree>s; Bitree Curr=T; Bitree Pre=NULL; while(curr!=null| |!S.empty ()) { while(curr!=NULL)///first left node all into stack {s.push (curr); Curr= curr->Lchild; } Curr=S.top (); if(Curr->rchild = = null| | Curr->rchild = =Pre)//determine if the current node has a right child or the right child of the current node is equal to the last access node {cout<<curr->data; S.pop (); Pre=Curr; Curr=NULL; } else//have right child, need to enter the stackCurr= curr->Rchild; }}intMain () {Bitree T; intCreres = Createbitree (&t); cout<<"*******state of creating Bitree:"<<creRes<<Endl; Preorderdisplay (t); Inorderdisplay (t); Postorderdisplay (t); return 1;}
Binary Tree--non-recursive implementation