(1)实验要求:
掌握常用的查找方法,了解各种查找方法的过程及其依据的原则,并掌握各种查找方法的效率的分析方法。
(2)实验内容:
必做:随机产生一组m到n之间的一组整数,根据这一组整数完成如下操作:
建立一个顺序表,输入一个关键字,对该表进行顺序查找和折半查找。
选做:从键盘读入一组数据,建立二叉排序树并对其进行查找、遍历、插入、删除等有关操作。
顺序查找就是从头开始扫一遍,找到该元素
折半查找就是二分,每次均分两半找到该元素,折半查找是对有序顺序表的操作。这个实验我们随机生成的一组数据没有顺序,所有需要我们先对其进行排序,然后才能用二分进行查找。搜索该元素是否在序列中。
C++有lower_bound和upper_bound函数,可以直接二分
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
顺序表的建立和初始化
#define ListInitSize 100//初始时线性表的内存
#define ListIncerment 10//当内存不足时追加的内存
typedef struct
{
int *base;
int length;
int listsize;
}SqList;
//初始化操作
void InitSqList(SqList &L)
{
L.base = (int*)malloc(ListInitSize*sizeof(int));
if(!L.base){
cout << "分配失败";
return ;
}
L.length = 0;
L.listsize = ListInitSize;
}
顺序查找
//顺序查找
void sxcz(SqList L,int a)
{
cout << "顺序查找" << endl;
bool flag = 0;
for(int i = 0;i < L.length;i++){
if(L.base[i] == a){
cout << "找到该元素" << endl;
flag = 1;
}
}
if(!flag)
cout << "没有找到给元素" << endl;
}
折半查找代码
//折半查找
void zbcz(SqList L,int a)
{
cout << "折半查找" << endl;
int l = 0;
int r = L.length-1;
int mid;
bool flag = 0;
while(l <= r){
mid = (l+r)/2;
// cout << mid <
主程序
SqList L;
InitSqList(L);
srand(unsigned(time(0)));//用系统时间做伪随机数种子
int n;
cout << "请输入需要插入元素的个数:";
cin >> n;
int x,y;
cout << "请输入需要限制的元素范围m和n:";
cin >> x >> y;
if(x > y)
swap(x,y);
for(int i = 0;i < n;i++){
L.base[i] = rand() % (y-x) + x;
L.length++;
cout << L.base[i] << " ";
}
cout << endl;
cout << "请输入需要查找的元素:";
int a;
cin >> a;
sxcz(L,a);
paixu(L);
cout << "排序后的元素顺序"<< endl;
for(int i = 0;i < L.length;i++){
cout << L.base[i] << " ";
}
cout << endl;
zbcz(L,a);
#include
using namespace std;
typedef long long ll;
typedef struct BiTNode {
int data;
struct BiTNode *leftchild, *rightchild;
}BiTnode,*BiTree;
//查找
bool searchBST(BiTree T,int key,BiTree f,BiTree &p)
{
if (!T)
{
p = f;
return false;
}
else if (key == T->data)
{
p = T;
return true;
}
else if (key < T->data)
return searchBST(T->leftchild, key, T, p);
else
return searchBST(T->rightchild, key, T, p);
}
//插入
bool insertBST(BiTree &T, int key)
{
BiTree p, s;
if (!searchBST(T, key, NULL, p))
{
s = (BiTree)malloc(sizeof(BiTNode));
s->data = key;
s->leftchild = s->rightchild = NULL;
if (!p)
T = s;
else if(key < p->data)
p->leftchild = s;
else
p->rightchild = s;
return true;
}
else
return false;
}
//删除
bool Delete(BiTree &p)
{
BiTree q, s;
if (p->rightchild == NULL) //右子树空,则重接左子树
{
q = p;
p = p->leftchild;
free(q);
}
else if (p->leftchild == NULL) //左子树空,则重接右子树
{
q = p; p = p->rightchild; free(q);
}
else //两个子树都是非空的
{
q = p; s = p->leftchild;
while (s->rightchild)
{
q = s;
s = s->rightchild;
}
p->data = s->data;
if (q != p)
q->rightchild = s->leftchild;
else
q->leftchild = s->leftchild;
free(s);
}
return true;
}
bool deleteBST(BiTree &T,int key)
{
if (!T)
return false;
else
{
if (key == T->data)
return Delete(T);
else if (key < T->data)
return deleteBST(T->leftchild, key);
else
return deleteBST(T->rightchild, key);
}
}
void InOrderTraverse(BiTree T)
{
if (T)
{
InOrderTraverse(T->leftchild);
printf("%d ", T->data);
InOrderTraverse(T->rightchild);
}
}
int main()
{
//二叉树的创建
int n;
cout << "输入二叉树的节点个数:";
cin >> n;
BiTree T = NULL;
cout << "开始建树BST,请输入" << n << "个节点\n";
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
insertBST(T, a);
}
cout << "中序遍历\n";
InOrderTraverse(T);
cout << endl;
//二叉树的查找与插入
BiTree f = NULL;
BiTree p;
cout << "请输入需要查找的值:";
int key;
cin >> key;
if (searchBST(T, key, f, p))
cout << "该元素在二叉排序树中\n";
else
{
cout << "该元素不在二叉排序树中\n";
}
//二叉树的删除
cout << "请输入想要删除的元素:";
cin >> key;
if (searchBST(T, key, f, p))
{
deleteBST(T, key);
cout << "删除后的中序遍历: \n";
InOrderTraverse(T);
cout << endl;
}
else
cout << "该元素不在二叉排序树中\n";
}