i 've already mentioned "if number of levels is known". Well for dynamic
case we can print the tree from bottom level to top using recursion and at
each increment if level after printing the values we pass "back" the level
number and accordingly we print space . The output will look like this-

 5  6  7  8
   2  4  2
     1  3
       9

Could someone pleases modify this solution to print tree in top-down
fashion?

On Sun, Aug 28, 2011 at 11:38 PM, Rishabbh A Dua <[email protected]>wrote:

> mohit, wat if the tree is growing dynamically?
>
> On Sun, Aug 28, 2011 at 11:27 PM, mohit verma <[email protected]>wrote:
>
>> if the number of levels is known - while traversing the tree in BFS order
>> keep a loop to print spaces in number- n/2,n/2-1,n/2-2 and so on ,before
>> entering at each level. Now if you find any child node empty just print a
>> blank space in place of its value.
>>
>>
>> On Sun, Aug 28, 2011 at 10:01 PM, Dave <[email protected]> wrote:
>>
>>> @Navneet: I suggest that you do an in-order traversal. Assign x values
>>> to the nodes sequentially, with y values based on the depth from the
>>> root. Thus, in your example, d has coordinates (0,2), b: (1,1), e:
>>> (2,2), a: (3,0), f: (4,2), c: (5,1), g: (6,2).
>>>
>>> Dave
>>>
>>> On Aug 28, 9:46 am, Navneet Gupta <[email protected]> wrote:
>>> > Hope the question is clear. Basically you need to print a given tree
>>> > such that spaces will depict the left/right relation at every level.
>>> >
>>> > output should be something like
>>> >                     a
>>> >                  b     c
>>> >               d   e  f   g
>>> >
>>> > Levels are separated by new lines. Notice that space between nodes at
>>> > higher levels increases with the number of levels we have. Assume a
>>> > max of 10 levels. But the algorithm should scale.
>>> >
>>> > --
>>> > Regards,
>>> > Navneet
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>
>>
>> --
>> ........................
>> *MOHIT VERMA*
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Rishabbh A Dua
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
........................
*MOHIT VERMA*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to