473,657 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2724
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<DataChildC lasses;
}

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<DataChildC lasses;
}
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....prett y 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<Dataproper ty.

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....prett y 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<Dataproper ty.
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
3415
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. And so it says ANTLR. Maybe I don't understand EBNF notation. For me it should look like this. or_expr::= xor_expr | xor_expr "|" xor_expr and in ANTLR grammar file like this:
12
2749
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 in modern day applications. Should we readily use it when we can or only when absolutly forced to use it?
15
9053
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
21
4063
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 A { std::vector<B*> Bs; public:
3
3848
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 performs its recursion. How do I pass an instance into class1 so that it can add items to the listbox? Should be easy I guess.
10
2507
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... Thanks.
75
5602
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 complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
9
2307
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) and until now had no chance of testing the code with others. It may be an implementation detail or even in the standard (which I have no access to, unfortunately). Or maybe I have hit one of those cases whose solutions are "undefined" by the...
35
4714
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
9091
by: Muzammil | last post by:
i want good practice over recursion. can any one give me links for recursion questions site.?? or question.
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.