顺序表的查找和二叉排序树的查找-数据结构类C语言

目录

一、功能函数(func.cpp)

二、主调函数(main.cpp)

三、头文件(before.h)

四、运行截图及功能函数部分展示

        1、顺序表

       (1)顺序表的创建

       (2)顺序查找法

       (3)冒泡排序

       (4)折半查找法

        2、二叉排序树

        (1)二叉排序树的建立

        (2)二叉排序树的插入

        (3)二叉排序树的查找

五、运行环境


一、功能函数(func.cpp)

#include <iostream>
#include "before.h"

using namespace std;

//*************************************//
//顺序表相关函数定义
status CreateSST(SSTable &ST,int numb)
{
    ST.R = (ElemType *)malloc(sizeof(ElemType)*(numb+1));
    if(!ST.R) return ERROR;                     //空间分配失败
    ST.length = numb;
    cout << "---请输入" << numb << "个元素:" << endl;
    for(int i = 1;i < numb+1;i ++) cin >> ST.R[i].key;
    fflush(stdin);                        //清除缓存
    return OK;                                 //创建成功
}//创建顺序表

status Search_Seq(SSTable ST,KeyType key,int &cnt)
{
    ST.R[0].key = key;                             //监视哨
    int i;
    for(i = ST.length;i > 0;i --)                  //末尾开始查找
    {
        if(ST.R[i].key != key)
        {
            cnt ++;
            cout << "经过的元素:" << ST.R[i].key << endl;
        }
        if(ST.R[i].key == key) break;
    }
    cnt ++;
    return i;                                    //返回-1则不存在于顺序表中
}//顺序查找

status Search_Bin(SSTable ST,KeyType key,int &cnt)
{
    int low = 1;
    int high = ST.length;
    int mid;
    if(high < 1) return OVERFLOW;                    //表空
    while(low <= high)
    {
        mid = (low+high)/2;
        cout << "经过元素:" << ST.R[mid].key << endl;
        cnt ++;
        if(key == ST.R[mid].key) return mid;        //找到待查元素
        else if(key < ST.R[mid].key) high = mid-1;  //在前一子表进行查找
        else low = mid+1;                           //在后一子表进行查找
    }
    return ERROR;                                   //key不存在于顺序表
}//二分查找

status Bubble(SSTable &ST)
{
    int length = ST.length;
    if(length < 1) return OVERFLOW;                 //表为空
    for(int i = length-1;i > 0;i --)                //循环次数
    {
        for(int j = 1;j < i+1;j ++)
        {
            if(ST.R[j].key > ST.R[j+1].key)         //后一个值比前一个值小就交换
            {
                KeyType temp = ST.R[j].key;
                ST.R[j].key = ST.R[j+1].key;
                ST.R[j+1].key = temp;
            }
        }
    }
    return OK;
}//冒泡排序

status QuickSort(SSTable &ST,int left,int right)
{
    if(left >= right) return OK;
    int begin = left;
    int end = right;
    int keyi = begin;
    while(left < end)
    {
        while(begin < end && ST.R[end].key >= ST.R[keyi].key) end--;
        while(begin < end && ST.R[begin].key <= ST.R[keyi].key) begin++;

        KeyType temp = ST.R[begin].key;
        ST.R[begin].key = ST.R[end].key;
        ST.R[end].key = temp;
    }
    KeyType temp = ST.R[begin].key;
    ST.R[begin].key = ST.R[keyi].key;
    ST.R[keyi].key = temp;

    QuickSort(ST,left,begin-1);
    QuickSort(ST,begin+1,right);
}//快速排序

void TraverseSST(SSTable ST)
{
    cout << "当前顺序表为:" << endl;
    for(int i = 1;i < ST.length+1;i ++)
    {
        if(i%7 == 0) cout << endl;
        cout << ST.R[i].key << "  ";
    }
    cout << endl;
}//遍历顺序表
//*************************************//

//*************************************//
//二叉排序树的函数定义
void CreateBST(BSTree &T)
{
    T = NULL;          //初始化为空树
    ElemType e;
    int numb[1000],i;
    cout << "---请输入一组二叉排序树的整型元素(以-1作为结束标志):" << endl;
    for(i = 0;numb[i] != -1;i ++)
    {
        cin >> numb[i];
        if(numb[i] == -1) break;
        e.key = numb[i];
        InsertBST(T,e);
    }
}//二叉排序树的创建

