[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

recursive function problem

Jianlong

12/5/2008 5:18:00 AM

Hi all,
I use a recursive function in my code. My data structure is a tree,
the tree is composed with branches, and the branch has its name and
children. The purpose of this function is to find the branch with a
given name. My function is as follows. The problem is that: printf can
print the branch with given name during search. but the final return
value of the function is always the last branch in the data. That
means the recursive function does not stop and return "b" even if the
"if" condition is met and "return b" is processed.

Would you be kind to help me how to solve this problem? That means
when the condition is met, I just want to return the value and does
not continue to get other values.

Thanks!


Branch * findBranch(Branch* b, int name)
{
if(b->name == name){
printf("Active Branch is: %d\n",b->name);
return b;
}
// loop through the whole tree to find it
for ( Branch * c = b->children.head; c != NULL; c = c->nextChild ){
findBranch( c, name );
}
}
6 Answers

Rolf Magnus

12/5/2008 5:54:00 AM

0

Jianlong wrote:

> Hi all,
> I use a recursive function in my code. My data structure is a tree,
> the tree is composed with branches, and the branch has its name and
> children.

That would be a node, not a branch. The branch is the link between a node
and one of its children.

> The purpose of this function is to find the branch with a
> given name. My function is as follows. The problem is that: printf can
> print the branch with given name during search. but the final return
> value of the function is always the last branch in the data. That
> means the recursive function does not stop and return "b" even if the
> "if" condition is met and "return b" is processed.

Well, you don't give the pointer back outside. If the current node isn't the
one you searched, you return nothing. That means for one that you don't
pass values back that are found somewhere in a subtree, and it means that
if the node isn't found in a s subtree, the function also doesn't report
that back.

> Would you be kind to help me how to solve this problem? That means
> when the condition is met, I just want to return the value and does
> not continue to get other values.

How about something like that:

Branch * findBranch(Branch* b, int name)
{
if(b->name == name){
printf("Active Branch is: %d\n",b->name);
return b;
}

// loop through the whole tree to find it
for ( Branch * c = b->children.head; c != NULL; c = c->nextChild ){
Branch* branch = findBranch(c, name);
if (branch)
return branch;
}
return 0;
}

And then, I suggest to rename Branch to Node.

Jianlong

12/5/2008 6:10:00 AM

0

On Dec 5, 4:54 pm, Rolf Magnus <ramag...@t-online.de> wrote:
> Jianlong wrote:
> > Hi all,
> > I use a recursive function in my code. My data structure is a tree,
> > the tree is composed with branches, and the branch has its name and
> > children.
>
> That would be a node, not a branch. The branch is the link between a node
> and one of its children.
>
> > The purpose of this function is to find the branch with a
> > given name. My function is as follows. The problem is that: printf can
> > print the branch with given name during search. but the final return
> > value of the function is always the last branch in the data. That
> > means the recursive function does not stop and return "b" even if the
> > "if" condition is met and "return b" is processed.
>
> Well, you don't give the pointer back outside. If the current node isn't the
> one you searched, you return nothing. That means for one that you don't
> pass values back that are found somewhere in a subtree, and it means that
> if the node isn't found in a s subtree, the function also doesn't report
> that back.
>
> > Would you be kind to help me how to solve this problem? That means
> > when the condition is met, I just want to return the value and does
> > not continue to get other values.
>
> How about something like that:
>
> Branch * findBranch(Branch* b, int name)
> {
>   if(b->name == name){
>     printf("Active Branch is: %d\n",b->name);
>     return b;
>   }
>
>   // loop through the whole tree to find it
>   for ( Branch * c = b->children.head; c != NULL; c = c->nextChild ){
>     Branch* branch = findBranch(c, name);
>     if (branch)
>       return branch;
>   }
>   return 0;
>
> }
>
> And then, I suggest to rename Branch to Node.

Great! Your solution works for me! Thanks a lot!

Salt_Peter

12/5/2008 6:11:00 AM

0

On Dec 5, 12:18 am, Jianlong <henryj...@gmail.com> wrote:
> Hi all,
> I use a recursive function in my code. My data structure is a tree,
> the tree is composed with branches, and the branch has its name and
> children. The purpose of this function is to find the branch with a
> given name. My function is as follows. The problem is that: printf can
> print the branch with given name during search. but the final return
> value of the function is always the last branch in the data. That
> means the recursive function does not stop and return "b" even if the
> "if" condition is met and "return b" is processed.
>
> Would you be kind to help me how to solve this problem? That means
> when the condition is met, I just want to return the value and does
> not continue to get other values.
>
> Thanks!
>
> Branch * findBranch(Branch* b, int name)
> {
> if(b->name == name){
> printf("Active Branch is: %d\n",b->name);
> return b;
> }
> // loop through the whole tree to find it
> for ( Branch * c = b->children.head; c != NULL; c = c->nextChild ){
> findBranch( c, name );
> }
>
> }

Inside that for loop, findBranch's return is dropped. Why should the
program somehow magicly start returning some value or pointer since
you've not told it to do so?

In case you don't see the light, the above can only work if the
initial call to the function immediately finds a match. The moment you
generate a new function stack, a return, if any, gets dropped.

Yisroel Markov

6/18/2014 10:01:00 PM

0

On Wed, 18 Jun 2014 11:12:20 +0000 (UTC), "news"
<news@fx13.iad.highwinds-media.com> said:

>On Tue, 17 Jun 2014 19:08:26 +0000 (UTC), Yisroel Markov
><ey.markov@MUNGiname.com> wrote:
>
>>On Tue, 17 Jun 2014 07:57:39 +0000 (UTC), "news"
>><news@fx10.iad.highwinds-media.com> said:
>>
>>>On Mon, 16 Jun 2014 15:28:34 +0000 (UTC), Yisroel Markov
>>><ey.markov@MUNGiname.com> wrote:
>>>
>>>>On Mon, 16 Jun 2014 09:22:23 +0000 (UTC), "news"
>>>><news@fx11.iad.highwinds-media.com> said:
>>
>>[snip]
>>
>>>>>>>If it's
>>>>>>>blood, you're always family. If it's marriage, then you're only
>>>>>>>family until you divorce your way out. Conversion could be analogous
>>>>>>>to marriage here.
>>>>>>
>>>>>>It could be except that it isn't.
>>>>>
>>>>>Why isn't it? It's a way in and also a way out.
>>>>
>>>>Because by Jews there's no way out, which is why adoption is a better
>>>>analogy. And that's what we're doing in this thread - looking for a
>>>>good analogy. At least that's what I'm doing.
>>>
>>>But by whichever religion the convert leaves for, there is a way out.
>>>Judaism has no way of holding on to someone who has left voluntarily.
>>>If someone no longer wishes to adhere to Jewish 'law', it's a fait
>>>accompli, regardless what Jewish 'law' thinks.
>>
>>This is absolutely true, but IMHO absolutely irrelevant. Judaism has
>>no way to hold on, true, but it never relinguishes its claim. Like I
>>said, I believe in voluntary legal systems.
>
>But surely the fact that Judaism never relinquishes its claim becomes
>irrelevant to those who choose to leave it?

Yes, surely. What's your point?

>'Voluntary legal system'
>sounds like a contradiction in terms.

Not to me, but I think I understand where you're coming from. Consider
the myriad voluntary associations out there that have rules and
by-laws, and even judicial bodies (adjudication panels and the like).
The analogy is imperfect, of course - ultimately, clubs and the like
enforce their rules by the threat of expulsion, something Jews can't
do - but IMHO it serves.

Or consider a somewhat similar case. On 06/16/14 the US Supreme Court
ruled for the respondent in Republic of Argentina v. NML Capital, Ltd.
Argentina is now under orders to pay up on its 1994 sovereign bonds
and disclose to NML the location and value of its extraterritorial
assets.

How come a private company can sue a sovereign nation in a US court?
Only because back in 1994 the government of Argentina agreed to be
bound by New York law regarding its bond offering. (This didn't stop
it from trying to assert sovereign immunity anyway, but the courts
laughed at that.) Now NML doesn't really have many ways to collect,
short of persuading the US to send a fleet south, as the US courts'
reach outside the country is rather limited, but IMHO Argentina is
honor-bound to obey the law it voluntarily chose to submit to.
--
Yisroel "Godwrestler Warriorson" Markov - Boston, MA Member
www.reason.com -- for a sober analysis of the world DNRC
--------------------------------------------------------------------
"Judge, and be prepared to be judged" -- Ayn Rand

Yisroel Markov

6/18/2014 10:15:00 PM

0

On Wed, 18 Jun 2014 18:53:48 +0000 (UTC), Herman Rubin
<hrubin@skew.stat.purdue.edu> said:

>On 2014-06-17, Yisroel Markov <ey.markov@MUNGiname.com> wrote:
>> On Tue, 17 Jun 2014 07:57:39 +0000 (UTC), "news"
>><news@fx10.iad.highwinds-media.com> said:
>
>>>On Mon, 16 Jun 2014 15:28:34 +0000 (UTC), Yisroel Markov
>>><ey.markov@MUNGiname.com> wrote:
>
>>>>On Mon, 16 Jun 2014 09:22:23 +0000 (UTC), "news"
>>>><news@fx11.iad.highwinds-media.com> said:
>
>> [snip]
>
>>>>>>>If it's
>>>>>>>blood, you're always family. If it's marriage, then you're only
>>>>>>>family until you divorce your way out. Conversion could be analogous
>>>>>>>to marriage here.
>
>>>>>>It could be except that it isn't.
>
>>>>>Why isn't it? It's a way in and also a way out.
>
>>>>Because by Jews there's no way out, which is why adoption is a better
>>>>analogy. And that's what we're doing in this thread - looking for a
>>>>good analogy. At least that's what I'm doing.
>
>>>But by whichever religion the convert leaves for, there is a way out.
>>>Judaism has no way of holding on to someone who has left voluntarily.
>>>If someone no longer wishes to adhere to Jewish 'law', it's a fait
>>>accompli, regardless what Jewish 'law' thinks.
>
>> This is absolutely true, but IMHO absolutely irrelevant. Judaism has
>> no way to hold on, true, but it never relinguishes its claim. Like I
>> said, I believe in voluntary legal systems.
>
>Then you agree with me that this claim should not be part of Judaism.

No. I don't see how this follows.
--
Yisroel "Godwrestler Warriorson" Markov - Boston, MA Member
www.reason.com -- for a sober analysis of the world DNRC
--------------------------------------------------------------------
"Judge, and be prepared to be judged" -- Ayn Rand

mm

6/18/2014 11:26:00 PM

0

On Wed, 18 Jun 2014 10:54:09 +0000 (UTC), "news"
<news@fx13.iad.highwinds-media.com> wrote:

>On Tue, 17 Jun 2014 19:46:28 +0000 (UTC), mm <mm2005@bigfoot.com>
>wrote:
>
>>On Tue, 17 Jun 2014 07:57:39 +0000 (UTC), "news"
>><news@fx10.iad.highwinds-media.com> wrote:
>>
>>>On Mon, 16 Jun 2014 15:28:34 +0000 (UTC), Yisroel Markov
>>><ey.markov@MUNGiname.com> wrote:
>>>
>>>>On Mon, 16 Jun 2014 09:22:23 +0000 (UTC), "news"
>>>><news@fx11.iad.highwinds-media.com> said:
>>>>
>>>>>On Sun, 15 Jun 2014 20:57:46 +0000 (UTC), mm <mm2005@bigfoot.com>
>>>>>wrote:
>>>>>
>>>>>>On Fri, 13 Jun 2014 06:22:28 +0000 (UTC), "news"
>>>>>><news@fx24.iad.highwinds-media.com> wrote:
>>>>>>
>>>>>>>On Thu, 12 Jun 2014 14:36:03 +0000 (UTC), Yisroel Markov
>>>>>>><ey.markov@MUNGiname.com> wrote:
>>>>>>>
>>>>>>>>On Thu, 12 Jun 2014 08:26:36 +0000 (UTC), "news"
>>>>>>>><news@fx21.iad.highwinds-media.com> said:
>>>>>>>>
>>>>>>>>>On Wed, 11 Jun 2014 20:22:20 +0000 (UTC), Yisroel Markov
>>>>>>>>><ey.markov@MUNGiname.com> wrote:
>>>>>>>>>
>>>>>>>>>>On Wed, 11 Jun 2014 05:44:05 +0000 (UTC), "news"
>>>>>>>>>><news@fx20.iad.highwinds-media.com> said:
>>>>>>>>>>
>>>>>>>>>>>On Tue, 10 Jun 2014 16:49:58 +0000 (UTC), Yisroel Markov
>>>>>>>>>>><ey.markov@MUNGiname.com> wrote:
>>>>>>>>>>>
>>>>>>>>>>>>On Tue, 10 Jun 2014 13:28:22 +0000 (UTC), "news"
>>>>>>>>>>>><news@fx05.iad.highwinds-media.com> said:
>>>>>>>>>>>>
>>>>>>>>>>>>[snip]
>>>>>>>>>>>>
>>>>>>>>>>>>>Ethnicity is determined at birth. It can neither be acquired nor
>>>>>>>>>>>>>lost. You cannot 'convert' from one ethnicity to another.
>>>>>>>>>>>>
>>>>>>>>>>>>Which is why traditional Jews speak of being Jewish in terms of
>>>>>>>>>>>>nationality. Ethnicity is usually irrelevant. I have Russian ethnicity
>>>>>>>>>>>>mixed in. Ethiopian Jews have Yoruba ethnicity. We're all Jews,
>>>>>>>>>>>>though.
>>>>>>>>>>>>
>>>>>>>>>>>>If you "stick to ethnicity," then we'll be talking at each other,
>>>>>>>>>>>>rather than to each other.
>>>>>>>>>>>
>>>>>>>>>>>So you don't accept the concept of a Jewish (Middle Eastern) ethnicity
>>>>>>>>>>>at all? It seems to me that most of us have at least some.
>>>>>>>>>>
>>>>>>>>>>I do, but it's mostly irrelevant to halakha, which is how I think
>>>>>>>>>>about the "who is a Jew" question, including application to
>>>>>>>>>>conversion. Plus, as you say, "religion has no effect at all on
>>>>>>>>>>ethnicity."
>>>>>>>>>
>>>>>>>>>I've heard the word 'tribe' used in conjunction with "who's a Jew",
>>>>>>>>>but I don't particularly care for it. It sounds like a loose
>>>>>>>>>collection of nomadic people.
>>>>>>>>
>>>>>>>>[nod] I prefer the term "clan" in this context. Then, conversion can
>>>>>>>>be viewed as an adoption into the clan. And adoption is usually (but
>>>>>>>>not always) irreversible - once you're family, you're always family.
>>>>>>>
>>>>>>>The word 'family' sugests a link by either blood or marriage.
>>>>>>
>>>>>>Or adoption.
>>>>>
>>>>>Indeed. But compared to blood and marriage it's a rarity.
>>>>
>>>>Indeed. But conversion *is* a rarity. So, historically, are clan
>>>>adoptions.
>>>
>>>See my other post.
>>>
>>>>>> Yisroel just used the word twice in the paragraph you
>>>>>>just quoted.
>>>>>
>>>>>But in the context of conversion being adoption.
>>>>>
>>>>>>If you don't think adoption of a child make hir part of
>>>>>>the family, it's best if you don't adopt a child.
>>>>>
>>>>>I didn't think it was worth mentioning. And I have no intention of
>>>>>adopting any children, thanks.
>>>>>
>>>>>>>If it's
>>>>>>>blood, you're always family. If it's marriage, then you're only
>>>>>>>family until you divorce your way out. Conversion could be analogous
>>>>>>>to marriage here.
>>>>>>
>>>>>>It could be except that it isn't.
>>>>>
>>>>>Why isn't it? It's a way in and also a way out.
>>>>
>>>>Because by Jews there's no way out, which is why adoption is a better
>>>>analogy. And that's what we're doing in this thread - looking for a
>>>>good analogy. At least that's what I'm doing.
>>
>>Below you have confused adhering to Judaism with being a Jew.
>
>No, I haven't. See below.

I looked below. I could phrase what I said better. You're confusing
not adhering to Judaism with not being a Jew***. You don't know what
it takes to not be a Jew, once one is one,and you're confusing not
adhering to Judaism with that.

***This is what I meant in my line just above, but I left out the two
not's, as people often do in situations like this. But it's better to
be precise.
>
>>>But by whichever religion the convert leaves for, there is a way out.
>>
>>Of course there is a way out of the religion and he doesn't even have to
>>convert to "whichever religion". He can just stop observing Judaism
>>and for example keep only the Noachide laws, or not even them.
>
>Right.
>
>>>Judaism has no way of holding on to someone who has left voluntarily.
>>
>>Of course not. The only people who say otherwise are others who confuse
>>Judaism, a religion, with being a Jew.
>
>I have never done so. I fully recognise the difference.
>
>>>If someone no longer wishes to adhere to Jewish 'law', it's a fait
>>>accompli, regardless what Jewish 'law' thinks.
>>
>>A straw man. Jewish "law" "thinks" nothing that contradicts that.
>>There have always been sinners, there have always been unrepentant
>>sinners, and there have, probably always, been those who sin in just
>>about every way. Do you think Judaism doesn't know that?. There are
>>even discussions about it, for example, various motivations for sins.
>>
>>Wherever your thoughts have gone, Judaism has been there, had a cup of
>>tea, and come back already.
>>
>>As to whether a person is a Jew or not (the thing you're confusing with
>>practicing Judaism), once a Jew always a Jew.
>
>I'm not confusing anything. Someone who practises Judaism is a Jew.

Huh? If you want to clarify this you might be right, but it sounds
like you're wrong.

>Christians don't do it. Nor do Muslims. But a Jew doesn't have to
>practise Judaism.

That's right.

>As for once a Jew always a Jew, see below.
>
>> If it werent' for
>>that**, your 20 years as an Anglican might have knocked you out of the
>>Jewish people. **But that IS the law, so it's almost silly to guess
>>at what would happen if it it weren't. So you're still a Jew.
>
>I am indeed. But only because I'm a born Jew with Jewish ethnicity.

Ethniicity has nothing to do with it. Evertjan explains it well in his
reply.

>But had I converted to Judaism and thereby become a Jew and then
>subsequently converted to something else, I would no longer be either
>an adherent of Judaism nor a Jew

No. You would still be a Jew.

> because I would have abandoned the
>one thing that qualified me to be a Jew. This is where 'once a Jew
>always a Jew' breaks down.

No. This is your malarkey. Whoever told you this, go back and tell
them they were wrong. And if thought of it yourself, well, you're
wrong.


--

Meir
It is better to eat an onion in Jerusalem than a cockerel in Egypt. 1055CE