繁体   English   中英

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

[英]How to replace a node in a binary search tree

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

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

我所做的是:(1)从bstree中删除s1并保存s1的频率(2)在stree中插入s2(使用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);
}

出现问题是因为replacehelp中缺少返回值。

注意,在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 $ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM