查找排序实验报告
文章《查找排序实验报告》是由【作文仓库】的会员【笑着的人怎么哭了】为大家整理并分享的,仅供大家参考,欢迎阅读!
数据结构实验报告
实验名称:查找
班级:12级电气本2
学号:2012081227
姓名:赵雪磊
指导教师:梁海丽
日期:2013年11月18日
数学与信息技术学院
一、实验目的
1.掌握二叉排序树的概念。
2.掌握查找的不同方法,并能用高级语言实现查找算法。
3.实现二叉排序树的基本操作方法。
二、实验要求
给定一个记录关键字的值,与二叉排序树的根结点值比较,如果小于根结点的值,则向左子树查找;如果大于根结点的值,则向右子树查找。如果查找到叶子结点leaf,仍没有找到记录,则:如果关键字的值小于leaf的值,则插入该leaf结点的左边,做leaf的左孩子,否则做leaf的右孩子。
三、算法描述
template
class hashdict
{
private:
Elem* HT; // 散列表
int M; // 散列表大小
Elem TOMB; //墓碑
int currcnt; // 现有元素数目
Elem EMPTY; // 空槽
int p(Key K, int i) const // 探查函数
{ return linear(i); }
int h(int x) const { return x % M; } // 散列函数
int h(char* x) const { // 字符串散列函数
int i, sum;
for (sum=0, i=0; x[i] != '\0'; i++) sum += (int) x[i];
return(sum % M);
}
int linear(int i) const {return i;} //线性探查
int square(int i) const { //二次探查
if (i%2==0) return -(i*i/4);
else return (i+1)*(i+1)/4;
}
int rehash (Key K, int i) const;
int division(int x ) const { return x % M; } //除余法
int ELFhash ( char* key )const {
unsigned long h = 0;
while(*key) {
h = ( ch << 4 ) + *key++;
unsigned long g = h&0xF0000000L;
if (g) h^= g >> 24;
h = ~g;
}
return h % M;
}
public:
hashdict(int sz, Elem e, Elem t){// 构造函数, e用来定义空槽M=sz; EMPTY= e; TOMB=t;
currcnt=0; HT=new Elem[sz];
for(int i=0; i}
~hashdict() { delete HT; }
bool hashInsert(const Elem&);
bool hashSearch(const Key&, Elem&) const;
Elem hashDelete(const Key& K);
int size() { return currcnt; } // 散列表中现有元素数
};
四、程序清单
#include
#include
#define OK 1
#define ERROR 0
int Search_Bin(int score[],int length,int key)
{
int low,high,mid;
low=1;
high=length;
while(low<=high)
{
mid=(low+high)/2;
if(score[mid]==key)
return mid;
else if(keyhigh=mid-1;
else if(key>score[mid])
low=mid+1;
}
return ERROR;
}
int main()
{
int score[11];
int length=10;
int key,index;
printf(
for(int i=1;i<=length;i++)
{
scanf(
}
printf(
scanf(
if(Search_Bin(score,length,key))
{
index=Search_Bin(score,length,key);
printf(
}
else
printf(
return OK;
}
五、实验结果与分析
六、实验心得
通过本次查找实验,让我懂得了如何应用冒泡排序,如何对有序数组进行折半查
找,加深了对折半查找的理解,初步掌握了其基本概念和操作。