修复trie c+中的分段错误+; 我使用TIE实现来存储和搜索C++编程语言中的单词。在使用search()函数时,我在搜索特定单词时遇到了一个分段错误。检查结构是否为空时似乎发生了错误

修复trie c+中的分段错误+; 我使用TIE实现来存储和搜索C++编程语言中的单词。在使用search()函数时,我在搜索特定单词时遇到了一个分段错误。检查结构是否为空时似乎发生了错误,c++,trie,C++,Trie,以下是错误消息: Program received signal SIGSEGV, Segmentation fault. 0x000055555555b2ff in search (this=0x55555577ee70, wordlist=0x55555577ef00, word="a1g6os") at test.cc:30 if (!pCrawl->children[index]) 以下是源代码: #include <bits/stdc++.h&g

以下是错误消息:

Program received signal SIGSEGV, Segmentation fault.
0x000055555555b2ff in search (this=0x55555577ee70, 
wordlist=0x55555577ef00, word="a1g6os") at test.cc:30
            if (!pCrawl->children[index])
以下是源代码:

#include <bits/stdc++.h> 
using namespace std; 
const int ALPHABET_SIZE = 26; 

struct TrieNode { 
struct TrieNode *children[ALPHABET_SIZE]; 

bool isEndOfWord; 
}; 



struct TrieNode *getNode(void) { 
    struct TrieNode *pNode =  new TrieNode; 
     pNode->isEndOfWord = false; 

    for (int i = 0; i < ALPHABET_SIZE; i++) 
        pNode->children[i] = NULL; 

    return pNode; 
} 



void insert(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - 'a'; 
        if (!pCrawl->children[index]) 
            pCrawl->children[index] = getNode(); 

        pCrawl = pCrawl->children[index]; 
   } 

   // mark last node as leaf 
   pCrawl->isEndOfWord = true; 
} 

// Returns true if key presents in trie, else 
// false 
bool search(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - 'a'; 
        if (!pCrawl->children[index]) 
             return false; 

        pCrawl = pCrawl->children[index]; 
    } 

    return (pCrawl != NULL && pCrawl->isEndOfWord); 
} 

