数据结构期末报告 实验四

1.对下列数据表,分别采用二分查找算法实现查找,给出查找过程依次所比较的元素(的下标),并以二分查找的判定树来解释。

实验测试数据:

第一组数据:

数据表为 (1,2,3,4,6,7,8,9,10,11,12,13,17,18,19,20,24,25,26,30,35,40,45,50,100)

查找的元素分别为: 2,8,20, 30,50,5,15,33,110

第二组数据:自拟

#include<iostream>
#include<cmath>
#include<__msvc_all_public_headers.hpp>
using namespace std;
int A[10000];
int bin_search(int A[], int n,int x) {
    int mid, low = 0, high = n - 1;
    //初始化查找区域 
    while (low <= high)
    {
        mid = (low + high) / 2;
        cout << mid <<"  ";
        if (A[mid] == x) return  mid;
        else if (x < A[mid]) high = mid - 1;
        else low = mid + 1;
    }
    return  -1;    //返回查找失败的标志
}
int main() {
    int n, key,choice;
    c1:cout << "请输入数组元素的个数:" << endl;
    cin >> n;
    cout << "请输入数组:" << endl;
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    c2:cout << "请输入要查找的数字:" << endl;
    cin >> key;
    if (bin_search(A, n, key) == -1)cout << "Not found!" << endl;
    else
        cout << endl;
    cout << "是否继续查找?1.继续查找 2.更换数列 其他数字退出" << endl;
    cin >> choice;
    if (choice == 1)goto c2;
    else if (choice == 2)goto c1;
    else return 0;
}

2.设计出在二叉排序树中插入结点的算法,在此基础上实现构建二叉排序树的算法,并给出其中序遍历序列。

实验测试数据:

构建二叉排序树的输入序列如下:100,150,120,50,70,60,80,170,180,160,110,30,40,35,175

3.设计算法在二叉排序树中查找指定值的结点。

   在任务2所建立的二叉排序树中分别查找下列元素:

  150,70,160,190,10,55,175

#include<__msvc_all_public_headers.hpp>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int endkey = 1000;
int a[3000];

typedef struct node
{
    int data;
    struct node* lchild, * rchild;
} node, * bin_tree;

node* root;
node* get_root() { return root; }

void insert(node*& T, node* s) {
    if (T == NULL)
        T = s;
    else
        if (s->data < T->data)
            insert(T->lchild, s);
        else
            insert(T->rchild, s);
}

void create_bst(node*& T) {
    node* u;
    int x;
    T = NULL;
    cin >> x;
    while (x != 1000) {
        node* s = new node;
        s->data = x;
        s->lchild = NULL;
        s->rchild = NULL;
        insert(T, s);
        cin >> x;
    }
}

void InOrder(bin_tree T)
{
    if (T)
    {
        InOrder(T->lchild);
        cout << (T->data)<<" ";
        InOrder(T->rchild);
    }
}

bin_tree  SearchBST(bin_tree bst, int data)
/*在根指针bst所指二叉排序树bst上,查找关键字等于key的结点,若查找成功,返回指向该元素结点指针,否则返回空指针*/
{
    bin_tree q;
    q = bst;
    while (q)
    {
        if (q->data == data)
            return q;  //查找成功
        if (q->data > data)
            q = q->lchild;  //在左子树中查找
        else
            q = q->rchild;  //在右子树中查找
    }
    return NULL;    //查找失败
}

int main() {
    int n,choice,key;
    node* T=NULL;
    bin_tree result;
    rewrite:cout << "请输入数据长度" << endl;
    cin >> n;
    cout << "请输入数据:(以1000结束)" << endl;
    create_bst(T);
    cout << "建立完成!"<<endl;
    cout << "二叉树的中序遍历为:";
    InOrder(T);
    cout << endl;
    research:cout << "请输入要查询的值:" << endl;
    cin >> key;
    result = SearchBST(T, key);
    if (result != NULL)
        cout << "找到数据,找到的数据为:" << result->data << endl;
    else
        cout << "没有找到数据!" << endl;
    cout << "是否继续寻找?1.继续寻找 2.重新输入数据(输入其他数字结束):" << endl;
    cin >> choice;
    if (choice == 1)goto research;
    if (choice == 2)goto rewrite;
    else 
        goto end;
    end:return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值