第十三周项目一C/C++验证算法

来源:互联网 发布:软件售后服务流程图 编辑:程序博客网 时间:2024/06/08 06:15
/*   *Copyright (c) 2017,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:.cpp*完成日期:2017年11月30日   *版 本 号:v1.0   * 验证算法 /* 

1、认真阅读并验证折半查找算法。请用有序表{12,18,24,35,47,50,62,83,90,115,134}作为测试序列,分别对查找90、47、100进行测试。

折半查找

#include <stdio.h>#define MAXL 100typedef int KeyType;typedef char InfoType[10];typedef struct{    KeyType key;                //KeyType为关键字的数据类型    InfoType data;              //其他数据} NodeType;typedef NodeType SeqList[MAXL];     //顺序表类型int BinSearch(SeqList R,int n,KeyType k){    int low=0,high=n-1,mid;    while (low<=high)    {        mid=(low+high)/2;        if (R[mid].key==k)      //查找成功返回            return mid+1;        if (R[mid].key>k)       //继续在R[low..mid-1]中查找            high=mid-1;        else            low=mid+1;          //继续在R[mid+1..high]中查找    }    return 0;}int main(){    int i,n=10;    int result;    SeqList R;    KeyType a[]= {1,3,9,12,32,41,45,62,75,77},x=75;    for (i=0; i<n; i++)        R[i].key=a[i];    result = BinSearch(R,n,x);    if(result>0)        printf("序列中第 %d 个是 %d\n",result, x);    else        printf("木有找到!\n");    return 0;}
运行结果:


递归的折半查找算法

#include <stdio.h>#define MAXL 100typedef int KeyType;typedef char InfoType[10];typedef struct{    KeyType key;                //KeyType为关键字的数据类型    InfoType data;              //其他数据} NodeType;typedef NodeType SeqList[MAXL];     //顺序表类型int BinSearch1(SeqList R,int low,int high,KeyType k){    int mid;    if (low<=high)      //查找区间存在一个及以上元素    {        mid=(low+high)/2;  //求中间位置        if (R[mid].key==k) //查找成功返回其逻辑序号mid+1            return mid+1;        if (R[mid].key>k)  //在R[low..mid-1]中递归查找            BinSearch1(R,low,mid-1,k);        else              //在R[mid+1..high]中递归查找            BinSearch1(R,mid+1,high,k);    }    else        return 0;}int main(){    int i,n=10;    int result;    SeqList R;    KeyType a[]= {1,3,9,12,32,41,45,62,75,77},x=75;    for (i=0; i<n; i++)        R[i].key=a[i];    result = BinSearch1(R,0,n-1,x);    if(result>0)        printf("序列中第 %d 个是 %d\n",result, x);    else        printf("木有找到!\n");    return 0;}
运行结果:


2、认真阅读并验证分块查找算法。请用22,4,23,11,20,2,15,13,30,45,26,34,29,35,26,36,55,98,56, 74,61,90,80,96,127,158,116,114,128,113,115,102,184,211,243,188,187,218,195,210,279,307,492,452,408,361,421,399,856,523,704,703,697,535,534,739(共n=56个数据,每块数据个数s=8)作为数据表,自行构造索引表,分别对查找61、739、200进行测试。 

分块查找

#include <stdio.h>#define MAXL 100    //数据表的最大长度#define MAXI 20     //索引表的最大长度typedef int KeyType;typedef char InfoType[10];typedef struct{    KeyType key;                //KeyType为关键字的数据类型    InfoType data;              //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //顺序表类型typedef struct{    KeyType key;            //KeyType为关键字的类型    int link;               //指向对应块的起始下标} IdxType;typedef IdxType IDX[MAXI];  //索引表类型int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k){    int low=0,high=m-1,mid,i;    int b=n/m;              //b为每块的记录个数    while (low<=high)       //在索引表中进行二分查找,找到的位置存放在low中    {        mid=(low+high)/2;        if (I[mid].key>=k)            high=mid-1;        else            low=mid+1;    }    //应在索引表的high+1块中,再在线性表中进行顺序查找    i=I[high+1].link;    while (i<=I[high+1].link+b-1 && R[i].key!=k) i++;    if (i<=I[high+1].link+b-1)        return i+1;    else        return 0;}int main(){    int i,n=25,m=5,j;    SeqList R;    IDX I= {{14,0},{34,5},{66,10},{85,15},{100,20}};    KeyType a[]= {8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87};    KeyType x=85;    for (i=0; i<n; i++)        R[i].key=a[i];    j=IdxSearch(I,m,R,n,x);    if (j!=0)        printf("%d是第%d个数据\n",x,j);    else        printf("未找到%d\n",x);    return 0;}
运行结果:


3、认真阅读并验证二叉排序树相关算法。 
(1)由整数序列{43,52,75,24,10,38,67,55,63,60}构造二叉排序树; 
(2)输出用括号法表示的二叉排序树; 
(3)用递归算法和非递归算法查找关键字55; 
(4)分别删除43和55,输出删除后用括号法表示的二叉排序树。 

#include <stdio.h>#include <malloc.h>typedef int KeyType;typedef char InfoType[10];typedef struct node                 //记录类型{    KeyType key;                    //关键字项    InfoType data;                  //其他数据域    struct node *lchild,*rchild;    //左右孩子指针} BSTNode;//在p所指向的二叉排序树中,插入值为k的节点int InsertBST(BSTNode *&p,KeyType k){    if (p==NULL)                        //原树为空, 新插入的记录为根结点    {        p=(BSTNode *)malloc(sizeof(BSTNode));        p->key=k;        p->lchild=p->rchild=NULL;        return 1;    }    else if (k==p->key)                 //树中存在相同关键字的结点,返回0        return 0;    else if (k<p->key)        return InsertBST(p->lchild,k);  //插入到*p的左子树中    else        return InsertBST(p->rchild,k);  //插入到*p的右子树中}//由有n个元素的数组A,创建一个二叉排序树BSTNode *CreateBST(KeyType A[],int n)   //返回BST树根结点指针{    BSTNode *bt=NULL;                   //初始时bt为空树    int i=0;    while (i<n)    {        InsertBST(bt,A[i]);             //将关键字A[i]插入二叉排序树T中        i++;    }    return bt;                          //返回建立的二叉排序树的根指针}//输出一棵排序二叉树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(")");                        //有孩子结点时才输出)        }    }}//在bt指向的节点为根的排序二叉树中,查找值为k的节点。找不到返回NULLBSTNode *SearchBST(BSTNode *bt,KeyType k){    if (bt==NULL || bt->key==k)         //递归终结条件        return bt;    if (k<bt->key)        return SearchBST(bt->lchild,k);  //在左子树中递归查找    else        return SearchBST(bt->rchild,k);  //在右子树中递归查找}//二叉排序树中查找的非递归算法BSTNode *SearchBST1(BSTNode *bt,KeyType k){    while (bt!=NULL)    {        if (k==bt->key)            return bt;        else if (k<bt->key)            bt=bt->lchild;        else            bt=bt->rchild;    }    return NULL;}void Delete1(BSTNode *p,BSTNode *&r)  //当被删*p结点有左右子树时的删除过程{    BSTNode *q;    if (r->rchild!=NULL)        Delete1(p,r->rchild);   //递归找最右下结点    else                        //找到了最右下结点*r    {        p->key=r->key;          //将*r的关键字值赋给*p        q=r;        r=r->lchild;            //直接将其左子树的根结点放在被删结点的位置上        free(q);                //释放原*r的空间    }}void Delete(BSTNode *&p)   //从二叉排序树中删除*p结点{    BSTNode *q;    if (p->rchild==NULL)        //*p结点没有右子树的情况    {        q=p;        p=p->lchild;            //直接将其右子树的根结点放在被删结点的位置上        free(q);    }    else if (p->lchild==NULL)   //*p结点没有左子树的情况    {        q=p;        p=p->rchild;            //将*p结点的右子树作为双亲结点的相应子树        free(q);    }    else Delete1(p,p->lchild);  //*p结点既没有左子树又没有右子树的情况}int DeleteBST(BSTNode *&bt, KeyType k)  //在bt中删除关键字为k的结点{    if (bt==NULL)        return 0;               //空树删除失败    else    {        if (k<bt->key)            return DeleteBST(bt->lchild,k); //递归在左子树中删除为k的结点        else if (k>bt->key)            return DeleteBST(bt->rchild,k); //递归在右子树中删除为k的结点        else        {            Delete(bt);     //调用Delete(bt)函数删除*bt结点            return 1;        }    }}int main(){    BSTNode *bt;    int n=12,x=46;    KeyType a[]= {25,18,46,2,53,39,32,4,74,67,60,11};    bt=CreateBST(a,n);    printf("BST:");    DispBST(bt);    printf("\n");    printf("删除%d结点\n",x);    if (SearchBST(bt,x)!=NULL)    {        DeleteBST(bt,x);        printf("BST:");        DispBST(bt);        printf("\n");    }    return 0;}
运行结果:


4、认真阅读并验证平衡二叉树相关算法。 
(1)由整数序列{43,52,75,24,10,38,67,55,63,60}构造AVL树; 
(2)输出用括号法表示的AVL树; 
(3)查找关键字55; 
(4)分别删除43和55,输出删除后用括号法表示的二叉排序树。