title: Binary search tree segmentation fault
body: <p>This program is meant to take strings from an input file, sort them alphabetically to make a signature, then insert the signature as a key for a BST node. The word the signature was created from is then stored in a vector of strings linked to the key. Any words afterwards who's signature is the same is then pushed back onto the same vector, etc. I am getting a segmentation fault, will show exact location below.</p>
  
  <p>BST.h</p>
  
  <pre><code>#ifndef BST_H
  #define BST_H
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Node;
  
  class BST {
    private:
     class Node {
       public:
        string key;
        Node *left, *right;
        vector&lt;string&gt; data;
       Node(string k, Node *l, Node *r, vector&lt;string&gt; d) : key{k}, left{l}, right{r}, data{d} {};
     };
     Node *root;
     void traverse(void (*f)(const string&amp; key, vector&lt;string&gt;&amp; value), Node* root);
    public:
     BST();
     ~BST();
     Node* find(Node* root, const string&amp; key);
     void insert(Node *&amp;root, const string&amp; key);
     vector&lt;string&gt;&amp; operator[](const string&amp; key);
     void traverse(void (*f)(const string&amp; key, vector&lt;string&gt;&amp; value));
  };
  
  #endif
  </code></pre>
  
  <p>BST.cc</p>
  
  <pre><code>#include "BST.h"
  
  BST::BST()
  {
     root = nullptr;
  }
  
  BST::~BST()
  {
     delete root;
  }
  
  BST::Node* BST::find(Node* root, const string&amp; key)
  {
     if(!root) return nullptr;
     if(root-&gt;key == key) return root;
     else if(root-&gt;key &gt; key) return BST::find(root-&gt;left, key);
     else return BST::find(root-&gt;right, key);
  }
  
  void BST::insert(Node *&amp;root,const string&amp; key)
  {
     if(!root)
     {
        vector&lt;string&gt; data;
        root=new Node(key, nullptr, nullptr, data);
     }
     else if(root-&gt;key &gt;= key) BST::insert(root-&gt;left, key);
     else BST::insert(root-&gt;right, key);
  }
  
  vector&lt;string&gt;&amp; BST::operator[](const string&amp; key)
  {
     Node* temp=BST::find(root, key);
     if(temp!=nullptr)
     {
        return temp-&gt;data;
     }
     else
     {
        BST::insert(root, key);
        return (BST::find(root, key))-&gt;data;
     }
  }
  </code></pre>
  
  <p>The following 2 member functions are what gives the segmentation fault</p>
  
  <pre><code>void BST::traverse(void (*f)(const string&amp; key, vector&lt;string&gt;&amp; value))
  {
     Node* tRoot=root;
     if(tRoot)
        traverse(*f, tRoot);
  }
  
  
  void BST::traverse(void (*f)(const string&amp; key, vector&lt;string&gt;&amp; value), Node* root)
  {
     string&amp; key=root-&gt;key;
     vector&lt;string&gt; value(root-&gt;data);
     if(root)
     {
        traverse(*f, root-&gt;left);
        f(key, value);
        traverse(*f, root-&gt;right);
     }
  }
  </code></pre>
  
  <p>Main program</p>
  
  <pre><code>#include "BST.h"
  #include &lt;algorithm&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  // Computes the signature of the string, which is the original string                                                                                                                                                                                                         
  // arranged in alphabetical order.                                                                                                                                                                                                                                            
  //                                                                                                                                                                                                                                                                            
  // Assumes that the string w consists of only upper case letters.                                                                                                                                                                                                             
  string signature(const string&amp; w);
  
  // prints all the anagrams in the BST                                                                                                                                                                                                                                         
  void printAnagrams(const string&amp; key, vector&lt;string&gt;&amp; value);
  
  int main(void)
  {
    string w, s;
    BST signatureList;
    vector&lt;string&gt; temp;
    ifstream myfile;
    myfile.open("words.txt");
  
    //read all words                                                                                                                                                                                                                                                            
    while(getline(myfile, w)) {
       // compute signature and store it into the list                                                                                                                                                                                                                          
       s = signature(w);
       temp = signatureList[s];
       temp.push_back(w);
    }
  
    myfile.close();
  
    // print the results
    //this call specifically gives the seg fault                                                                                                                                                                                                                                                 
     signatureList.traverse(*printAnagrams);
  
    return 0;
  }
  
  // Computes the signature of the string, which is the original string                                                                                                                                                                                                         
  // arranged in alphabetical order.                                                                                                                                                                                                                                            
  //                                                                                                                                                                                                                                                                            
  // Assumes that the string w consists of only upper case letters.                                                                                                                                                                                                             
  string signature(const string&amp; w)
  {
    string s = w;
    sort(s.begin(), s.end());
    return s;
  }
  
  // prints all the anagrams in the BST                                                                                                                                                                                                                                        
  void printAnagrams(const string&amp; key, vector&lt;string&gt;&amp; value)
  {
     cout &lt;&lt; key &lt;&lt; endl;
     for(string s : value)
         cout &lt;&lt; s &lt;&lt; ' ';
     cout &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Thank you for any help, cant get valgrind working on this. From what I can tell there are no questions explicitly related to my issue, I apologise if thats not the case.</p>