void InsertBST(BSTree &T,ElemType e)
{
    if(!T)
    {
        BSTNode *s = (BSTNode *)malloc(sizeof(BSTNode));
        s->data.key = e.key;
        s->lchild = s->rchild = NULL;
        T = s;
    }
    else if(e.key < T->data.key) InsertBST(T->lchild,e);    //插入左子树
    else if(e.key > T->data.key) InsertBST(T->rchild,e);    //插入右子树
}//二叉排序树的插入

BSTree SearchBST(BSTree T,KeyType key,int &cnt)
{
    cnt ++;
    if((T == NULL) || (T->lchild==NULL&&T->rchild==NULL&&T->data.key!=key) || key==T->data.key)
    {
        return T;//查找结束
    }
    else if(key < T->data.key)
    {
        cout << "经过元素:" << T->data.key << endl;
        return SearchBST(T->lchild,key,cnt); //在左子树中继续查找
    }
    else
    {
        cout << "经过元素:" << T->data.key << endl;
        return SearchBST(T->rchild,key,cnt); //在右子树中继续查找
    }
}//二叉排序树的递归查找

status TraverseBST(BSTree T)
{
    if(T == NULL) return OK;
    else
    {
        TraverseBST(T->lchild);
        cout << T->data.key << "  ";
        TraverseBST(T->rchild);
    }
}//二叉排序树的中序遍历

//调用菜单
void Menu()
{
    printf("\t\t\t********************此为最后一次数据结构实验的菜单***********************\n");
    printf("\t\t\t\t1、顺序表                        2、 二叉排序树                       \n");
    printf("\t\t\t\t0、退出当前操作系统                                                   \n");
    printf("\t\t\t**********************************************************************\n");
}//调用菜单的打印

void Menu_SL()
{
    printf("\t\t\t*************************此为顺序表的菜单*******************************\n");
    printf("\t\t\t\t1、顺序表的创建                          2、顺序查找                   \n");
    printf("\t\t\t\t3、冒泡排序                              4、快速排序                   \n");
    printf("\t\t\t\t5、折半查找法                            6、遍历顺序表                 \n");
    printf("\t\t\t\t0、退出当前操作系统                                                   \n");
    printf("\t\t\t**********************************************************************\n");
}//顺序表菜单的打印

void Menu_BST()
{
    printf("\t\t\t***********************此为二叉排序树的菜单*****************************\n");
    printf("\t\t\t\t1、二叉排序树的建立                      2、二叉排序树的查找            \n");
    printf("\t\t\t\t3、二叉排序树的插入                      4、二叉排序树的遍历            \n");
    printf("\t\t\t\t0、退出当前操作系统                                                   \n");
    printf("\t\t\t**********************************************************************\n");
}//二叉排序树菜单的打印

//
//Created by somewon on 2022/12/6.
//

二、主调函数(main.cpp)

#include <iostream>
#include "before.h"

using namespace std;

