繁体   English   中英

打印B +树的键

[英]Printing the keys of B+ Tree

我想打印B +树的键,就像它在C中的样子一样,例如以下形式

                 | 12 |20| 30|

         5|9|11|     |12|17|_|    |20|27|26|   |30|-|-|

上面的树是order(扇出)4。顶部节点是树节点。 任何算法或伪代码将不胜感激。

编辑

数据结构,我正在实现树。以及实现树的代码。 当我尝试打印树时,在行Enqueue(tempNode->pointers[i]);分段错误Enqueue(tempNode->pointers[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队列遍历节点,您将一层又一层地获取图层,然后可以按所需顺序打印它们。

我假设通过“它的实际外观”来表示一个漂亮的图,例如它们如何在教科书中打印b树。

如果要从最一般的角度解决树,按树的实际外观打印树是一个非常简单的问题。 问题包括:

  • 键的长度可变,应居中
  • 顶部的按键必须相距较远,因此底部的按键不能挤压在一起
  • 最佳解决方案将对树的稀疏性做出反应并避免大的空白空间
  • 多行打印和ASCII字符的原子性意味着您必须立即弄清所有内容,而不是依赖递归调用。

我在数据结构课程中花了一段时间研究它,但从未想到一个完全令人满意的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM