C# 2.0 Generic Tree containing multiple types
<p>I'm having a tricky time in thrashing out how best to approach this particular problem and would really like some guidance from the expert community!</p>

<p>Let's say I have 3 classes</p>

<pre><code>Branch
Twig
Leaf
</code></pre>

<p>A <code>Branch</code> can contain a collection of other <code>Branch</code> objects, but can also contain a collection of <code>Twig</code> objects and a collection of <code>Leaf</code> objects.</p>

<p>A <code>Twig</code> can contain a collection of other <code>Twig</code> objects, but can also contain a collection of <code>Leaf</code> objects.</p>

<p>A <code>Leaf</code> object can be considered a base component and contains no collections of any other objects.</p>

<p>Hence, the possible stuctures are</p>

<p><strong>Branch</strong> </p>

<pre><code>Branch                Branch                  Branch
|                     |                       |
|_Branch              |_Twig                  |_Leaf
|  |_etc...           |  |_etc...
|                     |
|_Twig                |_Leaf
|  |_etc...
|
|_Leaf
</code></pre>

<p><strong>Twig</strong></p>

<pre><code>Twig                  Twig
|                     |
|_Twig                |_Leaf
|  |_etc...
|
|_Leaf
</code></pre>

<p><strong>Leaf</strong></p>

<pre><code>Leaf
</code></pre>

<p>Given a <code>Branch</code>, I would like to be able to interrogate any descendent <code>Branch</code>, <code>Twig</code>, or <code>Leaf</code> objects and know</p>

<ol>
<li><p>The ratio that each descendent object contributes to the top object (the ratio is contextual to the parent and child object in each case).</p></li>
<li><p>If the descendent objects of a particular type contain a particular property value. For example, Check descendent <code>Leaf</code> objects to see if more than 4 have <code>UnderSide</code> property value <code>"Furry"</code></p></li>
</ol>

<p>I would also like to be able to have the options to </p>

<p>3.enumerate through the objects a descendent level at a time i.e. an ordered list of all first children, followed by all second children, etc (the order of objects in each level does not matter).</p>

<ol>
<li>enumerate through the objects breadth-wise i.e. an ordered list of first child, and if first child has children then first child's children, then if that object has children then it's children ....then second child, etc...</li>
</ol>

<p>My first thought is to use a generic tree and interface polymorphism, but I'm having difficulty in working out the details - <code>Twig</code> and <code>Leaf</code> objects share some common properties but <code>Branch</code> objects are very different. If I was dealing with only one type of object it would be very straightforward! Can this be done in a type-safe manner?</p>

<p>Any help would be greatly appreciated.</p>
