二叉树建立、遍历

#include <iostream>

#include <cstdlib>

#include <stack>

#include <queue>

using namespace std;

typedef struct node

{

    char ch;

    struct node *lchild,*rchild;

}BiTNode,*BiTree;

int treenum=0;//总结点数

 

void create(BiTree &T)//先序建立

{

    char c;

    cin>>c;

    if(c=='#')

        T=NULL;//空树

    else

    {

        T=new BiTNode;//等价于 T=(BiTree)malloc(sizeof(BiTNode))

        T->ch=c;

        create(T->lchild);//访问左孩子

        create(T->rchild);//访问右孩子

    }

}

 

void show1(BiTree T)//递归先序

{

    if(T)

    {

        cout<<T->ch<<" ";

        show1(T->lchild);

        show1(T->rchild);

    }

}

 

void show2(BiTree T)//递归中序

{

    if(T)

    {

        show2(T->lchild);

        cout<<T->ch<<" ";

        show2(T->rchild);

    }

}

 

void show3(BiTree T)//递归后序

{

    if(T)

    {

        show3(T->lchild);

        show3(T->rchild);

        cout<<T->ch<<" ";

    }

}

 

int num1(BiTree T)//求结点数

{

    if(T)

    {

        treenum++;//不为空+1

        num1(T->lchild);

        num1(T->rchild);

    }

    return 0;

}

/*int num1(BiTree T)//求叶子结点数
{
    if(T)
    {
        if(!(T->lchild)&&!(T->rchild))
        {
            treenum++;
        }
        num1(T->lchild);
        num1(T->rchild);
    }
    return 0;
}//没有孩子(出度为0)即为叶子结点*/

int num2(BiTree T)//求深度

{

    int l=0,r=0,level=0;

    if(T)

    {

        l=num2(T->lchild);

        r=num2(T->rchild);

        level=max(l,r);

        return level+1;//返回大的深度 +1(根结点)

    }

    return 0;

}

 

void show4(BiTree T)//非递归先序

{

    stack<BiTree>st;

    BiTree p=T;

    while(p||!st.empty())//栈非空或p不空

    {

        if(p)

        {

            st.push(p);//入栈

            cout<<p->ch<<" ";//输出

            p=p->lchild;//指向左孩子

        }

        else

        {

            p=st.top();//p指向栈顶元素

            //cout<<"*"<<p->ch<<"*"<<" ";

            st.pop();//删除栈顶元素

            p=p->rchild;//指向右孩子

        }

    }

}

 

void show5(BiTree &T)//非递归中序

{

    stack<BiTree>st;

    BiTree p=T;

    while(p||!st.empty())

    {

        if(p)

        {

            st.push(p);

            p=p->lchild;

        }

        else

        {

            p=st.top();

            cout<<p->ch<<" ";

            st.pop();

            p=p->rchild;

        }

    }

}

 

void show6(BiTree &T)//非递归后序

{

    stack<BiTree>st;

    int flag[100];//设置一个标记

    BiTree p=T;

    while(p)//p不为空

    {

        st.push(p);//入栈

        flag[st.size()]=0;//初始标记设为0

        p=p->lchild;//指向左孩子

    }

    while(!st.empty())//栈不为空

    {

        p=st.top();//p指向栈顶元素

        while(p->rchild&&flag[st.size()]==0)//p有右孩子且标记为0

        {

            p=p->rchild;

            flag[st.size()]=1;//标记为1

            while(p)//p不为空

            {

                st.push(p);//入栈

                flag[st.size()]=0;//标记清零

                p=p->lchild;

            }

            p=st.top();//p指向栈顶元素

        }

        cout<<p->ch<<" ";//没有右孩子 直接输出

        st.pop();//清除栈顶元素

    }

}

 

void show7(BiTree &T)//层次遍历

{

    queue<BiTree>qu;

    BiTree p=T;

    qu.push(p);//入队列

    while(!qu.empty())

    {

        p=qu.front();//p指向队头元素

        cout<<p->ch<<" ";

        qu.pop();//出队列

        if(p->lchild)

        {

            qu.push(p->lchild);

        }

        if(p->rchild)

        {

            qu.push(p->rchild);

        }

    }

}

 

int main()

{

    //输入:ABDG##H###CE#I##F##

    //输入:- + a # # * b # # - c # # d # # / e # # f # #

    BiTree T;//等价于 BiTNode *T

    cout<<"请按先序序列输入包含空结点的二叉树(空结点用‘#’表示):"<<endl;

    create(T);

    cout<<"二叉树创建完成!"<<endl;

    cout<<"先序遍历输出二叉树(递归):"<<endl;

    show1(T);

    cout<<endl;

    cout<<"中序遍历输出二叉树(递归):"<<endl;

    show2(T);

    cout<<endl;

    cout<<"后序遍历输出二叉树(递归):"<<endl;

    show3(T);

    cout<<endl;

    cout<<"树的结点数为:";

    num1(T);

    cout<<treenum<<endl;

    cout<<"树的深度为:";

    cout<<num2(T)<<endl;

    cout<<"先序遍历输出二叉树(非递归):"<<endl;

    show4(T);

    cout<<endl;

    cout<<"中序遍历输出二叉树(非递归):"<<endl;

    show5(T);

    cout<<endl;

    cout<<"后序遍历输出二叉树(非递归):"<<endl;

    show6(T);

    cout<<endl;

    cout<<"层次遍历输出二叉树:"<<endl;

    show7(T);

    cout<<endl;

    return 0;

}

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值