用c++编写代码,要求:1.定义长度为n的查找表,m个待查找数据,定义二叉树排序树的结点结构。2.初始化随机数发生器,产生n个随机数构成的查找表并用简单选择排序。3.创建顺序查找、折半查找、创建二叉排序树、二叉排序树查找,取得当前系统时间函数。4.调用顺序查找、折半查找、二叉排序树查找函数,输出在长度为n的查找表中,查找m个数据耗费的总时间。处理要求1.当待查找数据为m=1000和n=10000(查找表)时顺序查找的时间为: 毫秒折半查找的时间为: 毫秒二叉排序树查找的时间为: 毫秒2.当待查找数据为m=2000和n=10000(查找表)时顺序查找的时间为: 毫秒折半查找的时间为: 毫秒二叉排序树查找的时间为: 毫秒3.当待查找数据为m=5000和n=10000(查找表)时顺序查找的时间为: 毫秒折半查找的时间为: 毫秒二叉排序树查找的时间为: 毫秒
时间: 2023-12-03 18:46:26 浏览: 207
抱歉,我目前无法生成代码并进行运行测试。但是我可以给你提供一个思路:
1. 定义一个结构体表示二叉树排序树的结点,包括数据和左右子树指针。
2. 定义一个长度为n的数组作为查找表,使用随机数生成器填充数组并进行简单选择排序。
3. 创建顺序查找、折半查找、二叉排序树、二叉排序树查找函数,使用系统时间函数计算每个函数的执行时间,并输出结果。
4. 在主函数中调用上述函数,分别使用m=1000、m=2000和m=5000进行测试,输出结果。
以下是部分代码示例:
```c++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// 定义二叉排序树结点
struct BSTNode {
int data;
BSTNode* left;
BSTNode* right;
BSTNode(int val) : data(val), left(nullptr), right(nullptr) {}
};
// 初始化查找表
void init(int arr[], int n) {
srand(time(nullptr));
for (int i = 0; i < n; ++i) {
arr[i] = rand() % 10000;
}
// 简单选择排序
for (int i = 0; i < n - 1; ++i) {
int min_index = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
if (min_index != i) {
swap(arr[i], arr[min_index]);
}
}
}
// 顺序查找
int seqSearch(int arr[], int n, int key) {
for (int i = 0; i < n; ++i) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
// 折半查找
int binSearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 创建二叉排序树
void createBST(BSTNode*& root, int arr[], int n) {
root = nullptr;
for (int i = 0; i < n; ++i) {
BSTNode* node = new BSTNode(arr[i]);
if (root == nullptr) {
root = node;
} else {
BSTNode* cur = root;
while (true) {
if (node->data < cur->data) {
if (cur->left == nullptr) {
cur->left = node;
break;
} else {
cur = cur->left;
}
} else {
if (cur->right == nullptr) {
cur->right = node;
break;
} else {
cur = cur->right;
}
}
}
}
}
}
// 二叉排序树查找
bool BSTSearch(BSTNode* root, int key) {
if (root == nullptr) {
return false;
}
if (root->data == key) {
return true;
} else if (root->data < key) {
return BSTSearch(root->right, key);
} else {
return BSTSearch(root->left, key);
}
}
int main() {
const int n = 10000;
int arr[n];
init(arr, n);
const int m1 = 1000;
const int m2 = 2000;
const int m3 = 5000;
int key1[m1], key2[m2], key3[m3];
srand(time(nullptr));
for (int i = 0; i < m1; ++i) {
key1[i] = rand() % 10000;
}
for (int i = 0; i < m2; ++i) {
key2[i] = rand() % 10000;
}
for (int i = 0; i < m3; ++i) {
key3[i] = rand() % 10000;
}
BSTNode* root = nullptr;
createBST(root, arr, n);
clock_t start, end;
double total_time;
// m=1000
start = clock();
for (int i = 0; i < m1; ++i) {
seqSearch(arr, n, key1[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "SeqSearch time for m=1000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m1; ++i) {
binSearch(arr, 0, n - 1, key1[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BinSearch time for m=1000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m1; ++i) {
BSTSearch(root, key1[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BSTSearch time for m=1000: " << total_time << "ms" << endl;
// m=2000
start = clock();
for (int i = 0; i < m2; ++i) {
seqSearch(arr, n, key2[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "SeqSearch time for m=2000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m2; ++i) {
binSearch(arr, 0, n - 1, key2[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BinSearch time for m=2000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m2; ++i) {
BSTSearch(root, key2[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BSTSearch time for m=2000: " << total_time << "ms" << endl;
// m=5000
start = clock();
for (int i = 0; i < m3; ++i) {
seqSearch(arr, n, key3[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "SeqSearch time for m=5000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m3; ++i) {
binSearch(arr, 0, n - 1, key3[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BinSearch time for m=5000: " << total_time << "ms" << endl;
start = clock();
for (int i = 0; i < m3; ++i) {
BSTSearch(root, key3[i]);
}
end = clock();
total_time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000;
cout << "BSTSearch time for m=5000: " << total_time << "ms" << endl;
return 0;
}
```
阅读全文