用C++写一个b树源码
时间: 2023-08-31 09:24:15 浏览: 164
### 回答1:
当然可以,以下是一个用C语言写的二叉树源代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data) {
struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void printInorder(struct node* node) {
if (node == NULL) {
return;
}
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
int main() {
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Inorder traversal of binary tree is: ");
printInorder(root);
printf("\n");
return 0;
}
```
这是一个基本的二叉树实现,包括创建节点、插入节点和中序遍历等功能。你可以根据需要进行修改和扩展。
### 回答2:
B树是一种自平衡的搜索树,它具有多个子节点和多个键值对。每个节点包含一个键值对的数组,并按照键值的顺序排列。因此,B树可以高效地进行插入、删除和查找操作。
以下是用C语言编写的一个简单的B树源码:
```
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define M 4 // B树的最小度数
typedef struct btreeNode {
int *keys; // 键值对的数组
struct btreeNode **children; // 子节点的指针数组
int numKeys; // 当前节点的键值对数量
int isLeaf; // 判断是否为叶子节点
} BTreeNode;
// B树的查找操作
BTreeNode* search(BTreeNode *root, int key) {
int i = 0;
while (i < root->numKeys && key > root->keys[i])
i++;
if (i < root->numKeys && key == root->keys[i])
return root;
if (root->isLeaf == TRUE)
return NULL;
return search(root->children[i], key);
}
// B树的插入操作
void insert(BTreeNode **rootRef, int key) {
BTreeNode *root = *rootRef;
if (root == NULL) {
root = (BTreeNode*) malloc(sizeof(BTreeNode));
root->keys = (int*) malloc((M-1) * sizeof(int));
root->children = (BTreeNode**) malloc(M * sizeof(BTreeNode*));
root->numKeys = 0;
root->isLeaf = TRUE;
*rootRef = root;
}
if (root->numKeys == 2 * M - 1) {
BTreeNode *newRoot = (BTreeNode*) malloc(sizeof(BTreeNode));
newRoot->keys = (int*) malloc((M-1) * sizeof(int));
newRoot->children = (BTreeNode**) malloc(M * sizeof(BTreeNode*));
newRoot->numKeys = 0;
newRoot->isLeaf = FALSE;
newRoot->children[0] = root;
*rootRef = newRoot;
splitChild(newRoot, 0);
}
insertNonFull(root, key);
}
// 分裂节点
void splitChild(BTreeNode *x, int i) {
BTreeNode *y = x->children[i];
BTreeNode *z = (BTreeNode*) malloc(sizeof(BTreeNode));
z->keys = (int*) malloc((M-1) * sizeof(int));
z->children = (BTreeNode**) malloc(M * sizeof(BTreeNode*));
z->numKeys = 0;
z->isLeaf = y->isLeaf;
for (int j = 0; j < M-1; j++) {
z->keys[j] = y->keys[j+M];
}
if (y->isLeaf == FALSE) {
for (int j = 0; j < M; j++) {
z->children[j] = y->children[j+M];
}
}
y->numKeys = M - 1;
for (int j = x->numKeys; j >= i+1; j--) {
x->children[j+1] = x->children[j];
}
x->children[i+1] = z;
for (int j = x->numKeys-1; j >= i; j--) {
x->keys[j+1] = x->keys[j];
}
x->keys[i] = y->keys[M-1];
x->numKeys++;
}
// 在非满节点中插入键值对
void insertNonFull(BTreeNode *node, int key) {
int i = node->numKeys - 1;
if (node->isLeaf == TRUE) {
while (i >= 0 && key < node->keys[i]) {
node->keys[i+1] = node->keys[i];
i--;
}
node->keys[i+1] = key;
node->numKeys++;
} else {
while (i >= 0 && key < node->keys[i]) {
i--;
}
i++;
if (node->children[i]->numKeys == 2*M-1) {
splitChild(node, i);
if (key > node->keys[i]) {
i++;
}
}
insertNonFull(node->children[i], key);
}
}
int main() {
BTreeNode *root = NULL;
insert(&root, 1);
insert(&root, 2);
insert(&root, 3);
insert(&root, 4);
BTreeNode *result = search(root, 3);
if (result != NULL) {
printf("找到了键值 %d\n", result->keys[0]);
} else {
printf("未找到该键值\n");
}
return 0;
}
```
上述代码是一个用C语言编写的简单B树的实现。通过该代码,可以实现B树的插入和查找功能。该源代码可以作为B树的基础,可以根据需要进行扩展和优化。
### 回答3:
B树,也称为B-平衡树,是一种经典的数据结构,广泛应用于数据库和文件系统中,用于高效地存储和检索大量数据。
以下是用C语言编写的简化版B树的源码:
```c
#include <stdio.h>
#include <stdlib.h>
// B树的每个节点
typedef struct BTreeNode {
int *keys; // 节点的关键字数组
struct BTreeNode **child; // 子节点指针数组
int numKeys; // 节点中关键字的数量
int leaf; // 是否为叶子节点
} BTreeNode;
// 创建一个新的节点
BTreeNode *createNode(int leaf) {
BTreeNode *newNode = (BTreeNode *)malloc(sizeof(BTreeNode));
newNode->keys = (int *)malloc(sizeof(int) * 3); // 每个节点最多有2个关键字
newNode->child = (BTreeNode **)malloc(sizeof(BTreeNode *) * 4); // 每个节点最多有3个子节点
newNode->numKeys = 0;
newNode->leaf = leaf;
return newNode;
}
// 在已经满的节点中插入一个键
void insertNonFull(BTreeNode *node, int key) {
int i = node->numKeys - 1;
if (node->leaf) {
while (i >= 0 && node->keys[i] > key) {
node->keys[i + 1] = node->keys[i];
i--;
}
node->keys[i + 1] = key;
node->numKeys++;
} else {
while (i >= 0 && node->keys[i] > key) {
i--;
}
if (node->child[i + 1]->numKeys == 2) {
splitChild(node, i + 1);
if (node->keys[i + 1] < key) {
i++;
}
}
insertNonFull(node->child[i + 1], key);
}
}
// 分割满节点的子节点
void splitChild(BTreeNode *parent, int childIndex) {
BTreeNode *child = parent->child[childIndex];
BTreeNode *newChild = createNode(child->leaf);
newChild->numKeys = 1;
// 将原满节点的后一半键移入新节点
newChild->keys[0] = child->keys[2];
// 调整原满节点的关键字数量和子节点指针
child->numKeys = 1;
// 将新节点插入到原满节点的右边
int i = parent->numKeys;
while (i >= childIndex + 1) {
parent->child[i + 1] = parent->child[i];
parent->keys[i] = parent->keys[i - 1];
i--;
}
parent->child[childIndex + 1] = newChild;
parent->keys[childIndex] = child->keys[1];
parent->numKeys++;
}
// 插入一个键
void insertKey(BTreeNode **root, int key) {
BTreeNode *node = *root;
if (node->numKeys == 2) {
BTreeNode *newNode = createNode(0);
*root = newNode;
newNode->child[0] = node;
splitChild(newNode, 0);
insertNonFull(newNode, key);
} else {
insertNonFull(node, key);
}
}
// 打印B树的关键字
void printBTree(BTreeNode *node) {
int i;
for (i = 0; i < node->numKeys; i++) {
if (node->leaf == 0) {
printBTree(node->child[i]);
}
printf("%d ", node->keys[i]);
}
if (node->leaf == 0) {
printBTree(node->child[i]);
}
}
int main() {
BTreeNode *root = createNode(1); // 创建根节点
insertKey(&root, 10);
insertKey(&root, 20);
insertKey(&root, 5);
insertKey(&root, 30);
insertKey(&root, 25);
printBTree(root);
return 0;
}
```
这段源码实现了一个简化版的B树,可以通过插入键的操作来构建B树,并通过printBTree函数来打印B树的关键字。这里的B树是一个度为3的B树,每个节点最多有2个关键字和3个子节点。整个源码的基本思路是通过迭代地向下搜索找到插入键的位置,然后分裂满的节点,并插入新的键。最后使用printBTree函数来打印B树的所有关键字。
注意,这只是一个简化版本的B树的实现,没有包含完整的插入、删除和搜索等操作,并且没有考虑节点的删除和合并等情况。实际应用中,B树的实现会更加复杂和完善。
阅读全文