1、顺序查找
#include <iostream>
/*
线性表
*/
typedef int KeyType;
//元素的数据类型
typedef struct ElemType
{
KeyType key;//关键字(某个数据项类型 数据项的值)
} ElemType;
//线性表的数据类型
#define MAXSIZE 100
typedef struct SSTable
{
ElemType * R;//顺序表0下标位置放弃不用
int len;
} SSTable;
//顺序查找(从后往前)
int Search_Seq(SSTable ST, KeyType key);
//改进后的顺序查找(从后往前)
int Search_Seq_1(SSTable ST, KeyType key);
int main()
{
return 0;
}
//顺序查找(从后往前)
int Search_Seq(SSTable ST, KeyType key)
{
int i;
for (i = ST.len; ST.R[i].key != key; --i)
{
if (i <= 0)
break;//每查找1次都得比较2次
//元素的key值==
//是否查找到表的尽头(防止越界)
}
if (i > 0)
return i;
else
return 0;
}
//改进后的顺序查找(从后往前)
int Search_Seq_1(SSTable ST, KeyType key)
{
int i;
ST.R[0].key = key;//将待查找的key值存到顺序表的表头//哨兵
for (i = ST.len; ST.R[i].key != key; --i);//每查找1次只比较1次 元素的key值==
return i;
}
2、二分查找
#include <iostream>
//线性表
//元素的数据类型
typedef int KeyType;
typedef struct ElemType
{
KeyType key;
} ElemType;
//线性表的数据类型
#define MAXSIZE 100
typedef struct SSTable
{
ElemType * R;
int len;
} SSTable;
//折半查找算法(非递归)
int Search_seq(SSTable ST, KeyType key);
//折半查找算法(递归)
int Search_seq(SSTable ST, KeyType key, int low, int high);
int main()
{
SSTable ST;
ST.R = new ElemType[MAXSIZE];
ST.R[1].key = 5;
ST.R[2].key = 13;
ST.R[3].key = 19;
ST.R[4].key = 21;
ST.R[5].key = 37;
ST.R[6].key = 56;
ST.R[7].key = 64;
ST.R[8].key = 75;
ST.R[9].key = 80;
ST.R[10].key = 88;
ST.R[11].key = 92;
ST.len = 11;
//std::cout << Search_seq(ST, 63) << std::endl;
std::cout << "****test****\n";
std::cout << Search_seq(ST, 21, 1, 11) << std::endl;
std::cout << "****test****\n";
std::cout << "****test****\n";
std::cout << Search_seq(ST, 63, 1, 11) << std::endl;
std::cout << "****test****\n";
return 0;
}
//折半查找算法(非递归)
int Search_seq(SSTable ST, KeyType key)
{
//设置2个变量表示查找区间
int low = 1;
int high = ST.len;
int mid;
while (high >= low)
{
//找到中间位置
mid = (low + high) / 2;
//比较中间位置的key值与给定的key
if (ST.R[mid].key > key)
high = mid - 1;//在左半区查找
else if (ST.R[mid].key < key)
low = mid + 1;//在右半区查找
else
return mid;//找到了
}
return 0;//没找到
}
//折半查找算法(递归)
int Search_seq(SSTable ST, KeyType key, int low, int high)
{
if (high < low)
return 0;//递归的出口--没找到
//找到中间位置
int mid = (low + high) / 2;
//比较中间位置的key值与给定的key
if (ST.R[mid].key > key)
Search_seq(ST, key, low, mid - 1);//在左半区找
else if (ST.R[mid].key < key)
Search_seq(ST, key, mid + 1, high);//在右半区找
else
return mid;//找到了
}
3、二叉排序树
#include <iostream>
typedef int KeyType;
typedef struct ElemType
{
KeyType key;
};
//链式存储
typedef struct BiNode
{
ElemType data;
BiNode * lchild;
BiNode * rchild;
} *BiTree;
//二叉排序树的查找
/*
左子树所有结点值 < root结点
右子树所有结点值 >= root结点
左右子树均是二叉排序树
*/
BiNode * Search(BiTree T, KeyType key);
int main()
{
return 0;
}
/*
树为空----直接返回(没找到)
不空----1、key == T->data ----直接返回T(找到了)
2、key < T->data ----继续查找左子树---也需要返回值(return 查找的结果)
3、key > T->data ----继续查找右子树---也需要返回值(return 查找的结果)
*/
BiNode * Search(BiTree T, KeyType key)
{
if (!T || key == T->data.key)
return T;//递归出口, 空树(没找到), 在根结点找到了
else
{
if (key < T->data.key)
return Search(T->lchild, key);
else
return Search(T->rchild, key);
}
}