实验四 查找
2013-02-02 20:59
106 查看
实验四查找
一、 实验目的
1.掌握顺序查找、二分法查找的算法。
2.能运用线性表的查找方法解决实际问题。
二、 实验内容。
1.实现一个在无序线性表中查找元素x的算法
2.给出在一个递增有序表A中采用二分查找算法查找值为x的元素的算法。
3.
创建一棵二叉搜索树,给出查找元素x的算法
4.
自行设计测试步骤,并在实验步骤里详细说明要测试操作的边界情况。
5.
比较以上三种算法的时间复杂度。
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long clock_t;
int count=0;
template <class T>
struct BSTNode{//二叉树结点类
T data;
BSTNode<T> *left,*right;
BSTNode():left(NULL),right(NULL){}
BSTNode(const Td,BSTNode<T>*l=NULL,BSTNode<T>*r=NULL):
data(d),left(l),right(r){}
~BSTNode(){}
void setdata(Tt){data=t;}
T getdata(){returndata;}
};
template <class T>
class BST{
public:
BST():root(NULL){}
BST(T vvalue);
~BST(){}
bool search(const T& x)const
{
return(search(x,root)!=NULL)?true:false;
}
bool insert(constT& el){return insert(el,root);}
private:
BSTNode<T>*root;
T Refvalue; //输入停止标志,用于输入
int search(constT x,BSTNode<T>*ptr)const; //递归:搜索
bool insert(const T&el,BSTNode<T> *& ptr);//递归:插入
};
template<class T>
int BST<T>::search(const T x,BSTNode<T>*ptr)const
{
if(ptr==NULL){cout<<"失败"<<endl;return 0;}
BSTNode<T>*temp=ptr;
while(temp!=NULL)
{
if(x==temp->data)
{
cout<<temp->data;
count++;
cout<<"搜索成功"<<endl;
break;
}
else if(x<temp->data)
{
cout<<temp->data;
count++;
cout<<"失败"<<endl;
temp=temp->left;
}
else if(x>temp->data)
{
cout<<temp->data;
count++;
cout<<"搜索成功"<<endl;
temp=temp->right;
}
}
return count;
};
template <class T>
bool BST<T>::insert(const T&el,BSTNode<T>*&ptr){
if(ptr==NULL){
ptr=newBSTNode<T>(el);//创建新结点
if(ptr==NULL){cerr<<"Outof Space"<<endl; exit(1);}
return true;
}
elseif(el<ptr->data) insert(el,ptr->left); //左子树插入
else if(el>ptr->data)insert(el,ptr->right); //右子树插入
else return false; //el已在树中,无需插入
}
template <class T>
BST<T>::BST(T vvalue){//建立二叉树
T t;root=NULL;Refvalue=vvalue;//置空树,refvalue
为一个输入结束标志
cin>>t;
while(t!=Refvalue){
insert(t,root);cin>>t; //插入,再输入数据
}
}
void main(void){
//cout<<"建立二叉搜索树:(-1为结束标志)"<<endl;
BST<int> bst; //停止输入标志为-1
int x,c;
int number;
clock_t start,finish;
double duration;
srand((unsigned)time(0));
for(int i=1;i<=100;i++){
x=rand()%100;
bst.insert(x);
}
cout.width(40);
cout<<"二叉树搜索"<<endl;
cout<<"输入要查找数(100以内)"<<endl;
cin>>number;
start=clock();
c=bst.search(number);
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"共查找"<<count<<"次"<<endl;
cout<<duration<<'s'<<endl;
}
#include <stdio.h>
#include <iostream.h>
#include<math.h>
#include<time.h>
#define N 21
void main(void)
{
int a
;
int i,n,num;
inttop,bottom,mid;
int flag=1; //如果在表列中找到数字,则值为1,否则为0
int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置
clock_tstart,end;
double time;
printf("你想在多少个数中进行折半查找,请输入(1--20):");
scanf("%d",&n);
while(n<1 ||n>20)
{
printf("你输入的数不正确,请重新输入。\n");
printf("你想在多少个数中进行折半查找,请输入(1--20):");
scanf("%d",&n);
}
printf("请你输入一个整数 a[1]:");
scanf("%d",&a[1]);
i=2;
printf("从小到大输入列表\n");
while(i<=n) //输入从小到大的表列
{
printf("请你输入一个整数 a[%d]:",i);
scanf("%d",&a[i]);
if(a[i] >a[i-1])
i++;
else
printf("你输入的数不满足要求,请重新输入。\n");
}
//输出表列
printf("\n输出表列\n");
for(i=1; i<=n;i++)
{
printf("%6d",a[i]);
}
printf("\n");
printf("请你输入要查找的数:\n");
scanf("%d",&num);
flag=1; //假设输入的数在表列中
top=n;
bottom=1;
mid=(top+bottom)/2;
start=clock();
while(flag)
{
printf("top=%d, bottom=%d, mid=%d,a[%d]=%d\n",top,bottom,mid,mid,a[mid]);
if((num>a[top]) || (num<a[bottom]) ) //输入的数num>a[top]
或者num<a[bottom],肯定num不在这个表列中
{
loc=-1;
flag=0;
}
elseif(a[mid]==num) //如果num
等于找到的数
{
loc=mid;
printf("找到数 %6d
的位置%2d\n",num,loc);
break;
}
elseif(a[mid]>num) //若 a[mid]>num,则num
一定在 a[bottom]和a[mid-1]范围之内
{
top=mid-1;
mid=(top+bottom)/2;
}
elseif(a[mid]<num) //若 a[mid]<num,则num
一定在 a[mid+1]和a[top]范围之内
{
bottom=mid+1;
mid=(top+bottom)/2;
}
}
end=clock();
if(loc==-1)
{
printf("%d
这个数在表列中没有找到。\n",num);
}
time=(double)(end-start)/CLOCKS_PER_SEC;
cout<<"这半查找时间"<<time<<endl;
}
一、 实验目的
1.掌握顺序查找、二分法查找的算法。
2.能运用线性表的查找方法解决实际问题。
二、 实验内容。
1.实现一个在无序线性表中查找元素x的算法
2.给出在一个递增有序表A中采用二分查找算法查找值为x的元素的算法。
3.
创建一棵二叉搜索树,给出查找元素x的算法
4.
自行设计测试步骤,并在实验步骤里详细说明要测试操作的边界情况。
5.
比较以上三种算法的时间复杂度。
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long clock_t;
int count=0;
template <class T>
struct BSTNode{//二叉树结点类
T data;
BSTNode<T> *left,*right;
BSTNode():left(NULL),right(NULL){}
BSTNode(const Td,BSTNode<T>*l=NULL,BSTNode<T>*r=NULL):
data(d),left(l),right(r){}
~BSTNode(){}
void setdata(Tt){data=t;}
T getdata(){returndata;}
};
template <class T>
class BST{
public:
BST():root(NULL){}
BST(T vvalue);
~BST(){}
bool search(const T& x)const
{
return(search(x,root)!=NULL)?true:false;
}
bool insert(constT& el){return insert(el,root);}
private:
BSTNode<T>*root;
T Refvalue; //输入停止标志,用于输入
int search(constT x,BSTNode<T>*ptr)const; //递归:搜索
bool insert(const T&el,BSTNode<T> *& ptr);//递归:插入
};
template<class T>
int BST<T>::search(const T x,BSTNode<T>*ptr)const
{
if(ptr==NULL){cout<<"失败"<<endl;return 0;}
BSTNode<T>*temp=ptr;
while(temp!=NULL)
{
if(x==temp->data)
{
cout<<temp->data;
count++;
cout<<"搜索成功"<<endl;
break;
}
else if(x<temp->data)
{
cout<<temp->data;
count++;
cout<<"失败"<<endl;
temp=temp->left;
}
else if(x>temp->data)
{
cout<<temp->data;
count++;
cout<<"搜索成功"<<endl;
temp=temp->right;
}
}
return count;
};
template <class T>
bool BST<T>::insert(const T&el,BSTNode<T>*&ptr){
if(ptr==NULL){
ptr=newBSTNode<T>(el);//创建新结点
if(ptr==NULL){cerr<<"Outof Space"<<endl; exit(1);}
return true;
}
elseif(el<ptr->data) insert(el,ptr->left); //左子树插入
else if(el>ptr->data)insert(el,ptr->right); //右子树插入
else return false; //el已在树中,无需插入
}
template <class T>
BST<T>::BST(T vvalue){//建立二叉树
T t;root=NULL;Refvalue=vvalue;//置空树,refvalue
为一个输入结束标志
cin>>t;
while(t!=Refvalue){
insert(t,root);cin>>t; //插入,再输入数据
}
}
void main(void){
//cout<<"建立二叉搜索树:(-1为结束标志)"<<endl;
BST<int> bst; //停止输入标志为-1
int x,c;
int number;
clock_t start,finish;
double duration;
srand((unsigned)time(0));
for(int i=1;i<=100;i++){
x=rand()%100;
bst.insert(x);
}
cout.width(40);
cout<<"二叉树搜索"<<endl;
cout<<"输入要查找数(100以内)"<<endl;
cin>>number;
start=clock();
c=bst.search(number);
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"共查找"<<count<<"次"<<endl;
cout<<duration<<'s'<<endl;
}
#include <stdio.h>
#include <iostream.h>
#include<math.h>
#include<time.h>
#define N 21
void main(void)
{
int a
;
int i,n,num;
inttop,bottom,mid;
int flag=1; //如果在表列中找到数字,则值为1,否则为0
int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置
clock_tstart,end;
double time;
printf("你想在多少个数中进行折半查找,请输入(1--20):");
scanf("%d",&n);
while(n<1 ||n>20)
{
printf("你输入的数不正确,请重新输入。\n");
printf("你想在多少个数中进行折半查找,请输入(1--20):");
scanf("%d",&n);
}
printf("请你输入一个整数 a[1]:");
scanf("%d",&a[1]);
i=2;
printf("从小到大输入列表\n");
while(i<=n) //输入从小到大的表列
{
printf("请你输入一个整数 a[%d]:",i);
scanf("%d",&a[i]);
if(a[i] >a[i-1])
i++;
else
printf("你输入的数不满足要求,请重新输入。\n");
}
//输出表列
printf("\n输出表列\n");
for(i=1; i<=n;i++)
{
printf("%6d",a[i]);
}
printf("\n");
printf("请你输入要查找的数:\n");
scanf("%d",&num);
flag=1; //假设输入的数在表列中
top=n;
bottom=1;
mid=(top+bottom)/2;
start=clock();
while(flag)
{
printf("top=%d, bottom=%d, mid=%d,a[%d]=%d\n",top,bottom,mid,mid,a[mid]);
if((num>a[top]) || (num<a[bottom]) ) //输入的数num>a[top]
或者num<a[bottom],肯定num不在这个表列中
{
loc=-1;
flag=0;
}
elseif(a[mid]==num) //如果num
等于找到的数
{
loc=mid;
printf("找到数 %6d
的位置%2d\n",num,loc);
break;
}
elseif(a[mid]>num) //若 a[mid]>num,则num
一定在 a[bottom]和a[mid-1]范围之内
{
top=mid-1;
mid=(top+bottom)/2;
}
elseif(a[mid]<num) //若 a[mid]<num,则num
一定在 a[mid+1]和a[top]范围之内
{
bottom=mid+1;
mid=(top+bottom)/2;
}
}
end=clock();
if(loc==-1)
{
printf("%d
这个数在表列中没有找到。\n",num);
}
time=(double)(end-start)/CLOCKS_PER_SEC;
cout<<"这半查找时间"<<time<<endl;
}
相关文章推荐
- (模板题)sdut 3374 数据结构实验之查找二:平衡二叉树(平衡二叉树的建立)
- 数据结构实验之查找四:二分查找
- 数据结构实验之查找六:顺序查找
- 实验三 查找与排序
- 实验三 查找与排序
- 实验图数据集查找总结 Graph DataSet
- 数据结构实验之查找一:二叉排序树
- 数据结构实验之查找五:平方之哈希表
- 暑假集训 8.12 sdutoj3375 数据结构实验之查找三:树的种类统计 (简单字符排序二叉树)
- 数据结构实验之查找一:二叉排序树
- 数据结构上机实验之二分查找
- 数据结构实验之查找五:平方之哈希表
- 数据结构实验之查找二:平衡二叉树
- 数据结构上机实验之二分查找
- 数据结构实验之查找一:二叉排序树
- 数据结构实验之查找五:平方之哈希表
- 【判断两棵二叉排序树是否相同】数据结构实验之查找一:二叉排序树
- 数据结构实验之查找七:线性之哈希表
- 3376 数据结构实验之查找四:二分查找
- 数据结构实验之查找五:平方之哈希表