title: C# Display a Binary Search Tree in Console
body: <p>I have simple binary search tree </p>
  
  <pre><code>public class BNode
  {
      public int item;
      public BNode right;
      public BNode left;
  
      public BNode(int item)
      {
          this.item = item;
      }
  }
  
  public class BTree
  {
      private BNode _root;
      private int _count;
      private IComparer&lt;int&gt; _comparer = Comparer&lt;int&gt;.Default;
  
  
      public BTree()
      {
          _root = null;
          _count = 0;
      }
  
  
      public bool Add(int Item)
      {
          if (_root == null)
          {
              _root = new BNode(Item);
              _count++;
              return true;
          }
          else
          {
              return Add_Sub(_root, Item);
          }
      }
  
      private bool Add_Sub(BNode Node, int Item)
      {
          if (_comparer.Compare(Node.item, Item) &lt; 0)
          {
              if (Node.right == null)
              {
                  Node.right = new BNode(Item);
                  _count++;
                  return true;
              }
              else
              {
                  return Add_Sub(Node.right, Item);
              }
          }
          else if (_comparer.Compare(Node.item, Item) &gt; 0)
          {
              if (Node.left == null)
              {
                  Node.left = new BNode(Item);
                  _count++;
                  return true;
              }
              else
              {
                  return Add_Sub(Node.left, Item);
              }
          }
          else
          {
              return false;
          }
      }
  
      public void Print()
      {
          Print(_root, 4);
      }
  
      public void Print(BNode p, int padding)
      {
          if (p != null)
          {
              if (p.right != null)
              {
                  Print(p.right, padding + 4);
              }
              if (padding &gt; 0)
              {
                  Console.Write(" ".PadLeft(padding));
              }
              if (p.right != null)
              {
                  Console.Write("/\n");
                  Console.Write(" ".PadLeft(padding));
              }
              Console.Write(p.item.ToString() + "\n ");
              if (p.left != null)
              {
                  Console.Write(" ".PadLeft(padding) + "\\\n");
                  Print(p.left, padding + 4);
              }
          }
      }
  }
  </code></pre>
  
  <p>where I can insert values like</p>
  
  <pre><code>BTree btr = new BTree();
  btr.Add(6);
  btr.Add(2);
  btr.Add(3);
  btr.Add(11);
  btr.Add(30);
  btr.Add(9);
  btr.Add(13);
  btr.Add(18);
  </code></pre>
  
  <p>I want to display my tree within my console application. I have a <code>btr.Print();</code> which displays my tree from left to right (<code>6</code> is the root) - but I'm not happy with it. </p>
  
  <p><a href="https://i.stack.imgur.com/mK9NW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mK9NW.png" alt="enter image description here"></a></p>
  
  <p><strong>Question</strong>: Is there a better way to display this tree within a console application? Even a improvement of this <code>Print()</code> would help me.</p>