二叉樹的各類遍歷

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#define maxn 1111
using namespace std;
struct BinTree
{
    char data;
    BinTree *lchild,*rchild;
    int nc;
    BinTree(){nc = 0;}
};
BinTree* build(BinTree *t)
{
    char c;
    cin>>c;
    if(c=='#') return NULL;
    t=new BinTree;
    t->data=c;
    t->lchild=build(t->lchild);
    t->rchild=build(t->rchild);
    return t;
}
void preorder(BinTree* t)//前序遍歷遞歸
{
    if(t==NULL) return ;
    cout<<t->data;
    preorder(t->lchild);
    preorder(t->rchild);
}
void inorder(BinTree *t)//中序遍歷遞歸
{
    if(t==NULL) return ;
    inorder(t->lchild);
    cout<<t->data;
    inorder(t->rchild);
}
void postorder(BinTree *t)//後序遍歷遞歸
{
    if(t==NULL) return ;
    postorder(t->lchild);
    postorder(t->rchild);
    cout<<t->data;
}
void preorder_one(BinTree* t)//前序遍歷非遞歸
{
    stack<BinTree*>st;
    while(t||!st.empty())
    {
        if(t)
        {
            cout<<t->data;//訪問根節點
            st.push(t);
            t=t->lchild;//訪問左子樹
        }
        else
        {
            t=st.top();
            t=t->rchild;//訪問右子樹
            st.pop();
        }
    }
}
void inorder_one(BinTree *t)
{
    stack<BinTree*>st;
    while(t||!st.empty())
    {
        if(t)
        {
            st.push(t);
            t=t->lchild;//訪問左子樹
        }
        else
        {
            t=st.top();
            st.pop();
            cout<<t->data;//訪問根
            t=t->rchild;//訪問右子樹
        }
    }
}
void postorder_one(BinTree* t)//後序遍歷非遞歸
{
    stack<BinTree *>st;
    while(t||!st.empty())
    {
        if(t&&t->nc==0)
        {
            st.push(t);
            t->nc++;
            t=t->lchild;//訪問左子樹
        }
        else
        {
            if(st.empty()) break;//因爲最後一個節點訪問的是根,t始終不爲空,但此時棧已經空,此時遍歷完
            t=st.top();
            if(t->nc==1)//訪問右子樹
            {
                t->nc++;
                t=t->rchild;
            }
            else
            {
                st.pop();
                cout<<t->data;//訪問根節點
            }
        }

    }
}
void preorder_two(BinTree *t)//前序遍歷非遞歸二
{
    stack<BinTree*>st;
    st.push(t);
    while(!st.empty())
    {
        t=st.top();
        if(t==NULL) st.pop();
        else if(t->nc==0){cout<<t->data,st.push(t->lchild),t->nc=1;}
             else if(t->nc==1){st.pop(),st.push(t->rchild);}
    }
}
void inorder_two(BinTree *t)//中序遍歷非遞歸二
{
    stack<BinTree*>st;
    st.push(t);
    while(!st.empty())
    {
        t=st.top();
        if(t==NULL) st.pop();
        else if(t->nc==0){st.push(t->lchild),t->nc=1;}
             else if(t->nc==1){cout<<t->data,st.pop(),st.push(t->rchild);}
    }
}
void postorder_two(BinTree *t)//後序遍歷非遞歸二
{
    stack<BinTree*>st;
    st.push(t);
    while(!st.empty())
    {
        t=st.top();
        if(t==NULL) st.pop();
        else if(t->nc==0){st.push(t->lchild),t->nc=1;}
             else if(t->nc==1) {st.push(t->rchild),t->nc=2;}
                  else if(t->nc==2) {cout<<t->data,st.pop();}
    }
}
int main()
{
    BinTree *t=NULL;
    t=build(t);
    //preorder_two(t);
    //inorder_two(t);
    postorder_two(t);
    /*preorder(t);
    cout<<endl;
    preorder_one(t);
    cout<<endl;
    postorder(t);
    cout<<endl;
    postorder_one(t);
    cout<<endl;
    inorder(t);
    cout<<endl;
    inorder_one(t);
    cout<<endl;*/
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章