简体   繁体   中英

How to replace a node in a binary search tree

I have a binary search tree with strings. Each node contains the string and the frequency of the string.

My function is given two strings, s1, s2. I have to replace s1 with s2. If s2 already exist in the bstree I just need to add the frequency of s1.If it doesn't exist I create a new node with the frequency of s1.

What I did is: (1) I delete s1 from the bstree and save the frequency of s1 (2) I insert s2 to the bstree(using s1's frequency)

The problem is that while (1) works and the node of s1 is deleted the second part gives me nothing(when I run it without the delete funtion it gives me some weird symbols)

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

Your problem comes because a return is missing in replacehelp .

Note that to test (state < 0) is useless in replacehelp because it is not 0 nor positive, so

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

You do not give the definition of DeleteNode , do it is not possible to know if it is right as you suppose.

If I add some definitions to run :

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

Compilation and execution :

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 $ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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