Generic Tree Structure - How to populate an org chart
<p>I have what should be a fairly trivial problem but I want to make sure I'm doing this in the most "elegant" way possible in .Net 4.5 and I'd like some opinions from people smarter than me.</p>

<p>I have a class that represents a generic tree structure as such:</p>

<pre><code>   public class TreeNode&lt;T&gt;
    {
        List&lt;TreeNode&lt;T&gt;&gt; Children;

        T Item {get;set;}

        public TreeNode (T item)
        {
            Item = item;
        }

        public TreeNode&lt;T&gt; AddChild(T item)
        {
            TreeNode&lt;T&gt; nodeItem = new TreeNode&lt;T&gt;(item);
            Children.Add(nodeItem);
            return nodeItem;
        }
    }
</code></pre>

<p>Now, I have a Person class that represents an employee of the organization.  Each <code>Person</code> object has an <code>ID</code> and a <code>BossID</code> that points to their superior.</p>

<p>Multiple employees can have the same boss, hence why I am trying to create an organization chart with this tree structure.</p>

<p>The top node is going to be the <code>Person</code> object where <code>BossID</code> is null (it's an <code>int?</code>).  That I can quickly get with LINQ.</p>

<p>It's the next step that is puzzling me a bit.  There are multiple approaches but they seem somewhat sloppy to me and I know there must be a much easier/elegant way to fill in the rest of the org chart.</p>

<p>So right now I have a generic object of <code>List&lt;Person&gt;</code> holding all the employees, with their various <code>BossID</code>s and this generic tree structure that I can add child nodes to.</p>

<p>It's all very basic, but what is the proper sequence to fill the tree?  Am I recursively supposed to iterate down the line?  I know there is backtracking involved here, which is where I'm getting stumped.</p>

<p>I apologize, my background is not in Computer Science, and if it was I realize that tree structures, linked lists, and everything else is trivial stuff.  But this is my first attempt and I want to see how it's done properly.</p>

<p>I appreciate any guidance.</p>