gpt4 book ai didi

c++ - 修复 trie c++ 中的段错误

转载 作者:行者123 更新时间:2023-11-30 03:18:33 25 4
gpt4 key购买 nike

我正在使用一个 trie 实现来存储和搜索 c++ 编程语言中的单词。在使用 search() 函数时,我在搜索特定单词时遇到段错误。好像是检查struct是否为null时出错。

这是错误信息:

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;
}

最佳答案

@Some programmer dude 和@JohnnyJohansson 都指出了根本原因。现场测试显示代码读取数组的位置越界。实际上,一旦您了解会发生什么,修复就很容易了。如果您自己无法理解,以下是固定代码。现场测试在这里 cee.studio

#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;
}

关于c++ - 修复 trie c++ 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54626725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com