title: push_back() binary tree into vector
body: <p>I'm trying to put all the elements from a binary search tree into a vector, in order. Here is the function:</p>
  
  <p>edit: adding instructions for clarity.
  Write a class for implementing a simple binary search tree capable of storing numbers. The class should have member functions:</p>
  
  <pre><code>void insert(double x)
  bool search(double x)
  void inorder(vector &lt;double&gt; &amp; v)
  </code></pre>
  
  <p>The insert function should not use recursion directly or indirectly by calling a recursive function. The search function should work by calling a private recursive member function</p>
  
  <pre><code>bool search(double x, &lt;double&gt; &amp; v )
  </code></pre>
  
  <p>The inorder function is passed an initially empty vector v; if fills v with the inorder list of numbers stored in the binary search tree. Demonstrate the operation of the class using a suitable driver program.</p>
  
  <p>EDIT: Added full code for clarity.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class BinaryTree {
  
  private:
  struct TreeNode {
  
      double value;
      TreeNode *left;
      TreeNode *right;
      TreeNode(double value1,
          TreeNode *left1 = nullptr,
          TreeNode *right1 = nullptr) {
  
          value = value1;
          left = left1;
          right = right1;
      }
  };
  
  TreeNode *root;    //pointer to the root of the tree
  bool search(double x, TreeNode *t) {
  
      while (t) {
          std::cout &lt;&lt; "running through t." &lt;&lt; std::endl;
          if (t-&gt;value == x) {
              return true;
          }
          else if (x &lt; t-&gt;value) {
              std::cout &lt;&lt; "wasn't found, moving left." &lt;&lt; std::endl;
              search(x, t-&gt;left);
          }
          else {
              std::cout &lt;&lt; "wasn't found, moving right." &lt;&lt; std::endl;
              search(x, t-&gt;right);
          }
      }
      std::cout &lt;&lt; "wasn't found." &lt;&lt; std::endl;
      return false;
  }
  
  
  public:
  
  std::vector&lt;TreeNode&gt; v;
  
  BinaryTree() {
      root = nullptr;
  }
  
  void insert(double x) {
      TreeNode *tree = root;
  
      if (!tree) {
          std::cout &lt;&lt; "Creating tree." &lt;&lt; x &lt;&lt; std::endl;
          root = new TreeNode(x);
          return;
      }
  
      while (tree) {
  
          std::cout &lt;&lt; "Adding next value." &lt;&lt; std::endl;
          if (tree-&gt;value == x) return;
  
          if (x &lt; tree-&gt;value) {
              tree = tree-&gt;left;
              tree-&gt;value = x;
          }
          else {
              tree = tree-&gt;right;
              tree-&gt;value = x;
          }
      }
  
  }
  bool search(double x) {
  
      return search(x, root);
  }
  
  void inOrder(std::vector &lt;double&gt; &amp; v) {
  
      {
          if (left)
              left-&gt;inOrder(v);
          v.push_back(value);
          if (right)
              right-&gt;inOrder(v);
      }
  }
  TreeNode* left = nullptr;
  TreeNode* right = nullptr;
  double value;
  };
  
  int main() {
  
      BinaryTree t;
  
      std::cout &lt;&lt; "Inserting the numbers 5, 8, 3, 12, and 9." &lt;&lt; std::endl;
      t.insert(5);
      t.insert(8);
      t.insert(3);
      t.insert(12);
      t.insert(9);
  
      std::cout &lt;&lt; "Looking for 12 in tree." &lt;&lt; std::endl;
      if (t.search(12)) {
          std::cout &lt;&lt; "12 was found." &lt;&lt; std::endl;
      }
  
      std::cout &lt;&lt; "Here are the numbers in order." &lt;&lt; std::endl;
  
  
      return 0;
  }
  </code></pre>
  
  <p>I'm unable to get the values to push into the vector. Any ideas as to how I can accomplish this?</p>