实现二叉树操作的主函数

来源:互联网 发布:我淘宝店铺怎么激活 编辑:程序博客网 时间:2024/06/05 17:56
/*BiTree.cpp*/

#include "BiTree_Stack.h"
#include "BiTree_Op.h"
#include "extend.h"
#include "BiTree_Queue.h"
#include "BiTree_TaskStack.h"
#include "BiTree.h"

//功能菜单
void Mnue_Create()
{
    cout<<"--------------------------"<<endl;
    cout<<"1.以字符串形式创建二叉树"<<endl;
    cout<<"2.以原表达式形式创建二叉树"<<endl;
    cout<<"3.以前缀表达式创建二叉树"<<endl;
    cout<<"4.以后缀表达式创建二叉树"<<endl;
    cout<<"5.以先中序列创建二叉树"<<endl;
    cout<<"6.以后中序列创建二叉树"<<endl;
    cout<<"--------------------------"<<endl;
    cout<<"请选择操作(1-6):"<<endl;
    cout<<">";
}//Mnue_Create

void Mnue_Order()
{
    cout<<"--------------------------"<<endl;
    cout<<"0.递归之先序遍历二叉树"<<endl;
    cout<<"1.递归之中序遍历二叉树"<<endl;
    cout<<"2.递归之后序遍历二叉树"<<endl;
    cout<<"3.任务书之先序遍历二叉树"<<endl;
    cout<<"4.任务书之中序遍历二叉树"<<endl;
    cout<<"5.任务书之后序遍历二叉树"<<endl;
    cout<<"6.路径之先序遍历二叉树"<<endl;
    cout<<"7.路径之中序遍历二叉树"<<endl;
    cout<<"8.路径之后序遍历二叉树"<<endl;
    cout<<"9.层次遍历二叉树"<<endl;
    cout<<"--------------------------"<<endl;
    cout<<"请选择操作(0-9):"<<endl;
    cout<<">";
}

//显示功能菜单
void Mnue_view()
{
    cout<<"--------------------------"<<endl;
    cout<<"1.凹入图显示"<<endl;
    cout<<"2.广义表显示"<<endl;
    cout<<"--------------------------"<<endl;
    cout<<"请选择操作(1-2):"<<endl;
    cout<<">";
}

//主功能菜单
void MnuePrint()
{
    cout<<"**********************"<<endl;
    cout<<"1.创建二叉树"<<endl;
    cout<<"2.遍历二叉树"<<endl;
    cout<<"3.二叉树的深度"<<endl;
    cout<<"4.二叉树的总结点个数"<<endl;
    cout<<"5.二叉树的叶子结点总数"<<endl;
    cout<<"6.销毁二叉树"<<endl;
    cout<<"7.退出系统"<<endl;
    cout<<"8.显示二叉树"<<endl;
    cout<<"**********************"<<endl;
    cout<<"请选择操作(1-8):"<<endl;
    cout<<">";
}//MnuePrint

//清屏函数
void Clear()
{
    char a;
    cout<<endl<<"请按 回车键 继续……"<<endl;
    a=getchar();
    system("cls");
}//清屏函数

void main(){
    int i=0;
    BiTree T=NULL;
    char exp[MAXSIZE],preod[MAXSIZE],inod[MAXSIZE];
    char ch,choice;
    while(1)
    {
        MnuePrint();
        cin>>choice;
        switch(choice)
        {
        case '1':
            Clear();
            Mnue_Create();
            cin>>ch;
            switch(ch)
            {
            case '1'://以字符串形式创建二叉树
                cout<<"请输入字符串:"<<endl;
                if(CreateBiTree(T))
                        cout<<"创建成功"<<endl;
                else cout<<"创建失败"<<endl;
                break;

            case '2'://以原表达式形式创建二叉树
                cout<<"请输入原表达式:"<<endl;
                for(i=0;(exp[i]=getchar())!='/n';i++);
                exp[i]='#';
                if(CrtExptree(T,exp))
                    cout<<"创建成功"<<endl;
                else cout<<"创建失败"<<endl;
                break;
            case '3':
                cout<<"请输入前缀表达式:"<<endl;
                CreateTree(T);
                    cout<<"创建成功"<<endl;
                break;
            case '4':
                cout<<"请输入后缀表达式:"<<endl;
                for(i=0;(exp[i]=getchar())!='/n';i++);
                exp[i]='#';
                if(CreateTree(T,exp))
                    cout<<"创建成功"<<endl;
                else cout<<"创建失败"<<endl;
                break;
            case '5':
                cout<<"请输入前缀表达式:"<<endl;
                for(i=0;(preod[i]=getchar())!='/n';i++);
                cout<<"请输入后缀表达式:"<<endl;
                cout<<">";
                for(i=0;(inod[i]=getchar())!='/n';i++);
                PreInOd(preod,inod,1,i,1,i,T);
                break;
            case '6':
                break;
            default:cout<<"您的输入有误!"<<endl;
            }
            Clear();
            break;
        case '2':
            Clear();
            Mnue_Order();
            cin>>ch;
            switch(ch)
            {
            case '0'://递归之先序遍历二叉树
                cout<<"先序遍历二叉树的序列为:";
                Preorder(T);
                break;
            case '1'://递归之中序遍历二叉树
                cout<<"中序遍历二叉树的序列为:";
                Inorder(T);
                break;
            case '2'://递归之后序遍历二叉树
                cout<<"后序遍历二叉树的序列为:";
                Priorder(T);
                break;
            case '3'://任务书之先序遍历二叉树
                cout<<"先序遍历二叉树的序列为:";
                Preorder_iter(T);
                break;
            case '4'://任务书之中序遍历二叉树
                cout<<"中序遍历二叉树的序列为:";
                InOrder_iter(T);
                break;
            case '5'://任务书之后序遍历二叉树
                cout<<"后序遍历二叉树的序列为:";
                PriOrder_iter(T);
                break;
            case '6':
                cout<<"先序遍历二叉树的序列为:";
                PreOrder_I(T);
                break;
            case '7':
                cout<<"中序遍历二叉树的序列为:";
                Inorder_I(T);
                break;
            case '8':
                cout<<"后序遍历二叉树的序列为:";
                PriOrder_I(T);
                break;
            case '9'://层次遍历二叉树
                cout<<"层次遍历二叉树的序列为:";
                layer(T);
                break;
            default:cout<<"您的输入有误!"<<endl;
            }
            Clear();
            break;
        case '3'://二叉树的深度
            //cout<<"二叉树的深度为:"<<Depth(T,1,0);
            cout<<"二叉树的深度为:"<<Depth_BiTree(T);
            Clear();
            break;
        case '4'://二叉树的总结点个数
            cout<<"二叉树的总结点个数:"<<NumOfNode(T);
            Clear();
            break;
        case '5'://二叉树的叶子结点总数
            cout<<"二叉树的叶子结点总数:"<<NumOfLeaf(T);
            Clear();
            break;
        case '6'://销毁二叉树

            Clear();
            break;
        case '7':
            exit(0);
            //Clear();
            break;
        case '8':
            Clear();
            Mnue_view();
            cin>>ch;
            switch(ch)
            {
            case '1':
                cout<<"凹入图显示的二叉树为:"<<endl;
                PrintTree(T,0);
                break;
            case '2':
                cout<<"广义表显示的二叉树为:"<<endl;
                cout<<"(";
                PrintBiTree(T);
                cout<<")";
                break;
            default:cout<<"您的输入有误!"<<endl;
            }
            Clear();
            break;
        default:
            cout<<"您的输入有误!"<<endl;
        }
    }
}//main
原创粉丝点击