数据结构实验2 查找算法的实现和应用

时间:2024.3.31

实验2   查找算法的实现和应用

q 实验目的

1. 熟练掌握静态查找表的查找方法;

2. 熟练掌握动态查找表的查找方法;

3. 掌握hash表的技术.

q 实验内容

1. 用二分查找法对查找表进行查找;

2. 建立二叉排序树并对该树进行查找;

3. 确定hash函数及冲突处理方法,建立一个hash表并实现查找。

1.二分查找

#include <iostream>

using namespace std;

#define INVALID_INDEX -100

int IntCompare(const int& a, const int& b, void* param)

{

    return a - b;

}

template<class T1, class T2>

int BinarySearch(const T1* theArray,   int length,   const T2& key,   int (*compare)(const T1&, const T2&, void* param),    void *param)

{

    int indexRet = INVALID_INDEX;

    int mid = length / 2;

    int cmp = compare(theArray[mid], key, param);

    if (cmp == 0)

    {

        indexRet = mid;

    }

    else if (length != 1)

    {

        if (cmp < 0 && length != 1)

        {

                 indexRet = BinarySearch(theArray + mid, length - mid, key, compare,param);

            if (indexRet != INVALID_INDEX)

            {

                indexRet += mid;

            }

        }

        else

        {

            indexRet = BinarySearch(theArray, mid, key, compare, param);

        }

    }

    return indexRet;

}

int main()

{

    int length = 0;

    int i = 0;

    int key = 0;

    int index = INVALID_INDEX;

    cout <<"请输入元素的个数:"<<endl;

    cin >> length;

    int* aArray = new int[length];

    cout<<"请输入要输入的元素:"<<endl;

    for (i = 0; i < length; i++)

    {

        cin >> aArray[i];

    }

    cout<<"要查找的元素:"<<endl;

    while(cin >> key&&key != 'F'){

    index = BinarySearch(aArray, length, key, IntCompare, NULL);

    if (index == INVALID_INDEX)

    {

        cout << "The element is not exist." << endl;

    }

   else

    {

       cout << "The element position is " << index << "." << endl;     }

  delete aArray; }

    return 0;

}

2 二叉排序树

#include <cstdio>

#include <iostream>

#include <cstdlib>

using namespace std;

typedef int keyType;

typedef struct Node

{

    keyType key;

    struct Node* left;

    struct Node* right;

    struct Node* parent;

}Node,*PNode;

void inseart(PNode* root, keyType key)

{

    PNode p = (PNode)malloc(sizeof(Node));

    p -> key = key;

    p -> left = p -> right = p -> parent = NULL;

    if((*root) == NULL)

    {

        *root = p;

        return ;

    }

    if((*root)->left == NULL && (*root)->key > key)

    {

       p->parent=(*root);

        (*root)->left=p;

        return;

    }

     if((*root)->right == NULL && (*root)->key < key)

     {

       p->parent=(*root);

        (*root)->right=p;

        return;

    }

    if((*root) -> key > key)

    {

        inseart(&(*root) -> left, key);

    }

    else if((*root) -> key < key)

    {

        inseart(&(*root) -> right, key);

    }

    else

        return ;

}

PNode search(PNode root,keyType key)

{

    if(root == NULL)

    {

        return NULL;

    }

    if(key > root -> key)

    {

        return search(root -> right, key);

    }

    else if(key < root -> key)

    {

        return search(root -> left, key);

    }

    else

        return root;

}

void create(PNode* root, keyType* keyArray, int length)

{

    int i;

    for(i = 0; i < length; i++)

    {

        inseart(root, keyArray[i]);

    }

}

  int main()

{

    int i;

    PNode root = NULL;

    int a[100];

    int n;

    scanf("%d",&n);

    for(i = 0; i < n; i++)

    {

        scanf("%d",&a[i]);

    }

    create(&root, a,n);

    int m;

   while(~scanf("%d",&m)){

    if(search(root,m) ->key){

        printf("YES\n");

}

}

    return 0;

}

