title: Insert nodes in expression trees
body: <p>I'm trying to evaluate an expression using a binary tree. The tree has this characteristics:</p>
  
  <ul>
  <li>Each node has zero, one or two children.</li>
  <li><strong>Only</strong> nodes containing operators can have children.</li>
  <li>All leaf nodes <strong>must be</strong> numbers.</li>
  <li>For the sake of simplicity, the only operators allowed are <code>*</code> and <code>+</code></li>
  </ul>
  
  <p>Something like those ones:<br />
  <img src="https://i.stack.imgur.com/rsa6y.png" alt="Expression trees"></p>
  
  <p>This is my tree class:</p>
  
  <pre><code>class ExpressionTree {
      struct Node {
          std::string data;
  
          Node *leftChild, *rightChild;
  
          Node(std::string d): data(d), leftChild(NULL), rightChild(NULL) {}
      } *root;
      uint tsize;
  public:
      ExpressionTree(): root(NULL), tsize(0) {}
      Node* treeRoot() { return root; }
      void insert(std::string s);
  };
  </code></pre>
  
  <p>And this is my insert function:</p>
  
  <pre><code>void insert(std::string s) {
      if (root == NULL) {
          root = new Node(s);
          ++tsize;
      } else {
          Node* current = root;
          while (true) {
              if (is_operator(current-&gt;data)) {
                  if (current-&gt;leftChild == NULL) {
                      current-&gt;leftChild = new Node(s);
                      ++tsize;
                      return;
                  } else if (current-&gt;rightChild == NULL) {
                      current-&gt;rightChild = new Node(s);
                      ++tsize;
                      return;
                  } else {
                      if (is_operator(current-&gt;leftChild-&gt;data)) {
                          current = current-&gt;leftChild;
                          continue;
                      } else if (is_operator(current-&gt;rightChild-&gt;data)) {
                          current = current-&gt;rightChild;
                          continue;
                      } else {
                          std::cout &lt;&lt; "Error: only nodes who hold operators"
                                    &lt;&lt; " can have children." &lt;&lt; std::endl;
                          return;
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem is in this function. I wrote it starting from a function to insert nodes in a <em>Binary Search tree</em>, but it doesn't work. When I run a simple main (that, using <code>insert()</code>, adds the nodes of the second tree one at time) it crashes without returning any error, only a windows 7 dialog who asks to <em>check for an online solution</em>.</p>
  
  <p>I think that the main problem is that it does not inspect all the elements of a tree, but only a branch, and for this reason it appends new nodes in an illegal way. Unfortunately, I can't figure out how to solve this.</p>
  
  <p>I hope that this question is not too much specific.</p>
  
  <p>Note: <code>is_operator()</code> takes a string, and return <code>true</code> if it's <code>+</code> or <code>*</code>, and false otherwise.</p>