Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int k = 200 ; // defined in same class as the function print .

         public void print ( BNode n )      
        {
        for ( int k = temp ; k>=0 ; k=k-30 )
        {
            System.out.print("     ");
        }
        
        for ( int i = 0 ; i < n.count ; i++ ) 
        {
            System.out.print ( n.getValue(i) + " " ) ;    
        }
        
        
        
        if ( !n.leaf )  
        {
               System.out.println ("") ;
            
            for ( int j = 0; j <= n.count ; j++ )
            {				  
                temp = temp -- ;
               
                if ( n.getChild(j) != null ) 
                {			         
                 
                    print( n.getChild(j) ) ;     
            
                }
              
            }
            
        }
    
    }


I am not able to think of a way to print the key values in a tree fashion . I want the root node to be at the middle , children nodes starting from the next line and left most child to the left of the root and so on .

Please help .

What I have tried:

Implemented the code in Java .
Posted
Updated 28-Apr-18 5:56am
Comments
Richard MacCutchan 28-Apr-18 9:49am    
You would need to build a list of all the nodes first, calculating their relative positions in the tree. Once you have that information you can calculate where on your printout that you need to position each one in the tree diagram.

1 solution

A B-tree is a structure, basically, a node that contain a value and 2 pointers to 2 child nodes. The trick is that each child tree is a B-tree itself.
in this B-tree
  1
 2
  3
4
  5
 6
  7

 1
2
 3

is a B-tree by itself with just an indentation.
A B-tree can have an unlimited depth, so the most obvious solution is to use recursion.
you just have to keep track of indentation level.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900