title: C++ Binary Tree Using Vector
body: <p>I am tasked with storing a binary tree within a vector. Within each node is stored an int ID, int Age, and a string name.</p>
  
  <p>The nodes are stored and organized within the vector by ID.</p>
  
  <p>When storing the binary tree within a vector, I am using the algorithm 2i and 2i+1 to dictate a node's left and right child respectively.</p>
  
  <p>I have managed to create an insert method that I believe satisfies these conditions and inserts a node into a vector, however, after attempting to insert these nodes into the vector</p>
  
  <p>50 21 Tim</p>
  
  <p>75 22 Steve</p>
  
  <p>I notice that it is not actually inserting these nodes into the vector.</p>
  
  <p>I placed a line that prints the index after insertion, and I have realized that after the first insertion, index no longer updates.</p>
  
  <p>An example <strong>Running the insert method using this example</strong></p>
  
  <p><a href="https://i.stack.imgur.com/tefxy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tefxy.png" alt="enter image description here"></a></p>
  
  <p>Is there something wrong with my insert() method?</p>
  
  <p>Here is my code.</p>
  
  <pre><code>#include "BinaryTree.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  int index = 0;
  
  struct Node
  {
      int ID;
      int age;
      string name;
  
      Node()
      {
  
      }
  
      Node(int id, int Age, string nm)
      {
          this-&gt;ID = id;
          this-&gt;age = Age;
          this-&gt;name = nm;
      }
  };
  
  vector&lt;Node&gt; binaryTree(30);
  
  
  BST::BST()
  {
  
  }
  
  
  
  void BST::start()
  {
      int choice;
  
  
      cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
      cout &lt;&lt; "1. Add a node to the tree" &lt;&lt; endl;
      cout &lt;&lt; "2. Delete a node from the tree" &lt;&lt; endl;
      cout &lt;&lt; "3. Find a node in the tree" &lt;&lt; endl;
      cout &lt;&lt; "4. Report the contents of the tree" &lt;&lt; endl;
      cout &lt;&lt; "5. Exit program" &lt;&lt; endl;
  
      cin &gt;&gt; choice;
  
      if (choice == 1)
      {
          insert();
      }
  
      if (choice == 2)
      {
          Delete();
      }
  
      if (choice == 3)
      {
          find();
      }
  
      if (choice == 4)
      {
          report();
      }
  
  
  }
  
  
  void BST::insert()
  {
      int ID;
      int AGE;
      string NAME;
      int root = 1;
  
      bool success = false;
      cout &lt;&lt; "Please enter the ID number, age and name:" &lt;&lt; endl;
  
      do
      {
          cin &gt;&gt; ID &gt;&gt; AGE &gt;&gt; NAME;
      } while (ID &lt; 0);
  
      Node *tree = new Node(ID, AGE, NAME);
  
  
      if (index == 0)
      {
          binaryTree[1] = *tree;
      }
  
      if (index &gt; 0)
      {
          do
          {
              if (tree-&gt;ID &gt; binaryTree.at(root).ID)
              {
                  root = 2 * root + 1;
  
              }
  
              if (tree-&gt;ID &lt; binaryTree.at(root).ID)
              {
                  root = 2 * root;
              }
  
              if (binaryTree.at(root).ID == NULL)
              {
                  binaryTree.at(root) = *tree;
                  cout &lt;&lt; "Added!" &lt;&lt; endl;
                  cout &lt;&lt; "Vector size: " &lt;&lt; binaryTree.size() &lt;&lt; endl;
                  success = true;
              }
          } while (!success);
      }
  
      index++;
      cout &lt;&lt; index &lt;&lt; endl;
      delete tree;
  
      start();
  }
  </code></pre>
  
  <p>EDIT: I realized that I failed to check against index, so I changed it from '=' to '==' comparison to start the loop. However, Now I am getting a _Xout_of_range("invalid vector subscript"); exception thrown by the vector class</p>
  
  <p><strong>Here is the error.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/fuxje.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fuxje.png" alt="enter image description here"></a></p>