int main() { 
    string keys[] = {"the", "a", "there", 
                "answer", "any", "by", 
                 "bye", "their" }; 
    int n = sizeof(keys)/sizeof(keys[0]); 

    struct TrieNode *root = getNode(); 

    for (int i = 0; i < n; i++) 
        insert(root, keys[i]); 

    // Search for different keys 
    search(root, "a1g6os")? cout << "Yes\n" : 
                     cout << "No\n"; 
    return 0; 
}
#包括
使用名称空间std;
常量int字母表大小=26;
结构三节点{
结构三节点*子项[字母表大小];
布尔·伊森多福德;
}; 
结构三节点*getNode(void){
结构三节点*pNode=新三节点;
pNode->isendoford=false;
对于(int i=0;ichildren[i]=NULL;
返回pNode;
} 
空插入(结构三节点*根,字符串键){
结构三节点*pCrawl=根;
对于(inti=0;i子项[索引])
pCrawl->children[index]=getNode();
pCrawl=pCrawl->children[索引];
} 
//将最后一个节点标记为叶
pCrawl->isEndOfWord=true;
} 
//如果在trie中出现键,则返回true,否则返回
//假的
布尔搜索(结构三节点*根,字符串键){
结构三节点*pCrawl=根;
对于(inti=0;i子项[索引])
返回false;
pCrawl=pCrawl->children[索引];
} 
返回(pCrawl!=NULL&&pCrawl->isEndOfWord);
} 
int main(){
字符串键[]={“the”,“a”,“there”,
“回答”、“任何”、“由”,
“再见”,“他们的”};
int n=sizeof(键)/sizeof(键[0]);
结构三节点*root=getNode();
对于(int i=0;i搜索(root,“a1g6os”)?cout一些程序员dude和@JohnnyJohansson都指出了根本原因。现场测试显示了代码读取数组的位置超出了界限。实际上,一旦您了解了发生的情况,修复就很容易了。下面是修复代码,如果您自己无法解决它。这里是对它的现场测试

#包括
使用名称空间std;
const int ALPHABET_SIZE=75;//增加范围
结构三节点{
结构三节点*子项[字母表大小];
布尔·伊森多福德;
}; 
结构三节点*getNode(void){
结构三节点*pNode=新三节点;
pNode->isendoford=false;
对于(int i=0;ichildren[i]=NULL;
返回pNode;
} 
空插入(结构三节点*根,字符串键){
结构三节点*pCrawl=根;
对于(inti=0;i子项[索引])
pCrawl->children[index]=getNode();
pCrawl=pCrawl->children[索引];
} 
//将最后一个节点标记为叶
pCrawl->isEndOfWord=true;
} 
//如果在trie中出现键,则返回true,否则返回
//假的
布尔搜索(结构三节点*根,字符串键){
结构三节点*pCrawl=根;
对于(inti=0;i子项[索引])
返回false;
pCrawl=pCrawl->children[索引];
} 
返回(pCrawl!=NULL&&pCrawl->isEndOfWord);
} 
int main(){
字符串键[]={“the”,“a”,“there”,
“回答”、“任何”、“由”,
“再见”,“他们的”};
int n=sizeof(键)/sizeof(键[0]);
结构三节点*root=getNode();
对于(int i=0;i搜索(root,“a1g6os”)?是否显示的代码与调试器输出不匹配。调试器显示一个
参数,该参数指示成员函数。显示的代码中没有一个函数是成员函数。创建时,请确保它实际显示了您询问的问题(当然,您显示的所有输出都来自该代码)。在访问它的成员之前,您需要检查
pCrawl
是否不是
nullptr
。代码仅用于小写字母(字母表大小为26,索引由“a”偏移)但是,您的搜索字符串包含数字。这将导致在分配的数组之外访问。还要注意,如“代码>密钥[i] -' a’/COD>与ASCII编码很好地工作,但是ASCII不是由C++规范强制执行的。还有其他的(虽然不是很常见)。这样的编码是行不通的。@JohnnyJohansson你说得对,这个trie不支持数字。谢谢你指出这个问题
#include<iostream>
using namespace std; 
const int ALPHABET_SIZE = 75; // increase the range

struct TrieNode { 
struct TrieNode *children[ALPHABET_SIZE]; 

bool isEndOfWord; 
}; 



struct TrieNode *getNode(void) { 
    struct TrieNode *pNode =  new TrieNode; 
     pNode->isEndOfWord = false; 

    for (int i = 0; i < ALPHABET_SIZE; i++) 
        pNode->children[i] = NULL; 

    return pNode; 
} 



void insert(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - '0';  // lower the low bound
        if (!pCrawl->children[index]) 
            pCrawl->children[index] = getNode(); 

        pCrawl = pCrawl->children[index]; 
   } 

   // mark last node as leaf 
   pCrawl->isEndOfWord = true; 
} 

// Returns true if key presents in trie, else 
// false 
bool search(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - '0';  // lower the low bound
        if (!pCrawl->children[index]) 
             return false; 

        pCrawl = pCrawl->children[index]; 
    } 

    return (pCrawl != NULL && pCrawl->isEndOfWord); 
} 

int main() { 
    string keys[] = {"the", "a", "there", 
                "answer", "any", "by", 
                 "bye", "their" }; 
    int n = sizeof(keys)/sizeof(keys[0]); 

    struct TrieNode *root = getNode(); 

    for (int i = 0; i < n; i++) 
        insert(root, keys[i]); 

    // Search for different keys 
    search(root, "a1g6os")? cout << "Yes\n" : 
                     cout << "No\n"; 
    return 0; 
}