Ошибка сегментации в реализации tree C++ с использованием map+

Я пытаюсь сделать функцию вставки для вставки строки в дереве Trie. Я использую это для преобразования недетерминированной грамматики в детерминированную грамматику, используя левый метод факторизации. В каждом узле у меня есть карта. Где-то я получаю ошибку сегментации. Первое время использую карту, поэтому не могу понять это. Заранее спасибо.

struct trie_node
{
map<char,trie_node*> char_list;
bool end_of_word;
};

trie_node* init(){

trie_node *root = (trie_node*)malloc(sizeof(trie_node));
root->end_of_word=true;
return root;

}

trie_node* new_node(){

trie_node *temp = (trie_node*)malloc(sizeof(trie_node));
temp->end_of_word = true;
return temp;

}

void insert_in_trie(trie_node *root,string s){
int n = s.length();
    cout<<"bkjb";

trie_node *current = root;
for(int i=0;i<n;i++){
    map<char,trie_node*>::iterator it = current->char_list.begin();
    it = current->char_list.find(s[i]);
    if(it!=current->char_list.end()){
        current = it->second;
    }
    else{

        cout<<"khjkk";
        //make end_of_word false
        current->end_of_word = false;
        pair<char,trie_node*> x;
        x.first = s[i];
        x.second = new_node();
        current->char_list.insert(x);
        //update current
        current = x.second;

    }
}
}

void print_trie(){
cout<<"\n function print_trie"<<endl;
}

int main(){
string s[4] = {"aBcD","aBcDSe","aBcDFz","part"};
int n = sizeof(s)/sizeof(string);
//cout<<n<<endl;

trie_node *root = init();

insert_in_trie(root,"aBcDFz");

//printing trie tree 
print_trie();
return 0;


}

0 ответов

Другие вопросы по тегам