c - 打印 B+ 树的键

标签 c algorithm binary-tree

我想按照它在 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]);
                       }
                }

最佳答案

使用 BFS 算法。

基本上是通过使用 FIFO 遍历节点queue,你会一个接一个地得到图层,然后你可以按照你想要的顺序打印它们。

关于c - 打印 B+ 树的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405000/

相关文章:

c - 在第 n 个位置插入节点

c - 主函数的参数,无法理解!

algorithm - 分离和模式匹配技术

java - 随机整数上的堆栈溢出

从数组中按级别顺序创建二叉树

c - 返回指向自动变量的指针的函数

c++ - 如何仅使用 OpenGL 方法绘制文本?

c - 如何正确添加到链表数组?

c - C中的二叉搜索树搜索错误

algorithm - 左子树的深度小于右子树的深度