请帮我设计一段代码,这段代码的功能是对BST、AVL、B树在运行同一段随机数搜索的时间进行计算和对比大小,全程用C语言进行计算
时间: 2023-06-26 19:09:39 浏览: 182
下面是一个简单的代码示例,用于测试BST、AVL和B树在相同随机数搜索的时间和性能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 二叉搜索树节点结构体
typedef struct BSTNode {
int key;
struct BSTNode *left, *right;
} BSTNode;
// AVL树节点结构体
typedef struct AVLNode {
int key;
int height;
struct AVLNode *left, *right;
} AVLNode;
// B树节点结构体
typedef struct BTreeNode {
int *keys;
struct BTreeNode **child;
int n;
int leaf;
} BTreeNode;
// 创建一个BST节点
BSTNode* newBSTNode(int key) {
BSTNode* node = (BSTNode*)malloc(sizeof(BSTNode));
node->key = key;
node->left = NULL;
node->right = NULL;
return node;
}
// 插入一个节点到BST
BSTNode* BSTInsert(BSTNode* root, int key) {
if (root == NULL) {
return newBSTNode(key);
}
if (key < root->key) {
root->left = BSTInsert(root->left, key);
} else if (key > root->key) {
root->right = BSTInsert(root->right, key);
}
return root;
}
// 在BST中查找一个节点
BSTNode* BSTSearch(BSTNode* root, int key) {
if (root == NULL || root->key == key) {
return root;
}
if (key < root->key) {
return BSTSearch(root->left, key);
} else if (key > root->key) {
return BSTSearch(root->right, key);
}
}
// 计算BST的高度
int BSTHeight(BSTNode* node) {
if (node == NULL) {
return 0;
} else {
int leftHeight = BSTHeight(node->left);
int rightHeight = BSTHeight(node->right);
if (leftHeight > rightHeight) {
return leftHeight + 1;
} else {
return rightHeight + 1;
}
}
}
// 创建一个AVL节点
AVLNode* newAVLNode(int key) {
AVLNode* node = (AVLNode*)malloc(sizeof(AVLNode));
node->key = key;
node->height = 1;
node->left = NULL;
node->right = NULL;
return node;
}
// 计算AVL节点的高度
int AVLHeight(AVLNode* node) {
if (node == NULL) {
return 0;
} else {
return node->height;
}
}
// 计算AVL节点的平衡因子
int AVLBalanceFactor(AVLNode* node) {
if (node == NULL) {
return 0;
} else {
return AVLHeight(node->left) - AVLHeight(node->right);
}
}
// 更新AVL节点的高度
void AVLUpdateHeight(AVLNode* node) {
int leftHeight = AVLHeight(node->left);
int rightHeight = AVLHeight(node->right);
node->height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// AVL左旋转
AVLNode* AVLRotateLeft(AVLNode* node) {
AVLNode* right = node->right;
AVLNode* leftOfRight = right->left;
right->left = node;
node->right = leftOfRight;
AVLUpdateHeight(node);
AVLUpdateHeight(right);
return right;
}
// AVL右旋转
AVLNode* AVLRotateRight(AVLNode* node) {
AVLNode* left = node->left;
AVLNode* rightOfLeft = left->right;
left->right = node;
node->left = rightOfLeft;
AVLUpdateHeight(node);
AVLUpdateHeight(left);
return left;
}
// AVL插入操作
AVLNode* AVLInsert(AVLNode* node, int key) {
if (node == NULL) {
return newAVLNode(key);
}
if (key < node->key) {
node->left = AVLInsert(node->left, key);
} else if (key > node->key) {
node->right = AVLInsert(node->right, key);
} else {
return node;
}
AVLUpdateHeight(node);
int balanceFactor = AVLBalanceFactor(node);
if (balanceFactor > 1) {
if (AVLBalanceFactor(node->left) < 0) {
node->left = AVLRotateLeft(node->left);
}
return AVLRotateRight(node);
} else if (balanceFactor < -1) {
if (AVLBalanceFactor(node->right) > 0) {
node->right = AVLRotateRight(node->right);
}
return AVLRotateLeft(node);
}
return node;
}
// 在AVL中查找一个节点
AVLNode* AVLSearch(AVLNode* node, int key) {
if (node == NULL || node->key == key) {
return node;
}
if (key < node->key) {
return AVLSearch(node->left, key);
} else if (key > node->key) {
return AVLSearch(node->right, key);
}
}
// 创建一个B树节点
BTreeNode* newBTreeNode(int t, int leaf) {
BTreeNode* node = (BTreeNode*)malloc(sizeof(BTreeNode));
node->keys = (int*)malloc(sizeof(int) * (2 * t - 1));
node->child = (BTreeNode**)malloc(sizeof(BTreeNode*) * (2 * t));
node->n = 0;
node->leaf = leaf;
return node;
}
// B树分裂操作
void BTreeSplitChild(BTreeNode* parent, int i, BTreeNode* child) {
int t = (parent->n + 1) / 2;
BTreeNode* newChild = newBTreeNode(child->leaf, t);
newChild->n = t - 1;
for (int j = 0; j < t - 1; j++) {
newChild->keys[j] = child->keys[j + t];
}
if (!child->leaf) {
for (int j = 0; j < t; j++) {
newChild->child[j] = child->child[j + t];
}
}
child->n = t - 1;
for (int j = parent->n; j >= i + 1; j--) {
parent->child[j + 1] = parent->child[j];
}
parent->child[i + 1] = newChild;
for (int j = parent->n - 1; j >= i; j--) {
parent->keys[j + 1] = parent->keys[j];
}
parent->keys[i] = child->keys[t - 1];
parent->n += 1;
}
// B树插入操作
BTreeNode* BTreeInsertNonFull(BTreeNode* node, int key, int t) {
int i = node->n - 1;
if (node->leaf) {
while (i >= 0 && key < node->keys[i]) {
node->keys[i + 1] = node->keys[i];
i--;
}
node->keys[i + 1] = key;
node->n += 1;
} else {
while (i >= 0 && key < node->keys[i]) {
i--;
}
if (node->child[i + 1]->n == 2 * t - 1) {
BTreeSplitChild(node, i + 1, node->child[i + 1]);
if (key > node->keys[i + 1]) {
i++;
}
}
node->child[i + 1] = BTreeInsertNonFull(node->child[i + 1], key, t);
}
return node;
}
// 在B树中查找一个节点
BTreeNode* BTreeSearch(BTreeNode* node, int key) {
int i = 0;
while (i < node->n && key > node->keys[i]) {
i++;
}
if (i < node->n && key == node->keys[i]) {
return node;
}
if (node->leaf) {
return NULL;
}
return BTreeSearch(node->child[i], key);
}
// 生成一个随机数
int random(int min, int max) {
return rand() % (max - min + 1) + min;
}
// 打印BST的信息
void printBSTInfo(BSTNode* root, clock_t start, clock_t end) {
printf("BST height: %d\n", BSTHeight(root));
printf("BST search time: %ldms\n", end - start);
}
// 打印AVL的信息
void printAVLInfo(AVLNode* root, clock_t start, clock_t end) {
printf("AVL height: %d\n", root->height);
printf("AVL search time: %ldms\n", end - start);
}
// 打印B树的信息
void printBTreeInfo(BTreeNode* root, clock_t start, clock_t end) {
printf("B-tree height: %d\n", root->leaf ? 0 : root->child[0]->n + 1);
printf("B-tree search time: %ldms\n", end - start);
}
// 测试BST
void testBST(int n) {
BSTNode* root = NULL;
for (int i = 0; i < n; i++) {
int key = random(1, n * 10);
root = BSTInsert(root, key);
}
int searchKey = random(1, n * 10);
clock_t start = clock();
BSTNode* result = BSTSearch(root, searchKey);
clock_t end = clock();
if (result != NULL) {
printf("BST search key %d: found\n", searchKey);
} else {
printf("BST search key %d: not found\n", searchKey);
}
printBSTInfo(root, start, end);
}
// 测试AVL
void testAVL(int n) {
AVLNode* root = NULL;
for (int i = 0; i < n; i++) {
int key = random(1, n * 10);
root = AVLInsert(root, key);
}
int searchKey = random(1, n * 10);
clock_t start = clock();
AVLNode* result = AVLSearch(root, searchKey);
clock_t end = clock();
if (result != NULL) {
printf("AVL search key %d: found\n", searchKey);
} else {
printf("AVL search key %d: not found\n", searchKey);
}
printAVLInfo(root, start, end);
}
// 测试B树
void testBTree(int n) {
int t = 2;
BTreeNode* root = newBTreeNode(t, 1);
for (int i = 0; i < n; i++) {
int key = random(1, n * 10);
BTreeInsertNonFull(root, key, t);
}
int searchKey = random(1, n * 10);
clock_t start = clock();
BTreeNode* result = BTreeSearch(root, searchKey);
clock_t end = clock();
if (result != NULL) {
printf("B-tree search key %d: found\n", searchKey);
} else {
printf("B-tree search key %d: not found\n", searchKey);
}
printBTreeInfo(root, start, end);
}
int main() {
srand((unsigned int)time(NULL));
int n = 10000;
testBST(n);
testAVL(n);
testBTree(n);
return 0;
}
```
在这个代码中,我们使用了三种不同的数据结构:BST、AVL和B树。它们都可以用于搜索和查找操作,但是它们的性能和特点各不相同。
在测试函数中,我们首先生成了一个包含n个随机数的数据集,然后将这些随机数插入到每个数据结构中。接下来,我们随机选择一个数字,对每个数据结构进行搜索,并记录搜索时间。最后,我们打印出每个数据结构的高度和搜索时间,以便进行比较。
这个代码示例只是一个简单的实现,您可以根据需要进行修改和扩展。
阅读全文