int main()
{
    int CH;
    do {
        Menu();
        cout << "---请输入进入的菜单:";
        cin >> CH;
        if(CH == 1)         //顺序表调用函数
        {
            int ch;
            int numb;
            int fine,cnt;
            KeyType find;
            SSTable ST;
            do {
                Menu_SL();
                cout << "---请输入你的选择:";
                cin >> ch;
                switch(ch)
                {
                    case 1:
                        cout << "---请输入顺序表元素的个数:";
                        cin >> numb;
                        fine = CreateSST(ST,numb);
                        if(fine == ERROR) cout << "当前顺序表空间分配失败!请再次进行操作!" << endl;
                        else TraverseSST(ST);
                        break;
                    case 2:
                        cout << "---请输入需要查找的元素:";
                        cin >> find;
                        cnt = 0;                     //查找次数
                        fine = Search_Seq(ST,find,cnt);
                        if(fine == 0)
                        {
                            cout << "该元素不在顺序表中!" << endl;
                            cout << "查找次数为:" << cnt << endl;        //末尾开始查找
                        }
                        else
                        {
                            cout << "所查找的元素" << find << "的位置为:" << fine << endl;
                            cout << "查找次数为:" << cnt << endl;        //末尾开始查找
                        }
                        break;
                    case 3:
                        fine = Bubble(ST);
                        if(fine == OVERFLOW) cout << "当前顺序表为空!" << endl;
                        else if(fine == OK)
                        {
                            cout << "冒泡排序成功!" << endl;
                            TraverseSST(ST);
                        }
                        break;
                    case 4:
                        fine = QuickSort(ST,1,ST.length);
                        if(fine == OK)
                        {
                            cout << "快速排序成功!" << endl;
                            TraverseSST(ST);
                        }
                        break;
                    case 5:
                        cout << "---请输入需要查找的元素:";
                        cin >> find;
                        cnt = 0;
                        fine = Search_Bin(ST,find,cnt);
                        if(fine == ERROR)
                        {
                            cout << "该元素不在顺序表中!" << endl;
                            cout << "查找的次数为:" << cnt << endl;
                        }
                        else if(fine == OVERFLOW) cout << "该表为空!" << endl;
                        else
                        {
                            cout << "所查找的元素" << find << "的位置为:" << fine << endl;
                            cout << "查找的次数为:" << cnt << endl;
                        }
                        break;
                    case 6:
                        TraverseSST(ST);
                        break;
                    case 0:
                        goto END;
                }
            }while(ch);
        }
        else if(CH == 2)    //二叉排序树调用函数
        {
            int ch;
            BSTree T,s;
            int cnt;
            KeyType find;
            ElemType e;
            do {
                Menu_BST();
                cout << "---请输入你的选择:";
                cin >> ch;
                switch(ch)
                {
                    case 1:
                        CreateBST(T);
                        cout << "二叉排序树建立完成!" << endl;
                        TraverseBST(T);
                        cout << endl;
                        break;
                    case 2:
                        cout << "---请输入需要查询的整型数据:";
                        cin >> find;
                        cnt = 0;
                        s = NULL;
                        s = SearchBST(T,find,cnt);
                        if(s == NULL || s->data.key!=find)
                        {
                            cout << "该元素不存在于二叉排序树中!" << endl;
                            cout << "查找次数为:" << cnt << endl;
                        }
                        else
                        {
                            cout << "查找的元素" << find <<"存在于二叉排序树中!" << endl;
                            cout << "查找次数为:" << cnt << endl;
                        }
                        break;
                    case 3:
                        cout << "---请输入需要插入的元素:";
                        cin >> e.key;
                        InsertBST(T,e);
                        cout << "插入成功!" << endl;
                        cout << "当前排序二叉树为:" << endl;
                        TraverseBST(T);
                        cout << endl;
                        break;
                    case 4:
                        cout << "当前排序二叉树为:" << endl;
                        TraverseBST(T);
                        cout << endl;
                        break;
                    case 0:
                        goto END;
                }
            }while(ch);
        }
        else if(CH == 0) goto ENDD;
        else cout << "无该选择!" << endl;
        END:
        cout << "已退出当前菜单!" << endl;
    }while(CH);
    ENDD:
    cout << "已退出当前程序!" << endl;
    return 0;
}
//
//Created by somewon on 2022/12/6.
//

三、头文件(before.h)

#ifndef _BEFORE_H
#define _BEFORE_H

#define OK 1
#define ERROR 0
#define OVERFLOW (-1)

typedef int status;
typedef int KeyType;
typedef int InfoType;
typedef struct
{
    KeyType key;
    InfoType otherinfo;
}ElemType;

//顺序表相关函数声明
typedef struct
{
    ElemType *R;
    int length;
}SSTable;

status CreateSST(SSTable &ST,int numb);           //创建顺序表
status Search_Seq(SSTable ST,KeyType key,int &cnt);//顺序查找
status Search_Bin(SSTable ST,KeyType key,int &cnt);//二分查找
status Bubble(SSTable &ST);                       //冒泡排序
status QuickSort(SSTable &ST,int left,int right); //快速排序
void TraverseSST(SSTable ST);                     //遍历顺序表

