Binary Tree--non-recursive implementation

Source: Internet
Author: User

#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

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.