二叉树的创建与各种遍历方式

#include<cstdlib>
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;

typedef struct BiTNode
{
    string data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void createBiTree(BiTree &r)                                //按照前序遍历实现创建树           1 2 # 4 6 # # # 3 # 5 # #
{
    string temp;
    cin>>temp;
    if(temp.size() ==1 && temp[0]=='#')
    {
        r=NULL;
    }
    else
    {
        r = new BiTNode;
        r->data = temp;
        createBiTree(r->lchild);
        createBiTree(r->rchild);        
    }    

}


//递归实现树的三种遍历
void PreOrder(BiTree T)
{
    if(T!=NULL)
    {
        cout<<T->data;
        PreOrder(T->lchild);
        PreOrder(T->rchild);
    }
    else
        return;    
}

void InOrder(BiTree T)
{
    if(T!=NULL)
    {
        InOrder(T->lchild);
        cout<<T->data;
        InOrder(T->rchild);
    }
    else
        return;
}

void PostOrder(BiTree T)
{
    if(T!=NULL)
    {
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        cout<<T->data ;
    }
    else
        return ;    
}

//非递归实现方式树的三种遍历

void PreOrder2(BiTree T)
{
    stack<BiTree> st;                                                                                                
    BiTree p = T;
    while(p || !st.empty())
    {
        if(p!=NULL)
        {
            cout<<p->data;
            st.push(p);
            p=p->lchild ;
        }
        else
        {
            p=st.top();
            st.pop();
            p=p->rchild ;
        }
    }
}

void InOrder2(BiTree T)
{
    stack<BiTree> st;
    BiTree p = T;
    while(p || !st.empty())
    {
        if(p)
        {
            st.push(p);
            p=p->lchild;
        }
        else
        {
            p=st.top();
            st.pop();
            cout<<p->data ;
            p=p->rchild ;            
        }
    }    
}                            

void PostOrder2(BiTree T)                         //较难一点
{
    stack<BiTree> st;
    int Tag[2000];
    memset(Tag,0,sizeof(Tag));
    int topp=-1;
    BiTree p = T;
    while(p || !st.empty())
    {
        while(p!=NULL)
        {
            st.push(p);
            topp++;
            Tag[topp]=0;
            p=p->lchild ;            
        }        
        
        while(!st.empty() && Tag[topp]==1)
        {
            p=st.top();
            st.pop();
            topp--;
            cout<<p->data ;
        }
        if(!st.empty())
        {
            Tag[topp]=1;
            p=st.top();
            p=p->rchild;
        }
        else
            break;        
    }
}


//层次遍历
void  LeverOrder(BiTree T)
{
    queue<BiTree> que;
    BiTree p=T;
    if(T==NULL)
        return;
    que.push(p);
    while(!que.empty())
    {
        p=que.front();
        que.pop();
        cout<<p->data;
        if(p->lchild)
        {            
            que.push(p->lchild );
        }
        if(p->rchild )
        {        
            que.push(p->rchild );
        }        
    }    
}

int main()
{
    BiTree T;
    createBiTree(T);   //1 2 # 4 6 # # # 3 # 5 # #
    
    PreOrder(T);
    cout<<endl;
    
    InOrder(T);
    cout<<endl;
    
    PostOrder(T);
    cout<<endl;    

    PreOrder2(T);
    cout<<endl;
    
    InOrder2(T);
    cout<<endl;
    
    PostOrder2(T);
    cout<<endl;
    
    LeverOrder(T);
    cout<<endl;
    
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值