3 hash

#include <iostream>

//#include <ctime>

#include <iomanip>

using namespace std;

enum Result{

    ok = 2,

    success = 1,

    unsuccess = 0,

    duplicate = -1,

    fail = -2,

};

typedef int elemtype;

typedef struct{

    elemtype* elem;

    int count;

    int sizeindex;

}hashTable;

void Inithash(hashTable &H, int n){

    H.elem = new elemtype[n];

    H.count = 0;

    H.sizeindex = n;

    for(int i = 0; i < n; i++)

    {

        H.elem[i] = 0;

    }

}

int hash(elemtype K, int sizeindex)

{

    int count = H.count;

    int haAddr = K % sizeindex;

    return haAddr;

}

int SearchHash(hashTable &H, elemtype K, int &haAddr, int &c)

{

    int d = 1;

    int sizeindex = H.sizeindex;

    haAddr = hash(K, sizeindex);

    while(H.elem[haAddr] != NULL && H.elem[haAddr] != K)

    {

        haAddr = (K % sizeindex + d) % sizeindex;

        d++;

        c++;

        cout<<"产生冲突,解决中…………"<<endl;

        if(c > H.sizeindex - 2)

        {

            cout<<"冲突次数过多,重新建立哈希表"<<endl;

            break;

        }

    }

    if(K == H.elem[haAddr])

    {

        cout<<"chenggong"<<endl;

        return success;

    }

    else

    {

        cout<<"fail"<<endl;

        return unsuccess;

    }

}

int InsertHash(hashTable &H, elemtype e)

{

    int collision = 0;

    int haAddr = -1;

    if(SearchHash(H, e, haAddr, collision) == success)

    {

        cout<<"存在该元素!!!"<<endl;

        return duplicate;

    }

    else if(collision < H.sizeindex - 2)

    {

        H.elem[haAddr] = e;

        H.count++;

        cout<<"插入成功!!!"<<endl;

        return ok;

    }

    else

    {

     cout<<"冲突次数过多,重新建立哈希表"<<endl;

     return fail;

    }

}

int hashsearch(hashTable &H, elemtype e)

{

    int p = 0;

    for(int i = 0; i < H.sizeindex; i++)

    {

        if(H.elem[i] == e)

        {

            //H.elem[i] == 0;

            p = 1;

            cout <<"hashaddress = "<<i<<endl;

            if(p == 0)

                return unsuccess;

            else

                return success;

        }

    }

}

void PrintHash(hashTable H)

{

    int num = 1;

    cout<<"哈希表为……!"<<endl;

    for(int i = 0; i < H.sizeindex; i++)

    {

        if(num % 10 == 0)

            cout <<endl;

        if(H.elem[i] != NULL)

        {

            cout <<setw(5)<<H.elem[i];

            num++;

        }

        else continue;

    }

    cout <<endl;

}

void Performance(hashTable &H){

    //hashTable H;

    int sizeindex;

    cout <<哈希表容量为……!<<endl;

    cin >>sizeindex;

    Inithash(H, sizeindex);

    int e = 1;

    cout<<"输入插入元素,否则输入'0'"<<endl;

    cin >>e;

    while(e != 0)

    {

        InsertHash(H, e);

        cout<<"输入插入元素,否则输入'0'"<<endl;

        cin >>e;

    }

    PrintHash(H);

    int k = 1;

    cout<<"输入要查找元素:";

    //int  k;

    cin>>k;

    hashsearch(H,k);

    int status = hashsearch(H,K);

    if(!status)

        cout<<"该元素不存在"<<endl;

}

int main()

{

    hashTable H;

    Performance(H);

    cin.get();

    cin.get();

    return 0;

}


第二篇:数据结构实验指导(实验五:查找算法)


实验五 查找算法

实验项目:必做:顺序查找、折半查找

选做:二叉查找树

实验类型: 验证性

实验内容:

