插入数据
#include <stdio.h>
#include <stdlib.h>
typedef struct BTNode {
int *key;
int num;
struct BTNode *parent;
struct BTNode **child;
} BTNode;
typedef struct BT {
int order;
BTNode *root;
} BT;
BTNode *createBTNode(int order) {
BTNode *pNode = (BTNode *) malloc(sizeof(BTNode));
pNode->key = (int *) malloc(sizeof(int) * order);
pNode->num = 0;
pNode->parent = NULL;
pNode->child = (BTNode **) malloc(sizeof(BTNode *) * (order + 1));
for (int i = 0; i < order; ++i) {
pNode->key[i] = 0;
}
for (int i = 0; i < order + 1; ++i) {
pNode->child[i] = NULL;
}
return pNode;
}
void initBT(BT *bt, int order) {
bt->order = order;
bt->root = createBTNode(order);
}
bool searchBT(BT *bt, int key, BTNode **pNode, int *index) {
BTNode *p = *pNode = bt->root;
int i = *index = 0;
while (p) {
*pNode = p;
*index = i;
if (p->num == i || p->key[i] > key) {
p = p->child[i];
i = 0;
} else if (p->key[i] < key) {
i++;
} else {
return true;
}
}
return false;
}
BTNode *splitNode(BT *bt, BTNode *pNode1) {
BTNode *pNode2 = createBTNode(bt->order);
for (int i = 0; i < bt->order / 2; ++i) {
pNode2->key[i] = pNode1->key[1 + i + bt->order / 2];
}
for (int i = 0; i < 1 + bt->order / 2; ++i) {
pNode2->child[i] = pNode1->child[1 + i + bt->order / 2];
if (pNode2->child[i]) {
pNode2->child[i]->parent = pNode2;
}
}
pNode2->num = pNode1->num = bt->order / 2;
pNode2->parent = pNode1->parent;
return pNode2;
}
void insertFix(BT *bt, BTNode *pNode1) {
BTNode *pNode2 = splitNode(bt, pNode1);
if (pNode1->parent) {
int i = 0;
while (i != pNode1->parent->num && pNode1->key[bt->order / 2] > pNode1->parent->key[i]) {
i++;
}
for (int j = pNode1->parent->num; j > i; j--) {
pNode1->parent->key[j] = pNode1->parent->key[j - 1];
pNode1->parent->child[j + 1] = pNode1->parent->child[j];
}
pNode1->parent->key[i] = pNode1->key[bt->order / 2];
pNode1->parent->child[i + 1] = pNode2;
pNode1->parent->num++;
if (pNode1->parent->num == bt->order) {
insertFix(bt, pNode1->parent);
}
} else {
initBT(bt, bt->order);
bt->root->key[0] = pNode1->key[bt->order / 2];
bt->root->child[0] = pNode1;
bt->root->child[1] = pNode2;
bt->root->num++;
pNode1->parent = pNode2->parent = bt->root;
}
}
void insertBT(BT *bt, int key) {
BTNode *p = NULL;
int i = 0;
if (searchBT(bt, key, &p, &i)) {
printf("关键字已存在!\n");
return;
}
for (int j = p->num; j > i; j--) {
p->key[j] = p->key[j - 1];
}
p->key[i] = key;
p->num++;
if (p->num == bt->order) {
insertFix(bt, p);
}
}
void printBT(BT *bt) {
BTNode *queue[1024] = {NULL};
int front = 0, rear = 0;
queue[rear++] = bt->root;
rear++;
while (front != rear - 1) {
if (queue[front]) {
printf("{");
for (int i = 0; i < queue[front]->num; ++i) {
if (i > 0) {
printf(",");
}
printf("%d", queue[front]->key[i]);
}
printf("} ");
for (int i = 0; i < queue[front]->num + 1; ++i) {
if (queue[front]->child[i]) {
queue[rear++] = queue[front]->child[i];
}
}
} else {
printf("\n");
rear++;
}
front++;
}
printf("\n");
}
void test() {
BT bt;
initBT(&bt, 5);
int sel, key;
while (true) {
printf("1.插入数据\n2.删除数据\n3.打印信息\n");
scanf("%d", &sel);
switch (sel) {
case 1: {
printf("关键字:");
scanf("%d", &key);
insertBT(&bt, key);
break;
}
case 2: {
printf("关键字:");
scanf("%d", &key);
break;
}
case 3: {
printBT(&bt);
break;
}
default:
return;
}
system("pause");
system("cls");
}
}
int main() {
test();
return 0;
}