Click here to Skip to main content
11,493,937 members (70,484 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 2-Feb-13 8:33am
z3ngew813
Edited 2-Feb-13 8:36am
v2
Comments
zaphoed at 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 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"?
Sergey Alexandrovich Kryukov at 2-Feb-13 16:34pm
   
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 2-Feb-13 16:33pm
   
Agree, a 5.
—SA
Edo Tzumer at 3-Feb-13 1:12am
   
5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check this link: Link
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 408
1 F-ES Sitecore 254
2 Nakul Chaudhari 235
3 OriginalGriff 235
4 CPallini 223
0 Sergey Alexandrovich Kryukov 10,237
1 OriginalGriff 8,730
2 Sascha Lefèvre 3,664
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100