栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

#include<stdio.h>#include <time.h>#define MAX 10typedef int KeyType;typedef int InfoType;typed

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

#include<stdio.h>#include <time.h>#define MAX 10typedef int KeyType;typedef int InfoType;typed

#include
#include
#define MAX 10

typedef int KeyType;
typedef int InfoType;
typedef bool ;
typedef ElenType;

typedef struct//顺序表
{
    int R[MAX];
    KeyType key;
    InfoType data[MAX];//存放元素
    int length;//线性表长度
}RecType;


typedef struct node//二叉树
{
    RecType key;
    InfoType data;
    struct node* lchild, * rchild;
}BSTNode;

void initdata(int a[], int n)
{
    int i; int number = 0;
    
    srand((unsigned)time(NULL));
    for (i = 1; i <= n; i++)
    {
        number = rand() % 10000; //产生10000以内的随机数 
        a[i] = number;
    }
}

int CreateList(RecType*L, ElenType a[],int n)//创建线性表
{
    int i = 0, k = 0;
    L = (RecType*)malloc(sizeof(RecType));
    while (i > n)
    {
        L->data[i] = a[i];
        k++; i++;
    }
    L->length = k;
}

int SeqSearch(RecType R[], int n, KeyType k)//实现线性表的顺序查找算法
{
    int i = 0; 
    while (i < n && R[i].key != k)
        i++;
    if (i >= n)
        return 0;
    else
        return i + 1;
}

int BinSearch(RecType L, int n, KeyType k)//实现线性表的二分查找算法
{
    RecType *L = (RecType*)malloc(sizeof(RecType));
    int low = 0, high = n - 1, mid;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (k == L.R[mid].key)
            return mid + 1;
        if (k <=L.R[mid].key)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return 0;
}

BSTNode* CreateBST(KeyType A[], int n)//创建二叉排序树
{
    BSTNode* bt = NULL;
    int i = 0;
    while (i < n)
    {
        InsertBST(bt, A[i]);
        i++;
    }
    return bt;
}

void Show(int result, int key) {
    if (result == 0)
        printf("n未找到%dn", key);
    else
        printf("n找到%d,位置为%dn", key, result);
    return;
}

BSTNode* BSTSearch(BSTNode* bt, KeyType k)//二叉树查找排序
{
    BSTNode* p = bt;
    while (p != NULL)
    {
        if (p->key == k)
            return p;
        else if (k < p->key)
            p = p->lchild;
        else
            p = p->rchild;
    }
}

void DispBST(BSTNode* bt)
{
    if (bt != NULL)
    {
        printf("%d", bt->key);
        if (bt->lchild != NULL || bt->rchild != NULL)
        {
            printf("(");
            DispBST(bt->lchild);
            if (bt->rchild != NULL)
                printf(",");
            DispBST(bt->rchild);
            printf(")");
        }
    }
}

void main()
{
    int a[MAX]; KeyType k; int n=0,m=0; RecType R[MAX];
    RecType* L = (RecType*)malloc(sizeof(RecType));
    printf("**创建顺序表**n");
    printf("请输入顺序表元素,用空格隔开:n");
    for(int i=0;i     {
        initdata(a[i], n);
    }
    
    printf("n-----------顺序查找-----------n");
    printf("n【请输入要查找的关键字】n>>>");
    getchar();
    scanf("%d", &k);
    int result;
    result = SeqSearch(R[],n,k);
    printf("找到了:%d", result);
    Show(result, k);
    printf("n-------------------------------n");

    printf("n-----------折半查找(非递归)-----------n");
    printf("n【请输入要查找的关键字】n>>>");
    getchar();
    scanf("%d", &k);
    result = BinSearch (R[],m, k);
    Show(result, k);
    printf("n---------------------------------------n");
    
    printf("n-----------二叉排序树查找-----------n");
    printf("n【请输入要查找的关键字】n>>>");
    getchar();
    scanf("%d", &k);
    BSTNode* bt;
    result = BSTSearch(bt, k);
    Show(result, k);
    printf("n---------------------------------------n");
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号