我想按照它在 C 中的实际外观打印 B+ 树的键。例如以下形式
| 12 |20| 30|
5|9|11| |12|17|_| |20|27|26| |30|-|-|
上面的树是有序的(fanout) 4. Top节点是树节点。任何算法或伪代码将不胜感激。
编辑
数据结构我正在实现树。以及实现树的代码。当我尝试打印树时,我在行中遇到段错误
入队(tempNode->指针[i]);
模块 printBplus(root)
typedef struct bplus{
void ** pointers; /*These are the array of pointers that each tree node contains*/
int * keys; /*These are the array of keys in each tree node*/
struct bplus * parent; /*This the pointer to the parent tree node */
bool is_Leaf; /*To check if the node is leaf*/
int num_Keys; /*This keeps track the number of active keys in the node */
struct bplus * next ; /*This is the pointer to next level tree,used for queuing and dequeing node*/
} *bplus, bplus_node;
入队和出队:
void Enqueue(bplus a){
bplus bplusTemp;
if (queue == NULL) { //bplus queue=NULL is global variable
queue = a
queue->next = NULL;
}
else {
bplusTemp = queue;
while(bplusTemp->next != NULL) {
bplusTemp = bplusTemp->next;
}
bplusTemp->next = a;
bplusNew->next = NULL;
}
}
bplus Dequeue( void ) {
bplus bplusTemp = queue;
queue = queue->next;
bplusTemp->next = NULL;
return bplusTemp;
}
打印模块
void PrintBplus(bplus root){
int i;
bplus tempNode;
queue = NULL;
Enqueue(root); /*It enques the root*/
if(root === leaf)
print the keys associated with it
while(queue != NULL){
tempNode = Dequeue();
for(i=0; i < tempNode->num_Keys; i++)
printf("%d,",root->keys[i]);
if(tempNode->is_Leaf == false){
for(i=0; i <= tempNode->num_Keys; i++)
Enqueue(tempNode->pointers[i]);
}
}
最佳答案
关于c - 打印 B+ 树的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405000/