473,396 Members | 1,764 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,396 software developers and data experts.

Class recursion question

Hello all.

I have a project that I am working on and I need some suggestions.

First, I have a class that contains a value and a reference to a
parent class.

For example:

public class Data
{
public Data ParentClass;
public double Value;
}
....

Data d1 = new Data();
d1.Value = 100;

Data d2 = new Data();
d2.Value = 50;
d2.ParentClass = d1;
There is actually a lot more to the class but you get the idea.

Now, at this point I can easily find the value of d1 from d2 by
running "up the tree".

But my question is, how do I run down the tree?

For example, if a user only knew about d1, how could he know that d1
has "children" of d2? For that matter, d1 could have hundreds of
children.

I thought about using a "Data Container" that would contain a List of
Data classes but I'm not sure if that's the best way.

Thanks for any suggestions.

cbmeeks
Jun 27 '08 #1
5 2719
On Wed, 23 Apr 2008 18:38:58 -0700, cbmeeks <cb*****@gmail.comwrote:
[...]
public class Data
{
public Data ParentClass;
public double Value;
}
...

Data d1 = new Data();
d1.Value = 100;

Data d2 = new Data();
d2.Value = 50;
d2.ParentClass = d1;
There is actually a lot more to the class but you get the idea.

Now, at this point I can easily find the value of d1 from d2 by
running "up the tree".

But my question is, how do I run down the tree?
Given the data structure you've presented, you don't. At best, you could
enumerate the linked-list (which is actually what you have here), starting
with the lowest child node and working your way up until you find the node
with the parent of interest.
For example, if a user only knew about d1, how could he know that d1
has "children" of d2? For that matter, d1 could have hundreds of
children.

I thought about using a "Data Container" that would contain a List of
Data classes but I'm not sure if that's the best way.
It sounds like what you want is to create a tree-like structure.
Typically this would involve each instance keeping a reference to the
parent (as you have here) as well as a collection of child nodes (perhaps
that's what you're suggesting? I can't really tell for sure from what you
wrote).

So your class would look something like this:

public class Data
{
public Data ParentClass;
public double Value;
public List<DataChildClasses;
}

And I use the phrase "like this" loosely, since you really shouldn't have
public fields in your class, and you'll probably want to add methods
specifically for the maintenance of the child/parent relationships, so
that when you set an instance as the child of some other instance, both
the parent and child data are automatically updated.

You might want to look at the System.Windows.Forms.Control class, as it
implements this exact kind of data structure (it's not the only example in
..NET, but it's one of the most commonly used ones).

Pete
Jun 27 '08 #2
Thanks for the reply.
Given the data structure you've presented, you don't. At best, you could
enumerate the linked-list (which is actually what you have here), starting
with the lowest child node and working your way up until you find the node
with the parent of interest.
Right. That's what I was thinking. But I'm not sure what the lowest
node is. If I started at the top node, there would be no way that I
know of to go down the tree.

I was trying to avoid keeping a list of child nodes because I wanted
to easily move entire nodes around and switch parents. I was hoping
to move a branch by simply changing the parent node(class).
It sounds like what you want is to create a tree-like structure.
That is exactly what I am trying to do.
So your class would look something like this:

public class Data
{
public Data ParentClass;
public double Value;
public List<DataChildClasses;
}
Yeah, I was hoping to not have to do it this way if I could...but I
may not have a choice. I wonder how hard it would be to move classes
around...hmmm...let me think about this.....if I move a class around
by switching the parent class, all children should move with it...so
maybe it won't be as hard as I think..... LOL

And I use the phrase "like this" loosely, since you really shouldn't have
public fields in your class, and you'll probably want to add methods
Oh, I don't. That was just a simple representation so that my example
code wouldn't have a lot of lines to read. I use public properties
with business logic in them. I also have methods for summing values,
etc.

specifically for the maintenance of the child/parent relationships, so
that when you set an instance as the child of some other instance, both
the parent and child data are automatically updated.
Yeah this might be some work. If I insert a node(class), I will have
to add it to the child list.
Thanks for suggestions.

cb
Jun 27 '08 #3
Ahhh!

Man, I totally nailed it....well, for summing at least.

I already had a method that would Sum the data for a class.

So, I just looped through the list of child classes calling the sum
and it just worked.

I even went three layers deep and it worked....pretty simple.

Next, I will work on being able to move child nodes (classes) around.

Thanks for the tip...I was doing it backwards. I don't even need the
ParentData property any more. Just a List<Dataproperty.

Jun 27 '08 #4
On Wed, 23 Apr 2008 19:21:49 -0700, cbmeeks <cb*****@gmail.comwrote:
Ahhh!

Man, I totally nailed it....well, for summing at least.

I already had a method that would Sum the data for a class.

So, I just looped through the list of child classes calling the sum
and it just worked.

I even went three layers deep and it worked....pretty simple.

Next, I will work on being able to move child nodes (classes) around.
As you guessed in your previous post, this should not be difficult at
all. Moving a branch of your tree is as simple as moving the one node
that is the root of that branch. All of the children underneath that root
will be logically moved along with it, without having to actually touch
them.
Thanks for the tip...I was doing it backwards. I don't even need the
ParentData property any more. Just a List<Dataproperty.
As long as you're only ever descending the tree, that's true. You may
find that it's convenient to keep track of the parent for each node as
well though. For example, if all you have is the reference to a specific
node, being able to quickly get the parent would allow you to move/remove
that one node without a traversal of the tree.

Pete
Jun 27 '08 #5
As long as you're only ever descending the tree, that's true. You may
find that it's convenient to keep track of the parent for each node as
well though. For example, if all you have is the reference to a specific
node, being able to quickly get the parent would allow you to move/remove
that one node without a traversal of the tree.

Pete
Very true.

Thanks for the suggestions!
Jun 27 '08 #6

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...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
3
by: SamIAm | last post by:
Hi I have a form named form1. I have a class called class1. I want to call a recursive method in class1 called MyMethod() I would like log info to a listbox, listbox1, on the form as MyMethod...
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
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...
9
by: Christian E. Böhme | last post by:
Hello all, I ran into a little problem with recursive templates that I am not sure what it has to do with, essentially, since I am currently limited in my access to compilers (namely GCC 4.1)...
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 ??
12
by: Muzammil | last post by:
i want good practice over recursion. can any one give me links for recursion questions site.?? or question.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.