title: How to print out a binary tree in order?
body: <p>I'm struggling with the printing of a binary tree that I coded. It's a little program where you can type in different states and then it orders them alphabetically in the binary tree and afterwards it should print them out again, in alphabetic order.<br>
  How should I start with the listing function? I read in the internet that a recursive algorithm would be the best, but I don't really know how to do that with my setup.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  int insertInt()
  {
    int x;
    cin&gt;&gt;x;
    while(cin.fail())
      {
        cout&lt;&lt;"Error! Please enter a valid integer: ";
        cin.clear();
        cin.ignore();
        cin&gt;&gt;x;
      }
    return x;
  }//closes insertInt
  
  struct node
  {
    string info;
    node* right;
    node* left;
  }; //closes node
  
  class States
  {
   private:
    node* start;
  
   public:
    void insert();
    void delete();
    void list();
    void search();
    States();
  }; //closes States class
  
  States::States()
  {
    start = new node;
    start -&gt; left = NULL;
    start -&gt; right = NULL;
    start -&gt; info = " ";
  }
  
  void States::insert()
  {
    string state;
    char c;
    node *temp, *p, *s;
    p = start;
    s = start;
    temp = new node;
    temp -&gt;info = state;
  
    cout&lt;&lt;"Please enter the state you want to add: ";
    cin&gt;&gt;state;
  
    if(s -&gt; info == " ")
      {
        s -&gt; info = state;
        cout&lt;&lt;"Added state "&lt;&lt;state&lt;&lt;"to the list.\n";
        cout&lt;&lt;"Ready to continue? (enter y)";
        cin&gt;&gt;c;
        return;
      }//close if
    else
      {
        while(true)
      {
        //moving pointer until next level is empty
  
        if(s-&gt;info &gt; temp-&gt;info &amp;&amp; s-&gt;left != NULL)
          {
            s = s-&gt;left;
            continue;
          }//close if
  
        if(s-&gt;info &lt; temp-&gt;info &amp;&amp; s-&gt;right != NULL)
          {
            s = s-&gt;right;
            continue;
          }//close if
  
        //inserting the new node
  
        if(s-&gt;info &gt; temp-&gt;info &amp;&amp; s-&gt; left == NULL)
          {
            s -&gt; left = temp;
            break;
          }//close if
        if(s-&gt;info &lt; temp-&gt;info &amp;&amp; s-&gt;right == NULL)
          {
            s-&gt;right = temp;
            break;
          }//cloese if
  
      }//close while loop
      }//close else
  
  }//close insert function
  
  void States::list()
  {
    node *p, *s;
    p = start;
    s = start;
  
    if(start-&gt;info == " ")
      cout&lt;&lt;"Nothing to display!\n";
  
    if(s-&gt;left == NULL)
      cout&lt;&lt;"-"&lt;&lt;s-&gt;info&lt;&lt;endl;
  
  }
  
  
  void States::search()
  {
    string state;
    cout&lt;&lt;"Please enter the state you're looking for: ";
    cin&gt;&gt;state;
    node *s, *temp;
    s = start;
    temp = new node;
    temp -&gt;info = state;
  
    while(true)
      {
        if(s-&gt;info == state)
      {
        cout&lt;&lt;"Found your state!\n";
        break;
      }
        if(s-&gt;info &gt; temp-&gt;info)
      {
        if(s-&gt;left == NULL)
          break;
        else
          s = s-&gt;left;
        continue;
      }//close if
        if(s-&gt;info &lt; temp-&gt;info)
      {
        if(s-&gt;right == NULL)
          break;
        else
          s = s-&gt;right;
        continue;
      }//close if
      }//close while loop
  }//close search function
  
  int main()
  {
    States s1;
    int c;
    int exit = 0;
  
    while(exit == 0)
      {
        cout&lt;&lt;"----------------------------------------------------------------------\n";
        cout&lt;&lt;"\t\tChoose one of the following options\n";
        cout&lt;&lt;"\t\t\t\t(1) Add a state\n";
        cout&lt;&lt;"\t\t\t\t(2) List states\n";
        cout&lt;&lt;"\t\t\t\t(3) Search for state\n";
        cout&lt;&lt;"\t\t\t\t(4) Delete state\n";
        cout&lt;&lt;"\t\t\t\t(5) Exit\n";
        cout&lt;&lt;"----------------------------------------------------------------------\n";
        c = insertInt();
  
        switch(c)
      {
      case 1:
        s1.insert();
        break;
      case 2:
        s1.list();
        break;
      case 3:
        s1.search();
        break;
      case 4:
        s1.delete();
        break;
      case 5:
        exit = 1;
        cout&lt;&lt;"Exiting...\n";
        break;
      default:
        cout&lt;&lt;"Error. Please enter a valid integer.\n";
      }//close switch
      }//closes while loop
  
  }//closes main
  </code></pre>