Click here to Skip to main content
14,920,188 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello everyone,
Suppose i have a class and this class has a properties of its own type
forexample// node.node. etc

Now i want to identify the depth of my tree, for that i made a new property(int depth)
so that each new generated node it will have a depth value = parentdepth + 1;

Now i want to know is there a relation between the class and its property so that i can use for this job,

Thanks in advance,
z3ngew
Posted
Updated 2-Feb-13 7:36am
v2
Comments
zaphoed 2-Feb-13 14:47pm
   
hi i admit that i don't understand your question. So far, i think i understood that you've got something like this:

class X
{
public int Depth { get; private set; }
public List<x> Children { get; private set; }

public X(X parent) { Depth = (parent != null ? parent.Depth + 1 : 0); }
}

can you please elaborate on "is there a relation between the class and its property"?
   
Wrong idea! You are thinking of classes as something which may or may not contain some tool. Instead, you should think of classes as an instrument for creation whatever you want. Ad storing depth is also a bad idea.
—SA

Don't try storing a depth - it will either be wrong, or will get out of step and cause problems. If you need to know the depth of a particular node, follow it's parent list back until you either get to the node you are checking from, or to the head of the tree. It's a simple loop if you have a parent property in your node - if you don't then you need to recursively check down the tree until you find the node, or run out of nodes to check.
   
Comments
   
Agree, a 5.
—SA
Joezer BH 3-Feb-13 1:12am
   
5+
Check this link: Link
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900