Re: [algogeeks] Re: Print binary tree in spiral

2009-11-23 Thread Arun
I've have one solution using two stacks, fs (forward stack) to print
elements in forward manner and rs to print in reverse.
Basic idea is while printing elements in forward order, put the elements in
reverse stack so that the while popping the reverse stack the last element
in row is popped first. Similary while popping reverse stack, make sure, the
first element in the next level is entered last in the stack for the next
iteration of forward printing.

if(root)
  fs.push(root)

while (!fs.empty() || !rs.empty()){
while(!fs.empty()){
elem = fs.pop();
print elem.data;
if(elem-left) rs.push(elem-left);
if(elem-right) rs.push(elem-right);
}
while(!rs.empty()){
elem = rs.pop();
print elem.data;
if(elem-right) fs.push(elem-right);
if(elem-left) fs.push(elem-left);
}
}


On Mon, Nov 23, 2009 at 10:47 AM, Nayn nayanish.hi...@gmail.com wrote:

 Honestly.. guys.. i would appreciate if we could come up with
 something concrete.

 On Nov 20, 5:38 pm, ganesa thandavam gthanda...@gmail.com wrote:
  we need to use queue and stack alternately ...
 
  once we handle this , i think it should be straight forward to
  code ...
 
  On Nov 20, 9:27 am, dinesh bansal bansal...@gmail.com wrote:
 
 
 
   On Wed, Nov 18, 2009 at 8:05 AM, Nayn nayanish.hi...@gmail.com
 wrote:
Hi guys,
Recently I came across a problem. We've to display a binary tree in
spiral.
1. We need to print the nodes in BFS manner.
2. The nodes should be displayed in alternate direction; in one level
from left to right and in next level right to left.
Needless to mention, we need least time complex solution.
Thanks
Nayn
 
--
 
You received this message because you are subscribed to the Google
 Groups
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com
 algogeeks%2bunsubscr...@googlegroups .com
.
For more options, visit this group at
   http://groups.google.com/group/algogeeks?hl=.
 
If its just about display, you can traverse the tree in BFS manner
 and
 
   store the nodes in an array at their specific locations. At the end,
 display
   the nodes from the array.
 
   Thanks,
   --
   Dinesh Bansal
   The Law of Win says, Let's not do it your way or my way; let's do it
 the
   best way.

 --

 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




--

You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




Re: [algogeeks] Re: Print binary tree in spiral

2009-11-23 Thread Rohit Saraf
And it cannot be made more efficient.

--

You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




Re: [algogeeks] Re: Print binary tree in spiral

2009-11-23 Thread Algoose Chase
I posted this long b4 but dint see this error : Delivery to the following
recipient failed permanently: algogeeks@googlegroups.com
re-posting:
BST_Spiral(struct node* root)
{
  ht = Height(root);

  for( i = 0; i= ht; i++)
  {
PrintSpiral(root, i, i%2  /*flip 1 and 0 alternately on each
iteration*/);
  }

void PrintSpiral(struct node* root, int level, bool bRTL/* Right-To-Left */)
{
  if ( root == NULL) return;

 if( level == 0 )
 {
   printf(%d, root-data)
   return;
 }

 if( bRTL )
 {
   PrintSpiral(root-right,level-
1,bRTL)
   PrintSpiral(root-left,level-1,bRTL)
 }
 else
 {
   PrintSpiral(root-left,level-1,bRTL)
   PrintSpiral(root-right,level-1,bRTL)
 }
}


On Tue, Nov 24, 2009 at 11:12 AM, Rohit Saraf
rohit.kumar.sa...@gmail.comwrote:

 And it cannot be made more efficient.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


--

You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.