簡體   English   中英

使用向量C ++實現Trie的分段錯誤

[英]Segmentation fault in trie implementation using vector c++

#include <iostream>
#include <vector>
#include <string>

using namespace std;

#define LOWERCASE_ALPHABET_SiZE 26

typedef int (*ptr) (char );

inline int charToIndex(char a) { return a - 'a'; };

class trienode
{
    private:
        vector< trienode* > child;
        bool leaf;
    public:
        trienode(int );
        ~trienode();
        void initialiseChild(int i);
        trienode* getChild(int i);
        void setLeaf() { leaf = true; };
        bool isLeaf() { return leaf; };
};

trienode::trienode(int size)
{
    for(int i = 0; i < size; i++)
    {
        child.push_back(NULL);
    }
    leaf = false;
}

trienode::~trienode()
{
    for(int i = 0; i < child.size(); i++)
    {
        delete child.at(i);
        child.at(i) = NULL;
    }
}

void trienode::initialiseChild(int i)
{
    child.at(i) = new trienode(child.size());
}

trienode* trienode::getChild(int i)
{
    return child.at(i);
}

class trie
{
    private:
        trienode* root;
        ptr toIndex;
    public:
        trie(int , ptr );
        ~trie();
        void insert(const string& ref);
        bool search(const string& ref);
};

trie::trie(int size, ptr toIndex) : toIndex(toIndex), root(new trienode(size)) { }

trie::~trie()
{
    cout << "In destructor trie" << endl;
    delete root;
    root = NULL;
}

void trie::insert(const string& ref)
{
    int size = ref.size();
    trienode* root = root;
    for(int i = 0; i < size; i++)
    {
        int index = toIndex(ref[i]);
        if(root->getChild(index) == NULL) // crashing in getChild()
        {
            root->initialiseChild(index);
        }
        root = root->getChild(index);
    }
    root->setLeaf();
}

bool trie::search(const string& ref)
{
    trienode* root = root;
    int size = ref.size();
    for(int i = 0; i < size && root != NULL; i++)
    {
        int index = toIndex(ref[i]);
        if((root = root->getChild(index)) == NULL)
        {
            break;
        }
    }
    return (root != NULL && root->isLeaf());
}

int main(int argc,char* argv[])
{
    trie* altrie = new trie(LOWERCASE_ALPHABET_SiZE, charToIndex);
    int n;
    string temp;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> temp;
        altrie->insert(temp);
    }
    int k;
    for(int i = 0; i < k; i++)
    {
        cin >> temp;
        if(altrie->search(temp))
        {
            cout << temp << " exists in the trie" << endl;
        }
        else
        {
            cout << temp << " doesn`t exist in the trie" << endl;
        }
    }
    return 0;
}

我通過提供每個級別和功能指針中沒有的子代來創建Trie,以將給定字符轉換為索引。 之后,我創建了trie的根節點,當我插入第一個字符串時,它在getChild函數中遇到了Segmentation Fault

首先,第一件事向我解釋了崩潰的原因。 向我解釋我如何可以改善trie的實現。

您正在為成員變量和局部變量使用相同的名稱,如下所示:

trienode* root = root;

編譯器無法告訴本地根和trie :: root之間的區別,因此您將其分配給自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM