Class recursion question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cbmeeks

    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


  • Peter Duniho

    #2
    Re: Class recursion question

    On Wed, 23 Apr 2008 18:38:58 -0700, cbmeeks <cbmeeks@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

    Comment

    • cbmeeks

      #3
      Re: Class recursion question

      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

      Comment

      • cbmeeks

        #4
        Re: Class recursion question

        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.

        Comment

        • Peter Duniho

          #5
          Re: Class recursion question

          On Wed, 23 Apr 2008 19:21:49 -0700, cbmeeks <cbmeeks@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

          Comment

          • cbmeeks

            #6
            Re: Class recursion question

            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!

            Comment

            Working...