Class recursion question

C

cbmeeks

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
 
C

cbmeeks

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<Data> ChildClasses;
}

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
 
C

cbmeeks

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<Data> property.
 
C

cbmeeks

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top