Connecting Tech Pros Worldwide Help | Site Map

recursive

  #1  
Old January 24th, 2006, 10:45 PM
jw
Guest
 
Posts: n/a
i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
2

if i use this recursive function it prints ((2x(5-1))+(3x2))

void printTree(t)
if(t.left!=NULL)
print("(");
printTree(t.left);
print(t.element);
if(t.right!=NULL)
printTree(t.right);
print(")");
can you explain why it prints ((2x(5-1))+(3x2))

  #2  
Old January 24th, 2006, 10:55 PM
Victor Bazarov
Guest
 
Posts: n/a

re: recursive


jw wrote:[color=blue]
> i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
> 2[/color]

How does your "b.tree" _have_ that expression?
[color=blue]
> if i use this recursive function it prints ((2x(5-1))+(3x2))
>
> void printTree(t)
> if(t.left!=NULL)
> print("(");
> printTree(t.left);
> print(t.element);
> if(t.right!=NULL)
> printTree(t.right);
> print(")");
> can you explain why it prints ((2x(5-1))+(3x2))
>[/color]

It prints what it's asked to print, apparently. Since you don't show how
you form the tree from your prefix expression, there is no way to tell if
the function actually does the right thing or not.

V
  #3  
Old January 24th, 2006, 10:55 PM
jw
Guest
 
Posts: n/a

re: recursive


root=+
root->right=x x->left=3 x->right=2
root->left=x x->left=2 x->right=- - ->left=5
- ->right1

  #4  
Old January 24th, 2006, 11:15 PM
Victor Bazarov
Guest
 
Posts: n/a

re: recursive


jw wrote:[color=blue]
> root=+
> root->right=x x->left=3 x->right=2
> root->left=x x->left=2 x->right=- - ->left=5
> - ->right1
>[/color]

So, as I get it, it's like this

.. [ + ]
.. / \
.. [ x ] [ x ]
.. / \ / \
.. [ 2 ] [ - ] [ 3 ] [ 2 ]
.. / \
.. [ 5 ] [ 1 ]

What should it print? First it takes the root, right? Then it sees that
the left (the 'x') is not NULL. So, it prints the parenthesis first, and
then the left tree. How? What does it mean to print a tree when the left
'x' is now a temporary root? It looks at its left. It's not NULL (it's
the leftmost '2'), so it prints another parenthesis, and then the subtree
where the leftmost '2' is the new root. How? Its left is NULL, its
'element' is "2", and its right is NULL. Then it returns. Where? To
print the rest of the left 'x' subtree. How? It prints the element, it's
"x", then it see that the right is not NULL. It prints the right subtree
(with the '-' as its root). How? ...

Continue until you finish printing the right part of the '+'.

I don't see any C++ question, by the way. Next time post to a newsgroup
where your general programming question is more topical, comp.programming.

V
  #5  
Old January 24th, 2006, 11:15 PM
roberts.noah@gmail.com
Guest
 
Posts: n/a

re: recursive



jw wrote:[color=blue]
> i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
> 2
>
> if i use this recursive function it prints ((2x(5-1))+(3x2))
>
> void printTree(t)
> if(t.left!=NULL)
> print("(");
> printTree(t.left);
> print(t.element);[/color]
....[color=blue]
> can you explain why it prints ((2x(5-1))+(3x2))[/color]

A miracle occured.

  #6  
Old January 25th, 2006, 12:05 AM
jw
Guest
 
Posts: n/a

re: recursive


how does it return to x after printing 2?

  #7  
Old January 25th, 2006, 12:25 AM
Howard
Guest
 
Posts: n/a

re: recursive



"jw" <jackwht@gmail.com> wrote in message
[color=blue]
> how does it return to x after printing 2?[/color]

Look at your code:

void printTree(t)
if(t.left!=NULL)
print("(");
printTree(t.left);
print(t.element);
if(t.right!=NULL)
printTree(t.right);
print(")");

It prints the left: "(" + "2", then itself: "x", then the right: "(5-1)" +
")".

(By the way, it would really help if you'd quote the relevant parts of
previous replies, so we can see what you're talking about without having to
look at other posts.)

-Howard


  #8  
Old January 25th, 2006, 12:35 AM
jw
Guest
 
Posts: n/a

re: recursive



