简体   繁体   中英

Error Executing Trie Tree

i want to insert a string and make a search opeartion using trie data structure.This is my first implementation using pointers so i am really confused what i am doing wrong in the code,It is giving compilation error.please help to debug it and please tell me what is wrong in my pointer logic.

typedef struct trie {
    unordered_multimap<char, struct trie> child;
    bool isEnd;
} trie;
trie* newtrienode()
{
    trie* newnode = (trie*)malloc(sizeof(trie));
    newnode->isEnd = false;
    return newnode;
}
trie* root = newtrienode();
void insert(string word)
{
    trie* current = root;
    for (int i = 0; i < word.length(); i++) {
        char ch = word[i];
        trie* node = current->child[ch];
        if (node == NULL) {
            trie* node = newtrienode();
            current->child.insert(pair<char, trie>(ch, node));
        }
        current = node;
    }
    current->isEnd = true;
}
bool search(string word)
{
    trie* current = root;
    for (int i = 0; i < word.length(); i++) {
        char ch = word[i];
        trie* node = current->child[ch];
        if (node == NULL) {
            return false;
        }
        current = node;
    }
    return true;
}

Apart from what have been mentioned in comments, I see a few problems with the code.

1.

trie* newnode = (trie*)malloc(sizeof(trie));

This doesn't create an object of type trie , it only allocates a block of memory the size of trie and assigns the pointer to it to the variable newnode . In particular, it doesn't call the constructor of unordered_multimap . Any access to the child member via that pointer produces undefined behavior. Besides, you never free that memory.

2.

typedef struct trie {
    unordered_multimap<char, struct trie> child;
    bool isEnd;
} trie;

At the second line you are declaring a unordered_multimap data member with an incomplete type trie as the second template parameter. Some compilers allow that, but the standard doesn't require them to. shared_ptr , on the other hand, is required to be usable with an incomplete type as the template parameter. Also, a node can only have one subtree per character, so you don't need a multimap; a map will suffice. So I'd suggest using unordered_map<char, shared_ptr<trie>> and replacing all occurrences of trie* with shared_ptr<trie> . It will also take care of deleting the objects once root is released.

The newtrienode() function will look like this:

shared_ptr<trie> newtrienode()
{
    shared_ptr<trie> newnode (new trie());
    newnode->isEnd = false;
    return newnode;
}

3.

trie* node = current->child[ch];
if (node == NULL) {

operator[] doesn't return NULL when the key doesn't exist. It inserts a default-constructed object into the container and returns the reference to it. In order to check whether a key exist, use find or count .

4.

trie* node = current->child[ch];
if (node == NULL) {
    trie* node = newtrienode();
    current->child.insert(pair<char, trie>(ch, node));
}
current = node;

Note that in the third line you're declaring a new variable. The variable node declared on the first line doesn't get changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM