我干 运行 这个尝试与对 class 的实现,但它没有给出预期的 output.What 我做错了吗?
I have dry ran this implementation of tries with pair class but it's not giving expected output.What am i doing wrong?
#include<iostream>
using namespace std;
class trieNode{
public:
int data;
bool isTerminal;
trieNode ** children;
trieNode(int data){
this->data = data;
children = new trieNode*[26];
for(int i=0;i<26;i++){
children[i] = NULL;
}
isTerminal = false;
}
};
class Pair{
public:
bool exist;
trieNode* address;
Pair(){
exist = false;
address = NULL;
}
};
class Trie{
public:
trieNode *root;
Trie(){
root = new trieNode('[=13=]');
}
// for programmer
private:
void insert(trieNode* root,string word){
//base case
if(word.size() == 0){
root->isTerminal = true;
return ;
}
// small calculation
int index = word[0] - 'a';
trieNode *child;
if(root->children[index] != NULL){
child = root->children[index];
}
else{
child = new trieNode(word[0]);
root->children[index] = child;
}
// recursion
insert(child,word.substr(1));
}
// for user
public:
void insertWord(string word){
insert(root,word);
}
// for programmer
private:
void deleteWord(trieNode* root, string word){
if(word.size() == 0){
root->isTerminal = false;
return;
}
int index = word[0] - 'a';
trieNode *child;
if(root->children[index] != NULL){
child = root->children[index];
}
else{
return;
}
deleteWord(child,word.substr(1));
if(child->isTerminal == false){
for(int i=0;i<26;i++){
if(child->children[i] != NULL)
return;
}
delete child;
root->children[index] = NULL;
}
}
// delete word
//for user
public:
void deleteWord(string word){
deleteWord(root,word);
}
// search a sting in trie
//function for programmer
// i used a pair class as return type brcause i want to return if word exists then return it's
address too
// i.e return a bool = true and adress where the word ends
private:
Pair find(trieNode *root, string word){
Pair p;
if(word.size() == 0){
Pair p;
p.address = root;
if(root->isTerminal == true)
p.exist = true;
else
p.exist = false;
return p;
}
trieNode *child;
int index = word[0]-'a';
if(root->children[index] == NULL){
Pair p;
p.address = root;
p.exist = false;
return p;
}
else{
child = root->children[index];
p = find(child, word.substr(1));
}
}
// search a string in the trie
// function for user
public:
Pair findstr(string word){
Pair p;
p = find(root,word);
return p;
}
};
int main(){
Trie t;
t.insertWord("sucess");
t.insertWord("s");
t.insertWord("a");
Pair p;
p = t.findstr("sucess");
cout<< p.address->data <<" "<< p.exist<<endl;
p = t.findstr("s");
cout<< p.address->data <<" "<< p.exist<<endl;
p = t.findstr("a");
cout<< p.address->data <<" "<< p.exist;
I am using pair class for implementing a function called findstr which finds a word in the trie and returns 2 things a bool and address of the last trieNode of the word, for that i used a pair class, in this code it should return address in hexadecimal and true for all three , but i an only see garbage values
}
这里有一个问题
else{
child = root->children[index];
p = find(child, word.substr(1));
}
应该是
else{
child = root->children[index];
p = find(child, word.substr(1));
return p;
}
编译器本应就缺少 return 语句向您发出警告。你忽略了吗?
代码可能还有很多其他问题。正如已经说过的,要做的事情是使用调试器。修复错误的方法比在 SO 上询问要快得多。
#include<iostream>
using namespace std;
class trieNode{
public:
int data;
bool isTerminal;
trieNode ** children;
trieNode(int data){
this->data = data;
children = new trieNode*[26];
for(int i=0;i<26;i++){
children[i] = NULL;
}
isTerminal = false;
}
};
class Pair{
public:
bool exist;
trieNode* address;
Pair(){
exist = false;
address = NULL;
}
};
class Trie{
public:
trieNode *root;
Trie(){
root = new trieNode('[=13=]');
}
// for programmer
private:
void insert(trieNode* root,string word){
//base case
if(word.size() == 0){
root->isTerminal = true;
return ;
}
// small calculation
int index = word[0] - 'a';
trieNode *child;
if(root->children[index] != NULL){
child = root->children[index];
}
else{
child = new trieNode(word[0]);
root->children[index] = child;
}
// recursion
insert(child,word.substr(1));
}
// for user
public:
void insertWord(string word){
insert(root,word);
}
// for programmer
private:
void deleteWord(trieNode* root, string word){
if(word.size() == 0){
root->isTerminal = false;
return;
}
int index = word[0] - 'a';
trieNode *child;
if(root->children[index] != NULL){
child = root->children[index];
}
else{
return;
}
deleteWord(child,word.substr(1));
if(child->isTerminal == false){
for(int i=0;i<26;i++){
if(child->children[i] != NULL)
return;
}
delete child;
root->children[index] = NULL;
}
}
// delete word
//for user
public:
void deleteWord(string word){
deleteWord(root,word);
}
// search a sting in trie
//function for programmer
// i used a pair class as return type brcause i want to return if word exists then return it's
address too
// i.e return a bool = true and adress where the word ends
private:
Pair find(trieNode *root, string word){
Pair p;
if(word.size() == 0){
Pair p;
p.address = root;
if(root->isTerminal == true)
p.exist = true;
else
p.exist = false;
return p;
}
trieNode *child;
int index = word[0]-'a';
if(root->children[index] == NULL){
Pair p;
p.address = root;
p.exist = false;
return p;
}
else{
child = root->children[index];
p = find(child, word.substr(1));
}
}
// search a string in the trie
// function for user
public:
Pair findstr(string word){
Pair p;
p = find(root,word);
return p;
}
};
int main(){
Trie t;
t.insertWord("sucess");
t.insertWord("s");
t.insertWord("a");
Pair p;
p = t.findstr("sucess");
cout<< p.address->data <<" "<< p.exist<<endl;
p = t.findstr("s");
cout<< p.address->data <<" "<< p.exist<<endl;
p = t.findstr("a");
cout<< p.address->data <<" "<< p.exist;
I am using pair class for implementing a function called findstr which finds a word in the trie and returns 2 things a bool and address of the last trieNode of the word, for that i used a pair class, in this code it should return address in hexadecimal and true for all three , but i an only see garbage values
}
这里有一个问题
else{
child = root->children[index];
p = find(child, word.substr(1));
}
应该是
else{
child = root->children[index];
p = find(child, word.substr(1));
return p;
}
编译器本应就缺少 return 语句向您发出警告。你忽略了吗?
代码可能还有很多其他问题。正如已经说过的,要做的事情是使用调试器。修复错误的方法比在 SO 上询问要快得多。