简体   繁体   中英

Printing the keys of B+ Tree

I would like to print the keys of B+ Tree the way it actually looks in C .For example in the following form

                 | 12 |20| 30|

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

The above tree is of order(fanout) 4. Top node is tree node. Any algorithm or pseudocode would be highly appreciated.

EDIT

data structure i am implementing the tree.And the code for implementing the tree. When I try to print the tree, i get segmentation fault in the line Enqueue(tempNode->pointers[i]); of module 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;

Enqueuing and dequeuing:

         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;
                   }

Printing module


        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]);
                       }
                }

Use a BFS algorithm.

Basically by traversing the nodes using a FIFO queue, you'll get the layers one after another and then you can print them in the order you want.

I'm assuming that by "the way it actually looks", you mean a pretty diagram like how they print b-trees in a textbook.

Printing trees the way they actually look is a pretty-non trival problem if you want to solve it at the most general level. Concerns include:

  • keys have variable length and should be centered
  • keys at the top must be spaced far apart so keys at the bottom aren't squashed together
  • an optimal solution would react to sparsity in the tree and avoid big empty spaces
  • printing across multiple lines and the atomicity of your ASCII characters mean you have to figure out everything at once rather than relying on a recursive call.

I spent a while working on it in my data structures class but never arrived at a completely satisfying solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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