title: Traversing a binary tree using boolean logic
body: <p>Let me start off by saying sorry if the title is wrong.</p>
  
  <p>I am being taught Binary Tree Traversal I've been given some of the code, I am really struggling to understand the logic in using the if/else statements and the boolean logic.</p>
  
  <p>I have to traverse the tree using postorder, preorder and inorder methods.</p>
  
  <p>The preorderTraversal method is all ready done and working.</p>
  
  <p>Any suggestions would be appreciated.</p>
  
  <p>The Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace BinaryTree
  {
  class BinaryTree&lt;T&gt;
  {
     class BinaryTreeNode
      {
          public BinaryTreeNode Left;
          public BinaryTreeNode Right;
          public BinaryTreeNode Parent;
          public T Data;
          public Boolean processed;
  
          public BinaryTreeNode()
          {
              Left = null;
              Right = null;
              Parent = null;
              processed = false;
          }
      }
  
      BinaryTreeNode Root;
      Comparison&lt;T&gt; CompareFunction;
  
      public BinaryTree(Comparison&lt;T&gt; theCompareFunction)
      {
          Root = null;
          CompareFunction = theCompareFunction;
      }
  
      public static int CompareFunction_Int(int left, int right)
      {
          return left - right;
      }
  
      public void preorderTraversal()
      {
          if (Root != null)
          {
              BinaryTreeNode currentNode = Root;
              Boolean process = true;
              int nodesProcessed = 0;
              while (nodesProcessed != 11)
              {
                  if (process)
                  {
                      Console.Write(currentNode.Data.ToString());
                      currentNode.processed = true;
                      nodesProcessed = nodesProcessed +1;
                  }
                  process = true;
  
                  if (currentNode.Left != null &amp;&amp; currentNode.Left.processed == false)
                  {
                      currentNode = currentNode.Left;
                  }
                  else if (currentNode.Right != null &amp;&amp; currentNode.Right.processed ==            
                  false)
                  {
                      currentNode = currentNode.Right;
                  }
                  else if (currentNode.Parent != null)
                  {
                      currentNode = currentNode.Parent;
                      process = false;
                  }
              }
          }
          else
          {
              Console.WriteLine("There is no tree to process");
          }
      }
  
      public void postorderTraversal()
      {
          if (Root != null)
          {
          }
          else
          {
              Console.WriteLine("There is no tree to process");
          }
      }
      public void inorderTraversal()
      {
          if (Root != null)
          {
          }
          else
          {
              Console.WriteLine("There is no tree to process");
          }
      }        
       public static int CompareFunction_String(string left, string right)
      {
          return left.CompareTo(right);
      }
  
      public void Add(T Value)
      {
          BinaryTreeNode child = new BinaryTreeNode();
          child.Data = Value;
  
          if (Root == null)
          {
              Root = child;
          }
          else
          {
  
              BinaryTreeNode Iterator = Root;
              while (true)
              {
  
                  int Compare = CompareFunction(Value, Iterator.Data);
  
                  if (Compare &lt;= 0)
                      if (Iterator.Left != null)
                      {
  
                          Iterator = Iterator.Left;
                          continue;
                      }
                      else
                      {
  
                          Iterator.Left = child;
                          child.Parent = Iterator;
                          break;
                      }
  
                  if (Compare &gt; 0)
                      if (Iterator.Right != null)
                      {
  
                          Iterator = Iterator.Right;
                          continue;
                      }
                      else
                      {
  
                          Iterator.Right = child;
                          child.Parent = Iterator;
                          break;
                      }
              }
          }
      }
  
      public bool Find(T Value)
      {
          BinaryTreeNode Iterator = Root;
          while (Iterator != null)
          {
              int Compare = CompareFunction(Value, Iterator.Data);
  
              if (Compare == 0) return true;
              if (Compare &lt; 0)
              {
  
                  Iterator = Iterator.Left;
                  continue;
              }
  
              Iterator = Iterator.Right;
          }
          return false;
      }
  
      BinaryTreeNode FindMostLeft(BinaryTreeNode start)
      {
          BinaryTreeNode node = start;
          while (true)
          {
              if (node.Left != null)
              {
                  node = node.Left;
                  continue;
              }
              break;
          }
          return node;
      }
  
  
      public IEnumerator&lt;T&gt; GetEnumerator()
      {
          return new BinaryTreeEnumerator(this);
      }
  
  
  
      class BinaryTreeEnumerator : IEnumerator&lt;T&gt;
      {
          BinaryTreeNode current;
          BinaryTree&lt;T&gt; theTree;
          public BinaryTreeEnumerator(BinaryTree&lt;T&gt; tree)
          {
              theTree = tree;
              current = null;
          }
  
  
          public bool MoveNext()
          {
  
              if (current == null)
                  current = theTree.FindMostLeft(theTree.Root);
              else
              {
  
                  if (current.Right != null)
                      current = theTree.FindMostLeft(current.Right);
                  else
                  {
  
                      T CurrentValue = current.Data;
  
                      while (current != null)
                      {
                          current = current.Parent;
                          if (current != null)
                          {
                              int Compare = theTree.CompareFunction(current.Data,                    
  CurrentValue);                            
                           if (Compare &lt; 0) continue;
                          }
                          break;
                      }
                  }
              }
              return (current != null);
          }
  
          public T Current
          {
              get
              {
                  if (current == null)
                      throw new InvalidOperationException();
                  return current.Data;
              }
          }
  
          object System.Collections.IEnumerator.Current
          {
              get
              {
                  if (current == null)
                      throw new InvalidOperationException();
                  return current.Data;
              }
          }
  
          public void Dispose() { }
  
          public void Reset() { current = null; }
  
      }
  }
  
  class TreeTest
  {
      static BinaryTree&lt;int&gt; Test = new BinaryTree&lt;int&gt;                                                                
  (BinaryTree&lt;int&gt;.CompareFunction_Int);
      static void Main(string[] args)
      {
  
          // Build the tree
          Test.Add(5);
          Test.Add(2);
          Test.Add(1);
          Test.Add(3);
          Test.Add(3); // Duplicates are OK
          Test.Add(4);
          Test.Add(6);
          Test.Add(10);
          Test.Add(7);
          Test.Add(8);
          Test.Add(9);
          // Test if we can find values in the tree
  
          for (int Lp = 1; Lp &lt;= 10; Lp++)
  
              Console.WriteLine("Find ({0}) = {1}", Lp, Test.Find(Lp));
  
          // Test if we can find a non-existing value
          Console.WriteLine("Find (999) = {0}", Test.Find(999));
  
          // Iterate over all members in the tree -- values are returned in sorted order
          foreach (int value in Test)
          {
              Console.WriteLine("Value: {0}", value);
          }
  
          Console.WriteLine("Preorder traversal");
          Test.preorderTraversal();
          Console.ReadKey();
      }
  
  }
  
  }
  </code></pre>