二叉树非递归遍历

1、先序node

    1.方法一ios

 void preorder()
{
    stack<Btnode*> s;
    Btnode *p;
    s.push(b);
    while(!s.empty())
    {
        p=s.top();
        cout<<p->data;
        s.pop();
        if(p->rchild!=NULL)
             s.push(p->rchild);
        if(p->lchild!=NULL)
             s.push(p->lchild); 
    }
    cout<<endl;
}

    2.方法二ide

        void pre_order()
        {
            stack<Btnode*> s;
            Btnode *p=b;
            while(!s.empty()||p!=NULL)
            {
                while(p!=NULL)
                {
                    s.push(p);
                    cout<<p->data;
                    p=p->lchild;
                }
                if(!s.empty())
                {
                    p=s.top();
                    s.pop();
                    p=p->rchild;
                }
        }
            cout<<endl;
    }

2、中序post

void in_order()
{
        stack<Btnode*> s;
        Btnode *p;
        p=b;
        while(p!=NULL||!s.empty())
        {
            while(p!=NULL)
            {
                s.push(p);
                p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            s.pop();
            cout<<p->data;
            p=p->rchild;
        }
    }
    cout<<endl;
}

3、后序spa

 

 void post_order()
        {
            stack<Btnode*> s;
            Btnode *p=b,*r;
            do
            {
                while(p!=NULL)
                {
                    s.push(p);
                    p=p->lchild;
                }
                r=NULL;
                int flag=true;
                while(!s.empty()&&flag)
                {
                    p=s.top();
                    if(p->rchild==r)
                    {
                        cout<<p->data;
                        s.pop();
                        r=p;
                    }
                    else
                    {
                        p=p->rchild;
                        flag=false;
                    }
                }
            }while(!s.empty());
            cout<<endl;
         }

4、范例code

#include<iostream>
#include<string>
#include<stack>
using namespace std;
const int max_size=100;
struct Btnode
{
    char data;
    Btnode *lchild;
    Btnode *rchild;
};
class Btree
{ 
    Btnode *b;
    public:
        Btree():b(NULL) {}
        ~Btree()
        {
            destroy(b);
        }
        void destroy(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                destroy(b1->lchild);
                destroy(b1->rchild);
                delete b1;
            }
        }
         void make_Btree()
        {
            Btnode *st[max_size],*p;
            string str;
            int k,j=0,top=-1;
            b=NULL;
            cout<<"请输入括号表示的二叉树:";
            cin>>str;
            for(int i=0;i<str.size();i++)
            {
                switch(str[i])
                {
                    case '(':st[++top]=p;k=1;break;
                    case ')':top--;break;
                    case ',':k=2;break;
                    default :
                             p=new Btnode;
                             p->data=str[i];
                             p->lchild=p->rchild=NULL;
                             if(b==NULL)
                                 b=p;
                             else
                             {
                                 switch(k)
                                 {
                                     case 1:st[top]->lchild=p;break;
                                     case 2:st[top]->rchild=p;break;
                                 }
                             }             
                }
            }
        }
        void post_order()
        {
            stack<Btnode*> s;
            Btnode *p=b,*r;
            do
            {
                while(p!=NULL)
                {
                    s.push(p);
                    p=p->lchild;
                }
                r=NULL;
                int flag=true;
                while(!s.empty()&&flag)
                {
                    p=s.top();
                    if(p->rchild==r)
                    {
                        cout<<p->data;
                        s.pop();
                        r=p;
                    }
                    else
                    {
                        p=p->rchild;
                        flag=false;
                    }
                }
            }while(!s.empty());
            cout<<endl;
         } 
};
int main()
{
    Btree t;
    t.make_Btree();
    t.post_order();
    return 0;
 } 
View Code