title: Traversing a Tree Structure
body: <p>I am trying to understand how to traverse a tree data structure, and I am having a problem doing it, especially as I am trying to use <code>IEnumerable</code>. I want the tree as a dictionary so I can refer to the nodes by their string names. The tree will hold different class objects but as these objects all implement an interface <code>IMyInterface</code>, type of the tree will be the <code>IMyInterface</code> interface.</p>
  
  <p>I have the tree:</p>
  
  <pre><code>internal class Tree&lt;T&gt;
  {
      private TreeNode&lt;T&gt; root;
  
      internal Tree(T value)
      {
          if (value == null)
          {
              throw new ArgumentNullException(
                  "Cannot use a null value to construct a tree.");
          }
  
          this.root = new TreeNode&lt;T&gt;(value);
      }
  
      internal Tree(T value, params Tree&lt;T&gt;[] children) : this(value)
      {
          foreach (Tree&lt;T&gt; child in children)
          {
              this.root.AddChild(child.root);
          }
      }
  
      internal TreeNode&lt;T&gt; Root
      {
          get { return this.root; }
      }
  
      private void PrintDFS(TreeNode&lt;T&gt; root, int spaces)
      {
          if (spaces &lt; 0)
          {
              throw new ArgumentOutOfRangeException(
                  "The number of spaces used to represent the parent-child relation in a tree must be greater than or equal to zero.");
          }
  
          if (this.root == null)
          {
              return;
          }
  
          StringBuilder sb = new StringBuilder();
          sb.Append(' ', spaces);
          sb.Append(root.Value);
  
          TreeNode&lt;T&gt; child = null;
  
          foreach (Tree&lt;T&gt; child in this.root)     // &lt;--- this generates an error
          {
              PrintDFS(child, spaces);
          }
      }
  
      // Traverses and prints the tree in
      // Depth-First Search (DFS) manner
      internal void TraverseDFS()
      {
          this.PrintDFS(this.root, 0);
      }
  }
  </code></pre>
  
  <p>And my node class is:</p>
  
  <pre><code>internal class TreeNode&lt;T&gt; : IEnumerable&lt;TreeNode&lt;T&gt;&gt;
  {
      private T value;
      private bool hasParent;
  
      private readonly Dictionary&lt;string, TreeNode&lt;T&gt;&gt; children = new Dictionary&lt;string, TreeNode&lt;T&gt;&gt;();
  
      internal TreeNode(T value)
      {
          if (value == null)
          {
              throw new ArgumentNullException(
                  "Cannot insert null values for a tree node!");
          }
  
          this.value = value;
          this.children = new Dictionary&lt;string, TreeNode&lt;T&gt;&gt;();      // dictionary that holds the children of each node
      }
  
      internal T Value
      {
          get { return this.value; }
          set { this.value = value; }
      }
  
      internal int ChildrenCount
      {
          get
          {
              return this.children.Count;
          }
      }
  
      internal void AddChild(TreeNode&lt;T&gt; child)
      {
          if (child == null)
          {
              throw new ArgumentNullException(
                  "Cannot insert null value as child node.");
          }
  
          if (child.hasParent)
          {
              throw new ArgumentException(
                  "The child node already has a parent.");
          }
  
          child.hasParent = true;
          this.children.Add(child.ToString(), child);
      }
  
      internal TreeNode&lt;T&gt; GetChild(string nodeName)
      {
          return this.children[nodeName];
      }
  
      internal IEnumerator&lt;TreeNode&lt;T&gt;&gt; GetEnumerator()
      {
          return this.children.Values.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return this.GetEnumerator();
      }
  
      IEnumerator&lt;TreeNode&lt;T&gt;&gt; IEnumerable&lt;TreeNode&lt;T&gt;&gt;.GetEnumerator()
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>The issue seems to be the code:</p>
  
  <pre><code>foreach (Tree&lt;T&gt; child in this.root)     // &lt;--- this generates an error
  {
      PrintDFS(child, spaces);
  }
  </code></pre>
  
  <p>(Code snippet from the Tree class) Any suggestions would be greatly appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I get the error messages:</p>
  
  <p>Error   669 A local variable named 'child' cannot be declared in this scope because it would give a different meaning to 'child', which is already used in a 'parent or current' scope to denote something else.</p>
  
  <p>Error   672 Cannot convert type <code>TreeNode&lt;T&gt;</code> to <code>Tree&lt;T&gt;</code></p>
  
  <p>And the warning message:</p>
  
  <p>Warning 668 <code>TreeNode&lt;T&gt;</code> does not implement the 'collection' pattern. <code>TreeNode&lt;T&gt;.GetEnumerator()</code> is either static or not public.</p>