title: Binary Search Tree Traversal - PreOrder
body: <p>I m trying to implement Tree Traversal PreOrder using yield return which returns an IEnumerable</p>
  
  <pre><code>private IEnumerable&lt;T&gt; Preorder(Node&lt;T&gt; node)
  {
  
      while(node != null)
      {
          yield return node.Data;
          yield return node.LeftChild.Data;
          yield return node.RightChild.Data;
      }
  
  }
  </code></pre>
  
  <p>In this case, it goes into infinite loop and yes I know that I need to keep traversing. How can this be done?</p>
  
  <p>If the LeftChild or RightChild is null, throws a null exception. I think at that point i need yield break;</p>
  
  <p>I assume, inorder and postorder would be similar too, any ideas?</p>
  
  <p>I have the Resursive version, that works well.</p>
  
  <pre><code>public void PreOrderTraversal(Node&lt;T&gt; node)
  {
  
      if(node!=null)
      {
          Console.Write(node.Data);
      }
      if (node.LeftChild != null)
      {
          PreOrderTraversal(node.LeftChild);
      }
  
      if (node.RightChild != null)
      {
          PreOrderTraversal(node.RightChild);
      }
  }
  </code></pre>
  
  <p>Thanks.</p>