gpt4 book ai didi

c++ - 如何替换二叉搜索树中的节点

转载 作者:行者123 更新时间:2023-11-28 04:16:39 27 4
gpt4 key购买 nike

我有一个带字符串的二叉搜索树。每个节点包含字符串和字符串的频率。

我的函数有两个字符串,s1, s2。我必须用 s2 替换 s1。如果 s2 已经存在于 bstree 中,我只需要添加 s1 的频率。如果它不存在,我创建一个频率为 s1 的新节点。

我所做的是:(1)我把s1从bstree中删除,保存s1的频率(2) 我将 s2 插入到 bstree(使用 s1 的频率)

问题是 while (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);
}

最佳答案

您的问题是因为replacehelp 中缺少return

注意要测试(state < 0)replacehelp 中没有用,因为它既不是 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 $

关于c++ - 如何替换二叉搜索树中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56447425/

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