Howard wrote:[color=blue]
> "jw" <jackwht@gmail.com> wrote in message
>[color=green]
> > how does it return to x after printing 2?[/color]
>
> Look at your code:
>
> void printTree(t)
> if(t.left!=NULL)
> print("(");
> printTree(t.left);
> print(t.element);
> if(t.right!=NULL)
> printTree(t.right);
> print(")");
>
> It prints the left: "(" + "2", then itself: "x", then the right: "(5-1)" +
> ")".[/color]
ok but after this how does it return to the real root the print the
rightmost.[color=blue]
>
> (By the way, it would really help if you'd quote the relevant parts of
> previous replies, so we can see what you're talking about without having to
> look at other posts.)
>
> -Howard[/color]

  #9  
Old January 25th, 2006, 02:35 AM
Jim Langston
Guest
 
Posts: n/a

re: recursive


"jw" <jackwht@gmail.com> wrote in message
news:1138141883.483633.151140@g43g2000cwa.googlegr oups.com...[color=blue]
>i have a b.tree that has a prefix expression like this->+ x 2 - 5 1 x 3
> 2
>
> if i use this recursive function it prints ((2x(5-1))+(3x2))
>
> void printTree(t)
> if(t.left!=NULL)
> print("(");
> printTree(t.left);
> print(t.element);
> if(t.right!=NULL)
> printTree(t.right);
> print(")");
> can you explain why it prints ((2x(5-1))+(3x2))
>[/color]

.. [ + ]
.. / \
.. [ x ] [ x ]
.. / \ / \
.. [ 2 ] [ - ] [ 3 ] [ 2 ]
.. / \
.. [ 5 ] [ 1 ]

Well, just follow the logic. I presume you first call printTree by passing
it the root. Then printTree looks to see if there is a left brance, and
there is (the x). It prints a "(" and it calls printTree on the x.
printTree then looks to see if there is a left, and there is, the 2. It
prints a "(" and calls printTree on the 2. printTree looks to see if there
is a left on the 2, there isn't. It then prints the element 2. It looks to
see if there is a right branch on the 2, there isn't. So it returns (now
we're back where the x was). printTree prints the element x (so far we have
((2x ) It then looks for the right branch, there is one, it calls printTree
for the element -, etc...

It is recursion. You need to follow it through and remember what element
you are dealing with.


  #10  
Old January 25th, 2006, 04:25 AM
red floyd
Guest
 
Posts: n/a

re: recursive


jw wrote:[color=blue]
> how does it return to x after printing 2?
>[/color]

OK, guys, from this it's most likely homework.
  #11  
Old January 25th, 2006, 02:55 PM
roberts.noah@gmail.com
Guest
 
Posts: n/a

re: recursive



red floyd wrote:[color=blue]
> jw wrote:[color=green]
> > how does it return to x after printing 2?
> >[/color]
>
> OK, guys, from this it's most likely homework.[/color]

And it is riddled with syntax errors...thing doesn't even compile, let
alone print anything.

  #12  
Old January 25th, 2006, 03:55 PM
Howard
Guest
 
Posts: n/a

re: recursive



"jw" <jackwht@gmail.com> wrote in message
[color=blue][color=green]
>> Look at your code:
>>
>> void printTree(t)
>> if(t.left!=NULL)
>> print("(");
>> printTree(t.left);
>> print(t.element);
>> if(t.right!=NULL)
>> printTree(t.right);
>> print(")");
>>
>> It prints the left: "(" + "2", then itself: "x", then the right: "(5-1)"
>> +
>> ")".[/color]
> ok but after this how does it return to the real root the print the
> rightmost.[color=green]
>>[/color][/color]

If you can't figure it out by now, ask your teacher for help.

-Howard



Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Garbage collection of recursive inner function from.future.import@gmail.com answers 3 August 5th, 2008 01:25 PM
C# to SQL Recursive CTE Translation help champ1979 answers 0 March 19th, 2007 06:01 AM
Recursive descent algorithm able to parse Python? seberino@spawar.navy.mil answers 9 October 7th, 2006 04:25 AM
Recursive templates Jon Slaughter answers 7 September 12th, 2005 02:05 AM
Recursive Function Help (pls) pt 2 answers 2 July 22nd, 2005 05:13 AM