顺序查找:用数组或链表实现,数据有序或无序均可;

折半查找:必须用数组实现,且数据有序;

注意:提交的实验报告要显示已有的数据元素、待查找的数据;应包含查找成功、不成功的情况。

更多相关推荐:
数据结构查找实验报告

实验题91设计一个程序exp91cpp输出在顺序表36210185749中采用顺序方法找关键字5的过程程序如下文件名exp91cppincludeltstdiohgtdefineMAXL100typedefin...

数据结构实验报告 实验五 查找算法

昆明理工大学信息工程与自动化学院学生实验报告201201学年第一学期课程名称数据结构开课实验室年月日一实验内容查找算法其中线性表的查找包括顺序查找二分查找分块查找树表的查找包括二叉排序树等还有散列表的查找等等二...

数据结构查找实验报告

实验报告课程名称实验项目数据结构查找姓名xx专业班级学号网络工程网络132130402xxxx计算机科学与技术学院实验教学中心20xx年12月10日哈尔滨理工大学计算机科学与技术学院实验教学中心实验报告实验项目...

数据结构查找算法实验报告

100410528孙晨添数据结构实验报告实验第四章实验简单查找算法一需求和规格说明查找算法这里主要使用了顺序查找折半查找二叉排序树查找和哈希表查找四种方法由于自己能力有限本想实现其他算法但没有实现其中顺序查找相...

《数据结构》实验报告查找

实验四查找一实验目的1掌握顺序表的查找方法尤其是折半查找方法2掌握二叉排序树的查找算法二实验内容1234建立一个顺序表用顺序查找的方法对其实施查找建立一个有序表用折半查找的方法对其实施查找建立一个二叉排序树根据...

数据结构实验报告-查找算法

数据结构第八次实验报告学生姓名学生班级学生学号指导老师重庆邮电大学计算机学院计算机专业实验中心一实验内容1有序表的二分查找建立有序表然后进行二分查找2二叉排序树的查找建立二叉排序树然后查找二需求分析二分查找的基...

数据结构动态查找表实验报告

成都信息工程学院计算机系课程实验报告一上机实验目的1深入理解数据结构的算法思想将算法理论与实际应用相结合培养学生的编程能力与编程兴趣让学生清楚从项目分析编码调试程序维护的整个程序开发流程2使学生清楚解决一个编程...

数据结构实验报告(C语言)(强力推荐)

数据结构实验实验内容和目的掌握几种基本的数据结构集合线性结构树形结构等在求解实际问题中的应用以及培养书写规范文档的技巧学习基本的查找和排序技术让我们在实际上机中具有编制相当规模的程序的能力养成一种良好的程序设计...

数据结构实验报告

数据结构随堂实验实验报告指导教师姓名学号班级专业计算机科学与技术目录C语言结构体与指针1线性顺序表的实现及操作3串的匹配与替换6线性链表的实现及操作8栈和队列的应用13二叉树的实现及遍历21图的实现及遍历27查...

数据结构实验报告(C语言)(强力推荐)

数据结构实验实验内容和目的掌握几种基本的数据结构集合线性结构树形结构等在求解实际问题中的应用以及培养书写规范文档的技巧学习基本的查找和排序技术让我们在实际上机中具有编制相当规模的程序的能力养成一种良好的程序设计...

安徽工业大学数据结构实验报告

安徽工业大学计算机学院数据结构实验报告姓名学号班级教师内容线性表基本操作的实现栈的基本操作串的模式匹配二叉树操作图的创建与遍历20xx525实验一线性表基本操作的实现一实验目的1掌握使用TurboC20上机调试...

数据结构实验报告(C语言)顺序表查找

计算机科学与技术系实验报告专业名称计算机科学与技术课程名称数据结构与算法项目名称顺序表查找班级学号姓名实验日期格式要求实验报告注意格式规范要求在word中编写文中不要有空行统一使用A4页面页边距上25cm下2c...

数据结构查找实验报告(40篇)