title: how to build a tree structure in C++ using std::map
body: <p>I am trying to write a tree sort of structure in C++. As in every tree there are branches and leaves. A branch can contain other branches as well as leaves. 
  Now my implementation calls for each branch and leaf to have different functionalities. So for example.
  Take the tree structure</p>
  
  <pre><code>                                          Root                                             
                                  |                          |                             
                               Branch1                     Branch2                     Branch3
        |                |                |
      Leaf1            Leaf2           Branch4
  </code></pre>
  
  <blockquote>
    <p>Now Each Leaf and branch has a different function to execute so Leaf1 will have a function called leaf1_func, Leaf2 will have leaf2_func, Branch4 has Branch4_func. </p>
  </blockquote>
  
  <p>I was initially trying to implement composite design pattern. But that means I would have as many classes as leafs. But since I have tons of leaves and branches I would like to avoid creates more classes. I realize this is an unusual situation but was hoping somebody could help me in this regard. What would be the best way to implement this tree without creating too many classes.</p>
  
  <p>i am using map STL container to store datas as well, i want to use this tree implementation to solve this in TSP problem.</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  int n=4;
  int min=1, max=10;
  
  
  
  
  struct graph
  {
  int nodes;//total no. of nodes or vertices namely cities
  std::map&lt;std::pair&lt;int,int&gt;, int&gt; graphMap;//an object that links a pair of vertices   
  };
  
  
  void directed_Graph(graph);
  
  void directed_Graph(graph G)
  {
  //int n = G-&gt;nodes; //city count
  int i, j;
  for(i = 0; i &lt;= n-1; i++)
  {
      for(j = 0; j &lt;= n-1; j++)
      {
          if(i!=j)
          {
              G.graphMap[std::make_pair(i,j)] = (rand()%10)+1;
              //cout&lt;&lt;G.graphMap[std::make_pair(i,j)]&lt;&lt;"\n";
          }
  
          else
          {
              G.graphMap[std::make_pair(i,j)] = 0;
          }
  
      }
  }
  }
  
  
  int main(int argc, char** argv) 
  {
  graph g;
  g.nodes = 4;
  
  directed_Graph(g);
  
  return 0;
  }
  </code></pre>