我有一个带有字符串的二进制搜索树。每个节点都包含字符串和字符串的频率。
给我的函数两个字符串s1,s2。我必须将s1替换为s2。 如果bstree中已经存在s2,则只需添加s1的频率。如果不存在,则创建一个频率为s1的新节点。
我所做的是: (1)我从bstree中删除s1并保存s1的频率 (2)我将s2插入bstree(使用s1的频率)
问题在于,虽然(1)起作用并且s1的节点被删除 第二部分什么也没有给我(当我运行不带删除功能的代码时,它给了我一些奇怪的符号)
struct node{
string data;
int freq;
node *left, *right;
node(string d, int f){
data = d;
freq = f;
left = right = nullptr;
}
node *replacehelp(node *r, string v, int f){
if(r == nullptr)
return new node(v,f);
int state = v.compare(r->data);//to check the alphabetical order
if(state == 0){
r->freq += f;
return r;
}
if(state > 0)
r->right = replacehelp(r->right, v, f);
else if(state < 0)
r->left = replacehelp(r->left, v, f);
}
void replace(const string &s1, const string &s2){
//the delete function works(I get the freq of s1)
root = DeleteNode(root, s1, &freq);
//I have to insert s2 to the tree
root = replacehelp(root,s2,freq);
}
答案 0 :(得分:1)
出现问题是因为 replacehelp 中缺少返回。
请注意,在{em> replacehelp 中测试(state < 0)
是没有用的,因为它既不是0也不是正数,所以
node *replacehelp(node *r, string v, int f){
if(r == nullptr)
return new node(v,f);
int state = v.compare(r->data);//to check the alphabetical order
if(state == 0)
r->freq += f;
else if(state > 0) // 'else' ADDED
r->right = replacehelp(r->right, v, f);
else // MODIFIED
r->left = replacehelp(r->left, v, f);
return r;
}
您没有给出 DeleteNode 的定义,不可能像您想的那样知道它是否正确。
如果我添加一些要运行的定义:
#include <iostream>
#include <string>
using namespace std;
struct node{
string data;
int freq;
node *left, *right;
node(string d, int f){
data = d;
freq = f;
left = right = nullptr;
}
};
node *replacehelp(node *r, string v, int f){
if(r == nullptr)
return new node(v,f);
int state = v.compare(r->data);//to check the alphabetical order
if(state == 0)
r->freq += f;
else if(state > 0) // 'else' ADDED
r->right = replacehelp(r->right, v, f);
else // MODIFIED
r->left = replacehelp(r->left, v, f);
return r; // ADDED
}
// ADDED
node * DeleteNode(node *r, string s, int * freq) // can be "int & freq" to not have to give the addr in the call
{
if (r != nullptr) {
int state = s.compare(r->data);
if (state > 0)
r->right = DeleteNode(r->right, s, freq);
else if (state < 0)
r->left = DeleteNode(r->left, s, freq);
else {
*freq = r->freq;
if (r->right == nullptr) {
node * t = r->left;
delete (r);
return t;
}
if (r->left == nullptr) {
node * t = r->right;
delete (r);
return t;
}
node * t = r->right;
while ((t != nullptr) && (t->left != nullptr))
t = t->left;
r->data = t->data;
r->freq = t->freq;
int dummy;
r->right = DeleteNode(r->right, t->data, &dummy);
}
}
return r;
}
node * root; // ADDED
void replace(const string &s1, const string &s2){
int freq = 0; // initialized to 0 to not have undefined value if DeleteNode do not find the node
//the delete function works(I get the freq of s1)
root = DeleteNode(root, s1, &freq);
//I have to insert s2 to the tree
root = replacehelp(root,s2,freq);
}
// ADDED
void insert(string s, int freq)
{
root = replacehelp(root, s, freq);
}
// ADDED
void pr(node *r)
{
cout << '(';
if (r != nullptr) {
pr(r->left);
cout << '"' << r->data << "\" " << r->freq;
pr(r->right);
}
cout << ')';
}
// ADDED
int main(void)
{
insert("5", 5);
insert("4", 4);
insert("3", 3);
insert("6", 6);
pr(root);
cout << endl;
replace("5", "55");
pr(root);
cout << endl;
replace("3", "33");
pr(root);
cout << endl;
replace("4", "33");
pr(root);
cout << endl;
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -g -pedantic -Wextra -Werror t.cc
pi@raspberrypi:/tmp $ ./a.out
(((()"3" 3())"4" 4())"5" 5(()"6" 6()))
(((()"3" 3())"4" 4(()"55" 5()))"6" 6())
(((()"33" 3())"4" 4(()"55" 5()))"6" 6())
(((()"33" 7())"55" 5())"6" 6())
pi@raspberrypi:/tmp $