使用Trie

时间:2017-04-08 10:45:17

标签: c++ algorithm data-structures trie

我正试图在c ++中制作某种自动完成功能。首先使用Trie并且一旦工作(最重要的是,我知道它是如何工作的)我会尝试使用三元树。但是至于现在,当我添加以不同于Trie中的字符开头的单词时,我会遇到分段错误。

EG。我们添加“abc”,“abcd”和“abcde”这没问题。后来当我想添加(而“abc”等仍然在Trie中)“xfce”,“xfced”时发生分段错误。

我现在已经调试了一段时间,似乎无法找到问题。

我认为这个问题存在于Trie.cpp的某个地方,所以这是我在这里提供的文件。然而,它可能在主要功能中,但我不想被大肆宣传发布到很多代码......

#include "Trie.h"
#include <iostream>

Trie::Trie()
{
    this->root = new Node(false);
}

Trie::~Trie()
{

}

Trie::Node::Node(bool isLeaf)
{
    this->isLeaf = isLeaf;
}

void Trie::insert(const std::string& word)
{
    Node* crawler = this->root;
    int index; 

    for(int i = 0; i < word.length(); ++i)
    {
        index = CHAR_TO_INDEX(word.at(i));

        if(!crawler->children[index])
        {
            crawler->children[index] = new Node(false); 
        }
        crawler = crawler->children[index];
    }
    crawler->isLeaf = true;
}

int Trie::contains(const std::string& word)
{
    int index;
    Node* crawler = this->root;

    for(int i = 0; i < word.length(); ++i)
    {
        index = CHAR_TO_INDEX(word.at(i));

        if(!crawler->children[index])
        {
            return -1; 
        }
        crawler = crawler->children[index];
    }

    return (crawler != NULL && crawler->isLeaf);
}

std::vector<std::string> Trie::possibleSuffixes(std::string& prefix)
{
    Node* crawler = this->root;
    int index;
    std::vector<std::string> result;

    for(int i = 0; i < prefix.length(); ++i)
    {
        index = CHAR_TO_INDEX(prefix.at(i));
        crawler = crawler->children[index];  
    }

    traverse(prefix, crawler, result);

    return result;
}

void Trie::traverse(std::string prefix, Node* node, std::vector<std::string>& v)
{
    if(node->isLeaf)
    {
        v.push_back(prefix); 
    }

    for(int i = 0; i < ALPHABET; ++i)
    {
        if(node->children[i])
        {
            traverse(prefix + (char)('a' + i), node->children[i], v);
        }
    }
}

整个Trie课程:

#ifndef TRIE_H
#define TRIE_H

#include <string>
#include <vector>

#define ARRAYSIZE(a) sizeof(a / sizeof(a[0]))
#define ALPHABET 26
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')

class Trie
{
    private:
        struct Node
        {
            Node(bool isLeaf);
            struct Node *children[ALPHABET];
            bool isLeaf;
        };

        Node *root;
        void traverse(std::string prefix, Node* node, std::vector<std::string>& v);

    public:
        Trie();
        ~Trie();
        int contains(const std::string& word);    //Checks the existance of a specific word in the trie
        void insert(const std::string& word);     //Inserts new word in the trie if not already there
        std::vector<std::string> possibleSuffixes(std::string& prefix);

};

1 个答案:

答案 0 :(得分:1)

虽然你没有提到你的Node课程,但我假设这个 -

class Node {
public:
    bool isLeaf;

    // must be >= 25 as you're inserting lowercase letters
    // assuming your CHAR_TO_INDEX(ch) returns 0 based index 
    // e.g. 'a' => 0, 'b' => 1 ... 'z' => 25
    Node* children[30];

    // default constructor should be like this
    Node(): isLeaf(false) {
          for(int i = 0; i < 26; i++) {
              children[i] = NULL;
          }
    }

    ~Node() {
        for(int i = 0; i < 26; i++) {
            if(children[i]) {
                delete children[i];
                children[i] = NULL;
            }
        }
        delete this;
    }
};

请比较你的Node类/结构是否像这样。