二叉树的各类遍历

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值