二叉树的各类遍历

来源:互联网 发布:淘宝提高信誉度 编辑:程序博客网 时间:2024/04/29 17:59
#include<iostream>#include<cstdio>#include<cstring>#include<stack>#define maxn 1111using 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;}

1 0
原创粉丝点击