//排序二叉树相关函数声明
typedef struct BSTNode
{
    ElemType data;
    struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

void CreateBST(BSTree &T);                        //二叉排序树的创建
void InsertBST(BSTree &T,ElemType e);             //二叉排序树的插入
BSTree SearchBST(BSTree T,KeyType key,int &cnt);  //二叉排序树的递归查找
status TraverseBST(BSTree T);                     //二叉排序树的遍历

//相关菜单的函数声明
void Menu();                                    //大菜单
void Menu_SL();                                 //顺序表的菜单
void Menu_BST();                                //二叉排序树的菜单

#endif
//
//Created by somewon on 2022/12/6.
//

四、运行截图及功能函数部分展示

        1、顺序表

       (1)顺序表的创建

status CreateSST(SSTable &ST,int numb)
{
    ST.R = (ElemType *)malloc(sizeof(ElemType)*(numb+1));
    if(!ST.R) return ERROR;                     //空间分配失败
    ST.length = numb;
    cout << "---请输入" << numb << "个元素:" << endl;
    for(int i = 1;i < numb+1;i ++) cin >> ST.R[i].key;
    fflush(stdin);                        //清除缓存
    return OK;                                 //创建成功
}//创建顺序表

         (2)顺序查找法

status Search_Seq(SSTable ST,KeyType key,int &cnt)
{
    ST.R[0].key = key;                             //监视哨
    int i;
    for(i = ST.length;i > 0;i --)                  //末尾开始查找
    {
        if(ST.R[i].key != key)
        {
            cnt ++;
            cout << "经过的元素:" << ST.R[i].key << endl;
        }
        if(ST.R[i].key == key) break;
    }
    cnt ++;
    return i;                                    //返回-1则不存在于顺序表中
}//顺序查找

 

        (3)冒泡排序

status Bubble(SSTable &ST)
{
    int length = ST.length;
    if(length < 1) return OVERFLOW;                 //表为空
    for(int i = length-1;i > 0;i --)                //循环次数
    {
        for(int j = 1;j < i+1;j ++)
        {
            if(ST.R[j].key > ST.R[j+1].key)         //后一个值比前一个值小就交换
            {
                KeyType temp = ST.R[j].key;
                ST.R[j].key = ST.R[j+1].key;
                ST.R[j+1].key = temp;
            }
        }
    }
    return OK;
}//冒泡排序

         (4)折半查找法

status Search_Bin(SSTable ST,KeyType key,int &cnt)
{
    int low = 1;
    int high = ST.length;
    int mid;
    if(high < 1) return OVERFLOW;                    //表空
    while(low <= high)
    {
        mid = (low+high)/2;
        cout << "经过元素:" << ST.R[mid].key << endl;
        cnt ++;
        if(key == ST.R[mid].key) return mid;        //找到待查元素
        else if(key < ST.R[mid].key) high = mid-1;  //在前一子表进行查找
        else low = mid+1;                           //在后一子表进行查找
    }
    return ERROR;                                   //key不存在于顺序表
}//二分查找

        2、二叉排序树

        (1)二叉排序树的建立

void CreateBST(BSTree &T)
{
    T = NULL;          //初始化为空树
    ElemType e;
    int numb[1000],i;
    cout << "---请输入一组二叉排序树的整型元素(以-1作为结束标志):" << endl;
    for(i = 0;numb[i] != -1;i ++)
    {
        cin >> numb[i];
        if(numb[i] == -1) break;
        e.key = numb[i];
        InsertBST(T,e);
    }
}//二叉排序树的创建

         (2)二叉排序树的插入

void InsertBST(BSTree &T,ElemType e)
{
    if(!T)
    {
        BSTNode *s = (BSTNode *)malloc(sizeof(BSTNode));
        s->data.key = e.key;
        s->lchild = s->rchild = NULL;
        T = s;
    }
    else if(e.key < T->data.key) InsertBST(T->lchild,e);    //插入左子树
    else if(e.key > T->data.key) InsertBST(T->rchild,e);    //插入右子树
}//二叉排序树的插入

 

         (3)二叉排序树的查找

BSTree SearchBST(BSTree T,KeyType key,int &cnt)
{
    cnt ++;
    if((T == NULL) || (T->lchild==NULL&&T->rchild==NULL&&T->data.key!=key) || key==T->data.key)
    {
        return T;//查找结束
    }
    else if(key < T->data.key)
    {
        cout << "经过元素:" << T->data.key << endl;
        return SearchBST(T->lchild,key,cnt); //在左子树中继续查找
    }
    else
    {
        cout << "经过元素:" << T->data.key << endl;
        return SearchBST(T->rchild,key,cnt); //在右子树中继续查找
    }
}//二叉排序树的递归查找

         

五、运行环境

        CLion、Dev(注:Dev中运行可能需要加上一些头文件,如:#include <stdlib.h>)

猜你喜欢

转载自blog.csdn.net/somewon/article/details/128256523
今日推荐