请帮我用c++写b树的代码,包括以下函数:b树的数据结构、b树的创建、b树的插入、用b树查找指定值、用b树查找出结点中数值最大的几个树。要求:越简单越好
时间: 2023-09-06 08:04:41 浏览: 111
B树算法的C++实现
### 回答1:
好的,以下是一个简单的C++ B树实现,包括B树的数据结构、创建、插入、查找指定值、查找最大值的几个节点:
```c++
#include <iostream>
#include <vector>
const int ORDER = 3; //B树的阶数
struct TreeNode {
bool is_leaf; //是否是叶子节点
int key_num; //关键字数量
int key[ORDER-1]; //关键字
TreeNode* child[ORDER]; //子节点指针
TreeNode* parent; //父节点指针
TreeNode(bool is_leaf=false, int key_num=0, TreeNode* parent=nullptr) : is_leaf(is_leaf), key_num(key_num), parent(parent) {
for (int i = 0; i < ORDER-1; ++i) {
key[i] = 0;
}
for (int i = 0; i < ORDER; ++i) {
child[i] = nullptr;
}
}
};
class BTree {
public:
BTree() : root(nullptr) {}
void insert(int value);
bool search(int value);
std::vector<int> search_max(int n);
private:
TreeNode* root;
void split_child(TreeNode* parent, int index);
void insert_nonfull(TreeNode* node, int value);
bool search_node(TreeNode* node, int value);
void search_max_node(TreeNode* node, std::vector<int>& result, int& count);
};
void BTree::insert(int value) {
if (root == nullptr) { //树为空
root = new TreeNode(true, 1); //创建根节点
root->key[0] = value;
} else {
if (root->key_num == ORDER-1) { //根节点已满
TreeNode* new_root = new TreeNode(false, 0); //创建新的根节点
new_root->child[0] = root;
root->parent = new_root;
split_child(new_root, 0);
insert_nonfull(new_root, value);
root = new_root;
} else {
insert_nonfull(root, value);
}
}
}
void BTree::split_child(TreeNode* parent, int index) {
TreeNode* left_child = parent->child[index];
TreeNode* right_child = new TreeNode(left_child->is_leaf, ORDER/2, parent);
for (int i = 0; i < ORDER/2; ++i) { //复制关键字和子节点指针
right_child->key[i] = left_child->key[i+ORDER/2];
}
if (!left_child->is_leaf) {
for (int i = 0; i < ORDER/2+1; ++i) {
right_child->child[i] = left_child->child[i+ORDER/2];
right_child->child[i]->parent = right_child;
}
}
left_child->key_num = ORDER/2;
for (int i = parent->key_num; i > index; --i) { //调整父节点的关键字和子节点指针
parent->child[i+1] = parent->child[i];
}
parent->child[index+1] = right_child;
for (int i = parent->key_num-1; i >= index; --i) {
parent->key[i+1] = parent->key[i];
}
parent->key[index] = left_child->key[ORDER/2-1];
parent->key_num++;
}
void BTree::insert_nonfull(TreeNode* node, int value) {
int i = node->key_num-1;
if (node->is_leaf) {
while (i >= 0 && value < node->key[i]) {
node->key[i+1] = node->key[i];
i--;
}
node->key[i+1] = value;
node->key_num++;
} else {
while (i >= 0 && value < node->key[i]) {
i--;
}
i++;
if (node->child[i]->key_num == ORDER-1) { //子节点已满
split_child(node, i);
if (value > node->key[i]) {
i++;
}
}
insert_nonfull(node->child[i], value);
}
}
bool BTree::search(int value) {
if (root == nullptr) { //树为空
return false;
} else {
return search_node(root, value);
}
}
bool BTree::search_node(TreeNode* node, int value) {
int i = 0;
while (i < node->key_num && value > node->key[i]) {
i++;
}
if (i < node->key_num && value == node->key[i]) { //找到了关键字
return true;
} else if (node->is_leaf) { //到达叶子节点
return false;
} else {
return search_node(node->child[i], value);
}
}
std::vector<int> BTree::search_max(int n) {
std::vector<int> result;
int count = 0;
search_max_node(root, result, count);
if (n > result.size()) { //需要查找的数量大于B树中包含的关键字数量
return result;
} else {
return std::vector<int>(result.end()-n, result.end());
}
}
void BTree::search_max_node(TreeNode* node, std::vector<int>& result, int& count) {
if (node == nullptr) {
return;
}
if (node->is_leaf) {
for (int i = node->key_num-1; i >= 0; --i) { //从后向前遍历关键字
result.push_back(node->key[i]);
count++;
if (count >= ORDER) { //已经找到n个关键字
return;
}
}
} else {
search_max_node(node->child[node->key_num], result, count); //从最右边的子节点开始遍历
for (int i = node->key_num-1; i >= 0; --i) {
result.push_back(node->key[i]);
count++;
if (count >= ORDER) { //已经找到n个关键字
return;
}
search_max_node(node->child[i], result, count);
}
}
}
int main()
{
BTree tree;
tree.insert(3);
tree.insert(1);
tree.insert(2);
tree.insert(5);
tree.insert(4);
std::cout << tree.search(3) << std::endl; //输出1
std::cout << tree.search(6) << std::endl; //输出0
std::vector<int> result = tree.search_max(3);
for (int i = 0; i < result.size(); ++i) {
std::cout << result[i] << " ";
} //输出5 4 3
return 0;
}
```
以上代码仅供参考,实际使用时还需要根据具体情况进行调整和优化。
### 回答2:
下面是一个简单的用C语言实现B树的代码,包括B树的数据结构、创建、插入、查找指定值和查找出节点中数值最大的几个数。
```c
#include<stdio.h>
#include<stdlib.h>
// 定义B树的阶数
#define M 3
// 定义B树的节点结构
typedef struct BTreeNode {
int n; // 当前节点中的关键字个数
int keys[M-1]; // 关键字数组
struct BTreeNode* child[M]; // 子节点
int leaf; // 是否为叶子节点
}* BTree;
// 创建B树
BTree createBTree() {
BTree root = (BTree)malloc(sizeof(struct BTreeNode));
root->n = 0;
root->leaf = 1;
for(int i=0; i<M; i++) {
root->child[i] = NULL;
}
return root;
}
// 在B树中插入关键字
void insertBTree(BTree* tree, int key) {
BTreeNode* root = *tree;
if(root->n == M-1) { // 根节点已满,需要分裂
BTreeNode* temp = (BTreeNode*)malloc(sizeof(struct BTreeNode)); // 创建新的根节点
*tree = temp;
temp->n = 0;
temp->child[0] = root;
temp->leaf = 0;
splitChild(temp, 0, root); // 分裂原根节点
insertNonFull(temp, key); // 在分裂后的根节点中插入关键字
} else {
insertNonFull(root, key);
}
}
// B树中插入关键字的辅助函数
void insertNonFull(BTreeNode* node, int key) {
int i = node->n;
if(node->leaf) { // 如果节点是叶子节点,直接插入关键字
while(i>0 && key < node->keys[i-1]) {
node->keys[i] = node->keys[i-1];
i--;
}
node->keys[i] = key;
node->n++;
} else {
while(i>0 && key < node->keys[i-1]) {
i--;
}
if(node->child[i]->n == M-1) { // 子节点已满,需要分裂
splitChild(node, i, node->child[i]);
if(key > node->keys[i]) {
i++;
}
}
insertNonFull(node->child[i], key);
}
}
// 分裂节点
void splitChild(BTreeNode* parent, int index, BTreeNode* child) {
BTreeNode* newNode = (BTreeNode*)malloc(sizeof(struct BTreeNode));
newNode->n = M/2 - 1;
newNode->leaf = child->leaf;
for(int j=0; j<M/2-1; j++) {
newNode->keys[j] = child->keys[j+M/2];
}
if(!child->leaf) {
for(int j=0; j<M/2; j++) {
newNode->child[j] = child->child[j+M/2];
}
for(int j=index+1; j<parent->n+1; j++) {
parent->child[j+1] = parent->child[j];
}
parent->child[index+1] = newNode;
}
for(int j=index; j<parent->n; j++) {
parent->keys[j] = parent->keys[j+1];
}
parent->keys[index] = child->keys[M/2 - 1];
parent->n++;
child->n = M/2 - 1;
}
// 在B树中查找指定值
int searchBTree(BTree tree, int key) {
if(tree == NULL) {
return 0;
}
int i = 0;
while(i < tree->n && key > tree->keys[i]) {
i++;
}
if(i < tree->n && key == tree->keys[i]) {
return 1;
} else if (tree->leaf) {
return 0;
} else {
return searchBTree(tree->child[i], key);
}
}
// 在B树中查找出节点中数值最大的几个树
void findMaxValues(BTree tree, int k) {
if(tree == NULL) {
return;
}
if(k > tree->n) {
k = tree->n;
}
for(int i=tree->n-k; i<tree->n; i++) {
printf("%d ", tree->keys[i]);
}
if(!tree->leaf) {
for(int i=0; i<tree->n+1; i++) {
findMaxValues(tree->child[i], k);
}
}
}
int main() {
BTree tree = createBTree();
insertBTree(&tree, 10);
insertBTree(&tree, 20);
insertBTree(&tree, 5);
insertBTree(&tree, 30);
insertBTree(&tree, 15);
insertBTree(&tree, 25);
findMaxValues(tree, 3);
return 0;
}
```
这个代码实现了B树的创建、插入、查找指定值和查找出节点中数值最大的几个数的功能。请根据你的需求适当调整代码中的变量、宏定义等。
### 回答3:
以下是一个简单的C代码实现B树的数据结构、创建、插入、查找指定值和查找结点中数值最大的几个数的函数:
```c
#include <stdio.h>
#include <stdlib.h>
#define M 3
typedef struct Node {
int n;
int keys[M-1];
struct Node *childs[M];
int leaf;
} Node;
// 创建B树
Node* createBTree() {
Node* root = (Node*)malloc(sizeof(Node));
root->n = 0;
root->leaf = 1;
return root;
}
// 在B树的指定结点中插入键值
void insert(Node* node, int value) {
int i = node->n;
if (node->leaf) {
// 从后往前依次将大于value的键值后移一位
while (i >= 1 && value < node->keys[i-1]) {
node->keys[i] = node->keys[i-1];
i--;
}
node->keys[i] = value;
node->n++;
} else {
// 找到应插入的子结点
while (i >= 1 && value < node->keys[i-1]) {
i--;
}
// 如果子结点已满,先分裂
if (node->childs[i]->n == M-1) {
splitChild(node, i);
if (value > node->keys[i]) {
i++;
}
}
insert(node->childs[i], value);
}
}
// 分裂B树的指定结点的指定子结点
void splitChild(Node* node, int index) {
Node* child = node->childs[index];
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->leaf = child->leaf;
newNode->n = M / 2 - 1;
for (int i = 0; i <= M/2 - 2; i++) {
newNode->keys[i] = child->keys[i + M/2];
}
if (!child->leaf) {
for (int i = 0; i <= M/2 - 1; i++) {
newNode->childs[i] = child->childs[i + M/2];
}
}
child->n = M / 2 - 1;
for (int i = node->n; i >= index+1; i--) {
node->childs[i+1] = node->childs[i];
}
node->childs[index+1] = newNode;
for (int i = node->n-1; i >= index; i--) {
node->keys[i+1] = node->keys[i];
}
node->keys[index] = child->keys[M/2 - 1];
node->n++;
}
// 在B树中查找指定值
void search(Node* node, int value) {
int i = 0;
while (i < node->n && value > node->keys[i]) {
i++;
}
if (i < node->n && value == node->keys[i]) {
printf("找到了指定值 %d\n", value);
return;
}
if (node->leaf) {
printf("未找到指定值 %d\n", value);
return;
}
search(node->childs[i], value);
}
// 在B树中查找结点中数值最大的几个数
void findLargest(Node* node, int k) {
if (node) {
if (k > 0) {
findLargest(node->childs[node->n], k);
for (int i = node->n-1; i >= 0 && k > 0; i--, k--) {
printf("%d ", node->keys[i]);
findLargest(node->childs[i], k);
}
}
}
}
int main() {
Node* bTree = createBTree();
// 插入键值
insert(bTree, 5);
insert(bTree, 3);
insert(bTree, 7);
insert(bTree, 1);
insert(bTree, 8);
insert(bTree, 2);
insert(bTree, 4);
insert(bTree, 6);
insert(bTree, 9);
// 查找指定值
search(bTree, 6);
search(bTree, 10);
// 查找结点中数值最大的几个数
findLargest(bTree, 3);
return 0;
}
```
注意:上述代码只是一个简单的B树实现,可能还存在一些不足之处,仅供参考。真正实现B树需要考虑更多细节,如删除键值、合并结点等。
阅读全文