473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

recursion ?

I'm surprised that I'm having trouble with this, I have done this before...
anyway, I'm trying to locate a TreeNode in a TreeView wqith a recursive
function, here is the code
<code>
private TreeNode RecursivelyFindNodeByTag(TreeNode node, Object data)
{
if(node.Tag == data)
{
return node;
}

foreach(TreeNode n in node.Nodes)
{
if(n.Tag == data)
{
return n;
}
RecursivelyFindNodeByTag(n, data);
}
return null;
}
</code>

I pass the TopNode to the initial call to RecursivelyFindNodeByTag
It find the node, but the stack still needs to unwind it's calls, as is it
does, it returns null. I'm after a total block on this, what am I doing
wrong?

Thanks for taking a look,
Steve
Nov 17 '05 #1
4 1367
Steve,
foreach(TreeNode n in node.Nodes)
{
if(n.Tag == data)
{
return n;
}
You don't really need this if since the check will be made by the
recursive call.

RecursivelyFindNodeByTag(n, data);


Should be

return RecursivelyFindNodeByTag(n, data);
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Steve wrote:

<snip>
I'm after a total block on this, what am I doing
wrong?

I guess it should be better like this, from the top of my head:

private TreeNode RecursivelyFindNodeByTag(TreeNode node, Object data) {
if(node.Tag == data)
return node;
foreach(TreeNode n in node.Nodes) {
TreeNode result = RecursivelyFindNodeByTag(n, data);
if (result != null)
return result;
}
return null;
}
--
Oliver Sturm
Developer Express Inc.
Nov 17 '05 #3
Thanks all, it was a bug on my part, I had some leftover code that was
getting called that was inserting nodes during the search, so that was
screwing everything up.
Thanks for the help!
Steve
"Steve" <ss*@sss.com> wrote in message
news:Or**************@tk2msftngp13.phx.gbl...
I'm surprised that I'm having trouble with this, I have done this before... anyway, I'm trying to locate a TreeNode in a TreeView wqith a recursive
function, here is the code
<code>
private TreeNode RecursivelyFindNodeByTag(TreeNode node, Object data)
{
if(node.Tag == data)
{
return node;
}

foreach(TreeNode n in node.Nodes)
{
if(n.Tag == data)
{
return n;
}
RecursivelyFindNodeByTag(n, data);
}
return null;
}
</code>

I pass the TopNode to the initial call to RecursivelyFindNodeByTag
It find the node, but the stack still needs to unwind it's calls, as is it
does, it returns null. I'm after a total block on this, what am I doing
wrong?

Thanks for taking a look,
Steve

Nov 17 '05 #4
To iterate is human, to recurse, divine
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.