#include<stdio.h>
#include <time.h>
#define MAX 10
typedef int KeyType;
typedef int InfoType;
typedef bool ;
typedef ElenType;
typedef struct//顺序表
{
int R[MAX];
KeyType key;
InfoType data[MAX];//存放元素
int length;//线性表长度
}RecType;
typedef struct node//二叉树
{
RecType key;
InfoType data;
struct node* lchild, * rchild;
}BSTNode;
void initdata(int a[], int n)
{
int i; int number = 0;
/* use the time to seed the random number generator */
srand((unsigned)time(NULL));
for (i = 1; i <= n; i++)
{
number = rand() % 10000; //产生10000以内的随机数
a[i] = number;
}
}
/*基础实验项目:
1. 实现线性表的顺序查找算法
2. 实现线性表的二分查找算法
3. 实现树表的二叉排序树查找
拓展实验项目:
实现散列表的散列查找方法。*/
int CreateList(RecType*L, ElenType a[],int n)//创建线性表
{
int i = 0, k = 0;
L = (RecType*)malloc(sizeof(RecType));
while (i > n)
{
L->data[i] = a[i];
k++; i++;
}
L->length = k;
}
int SeqSearch(RecType R[], int n, KeyType k)//实现线性表的顺序查找算法
{
int i = 0;
while (i < n && R[i].key != k)
i++;
if (i >= n)
return 0;
else
return i + 1;
}
int BinSearch(RecType L, int n, KeyType k)//实现线性表的二分查找算法
{
RecType *L = (RecType*)malloc(sizeof(RecType));
int low = 0, high = n - 1, mid;
while (low <= high)
{
mid = (low + high) / 2;
if (k == L.R[mid].key)
return mid + 1;
if (k <=L.R[mid].key)
high = mid - 1;
else
low = mid + 1;
}
return 0;
}
/*bool InsertBST(BSTNode* bt, KeyType k)
{
if(bt==NULL)
}*/
BSTNode* CreateBST(KeyType A[], int n)//创建二叉排序树
{
BSTNode* bt = NULL;
int i = 0;
while (i < n)
{
InsertBST(bt, A[i]);
i++;
}
return bt;
}
/*BSTNode* SearchBST(BSTNode* bt, KeyType k)//二叉排序树的查找
{
if (bt == NULL || bt->key == k)
return bt;
if (k < bt->key)
return SearchBST(bt->lchild, k);
else
return SearchBST(bt->rchild, k);
}*/
void Show(int result, int key) {
if (result == 0)
printf("\n未找到%d\n", key);
else
printf("\n找到%d,位置为%d\n", key, result);
return;
}
BSTNode* BSTSearch(BSTNode* bt, KeyType k)//二叉树查找排序
{
BSTNode* p = bt;
while (p != NULL)
{
if (p->key == k)
return p;
else if (k < p->key)
p = p->lchild;
else
p = p->rchild;
}
}
void DispBST(BSTNode* bt)
{
if (bt != NULL)
{
printf("%d", bt->key);
if (bt->lchild != NULL || bt->rchild != NULL)
{
printf("(");
DispBST(bt->lchild);
if (bt->rchild != NULL)
printf(",");
DispBST(bt->rchild);
printf(")");
}
}
}
/*void DispBSt(BSTNode* bt)
{
if (bt != NULL)
{
printf()
}
}*/
void main()
{
int a[MAX]; KeyType k; int n=0,m=0; RecType R[MAX];
RecType* L = (RecType*)malloc(sizeof(RecType));
printf("**创建顺序表**\n");
printf("请输入顺序表元素,用空格隔开:\n");
for(int i=0;i<MAX;i++)
{
initdata(a[i], n);
}
printf("\n-----------顺序查找-----------\n");
printf("\n【请输入要查找的关键字】\n>>>");
getchar();
scanf("%d", &k);
int result;
result = SeqSearch(R[],n,k);
printf("找到了:%d", result);
Show(result, k);
printf("\n-------------------------------\n");
printf("\n-----------折半查找(非递归)-----------\n");
printf("\n【请输入要查找的关键字】\n>>>");
getchar();
scanf("%d", &k);
result = BinSearch (R[],m, k);
Show(result, k);
printf("\n---------------------------------------\n");
printf("\n-----------二叉排序树查找-----------\n");
printf("\n【请输入要查找的关键字】\n>>>");
getchar();
scanf("%d", &k);
BSTNode* bt;
result = BSTSearch(bt, k);
Show(result, k);
printf("\n---------------------------------------\n");
}