user114518
user114518

Reputation:

Binary Search Tree root is working, but it can't have any children?

So I'm trying to create my own BST for a spellchecker because I want to additional functionality (find nearby nodes for suggestions). Anyways, I can create the root node, but after that it doesn't work. For example, if I have the following code:

BST myTree;
myTree.add("rootElement");
myTree.add("abcChild");

If I make root (node *root) public and check for myTree.root->left !=NULL || myTree.root->right != NULL, I get a seg fault. I don't understand. Here's some of my code:

struct node {
  string data;
  node *left;
  node *right;
};


void BST::add(string newData)
{
  //Find a position                                                                                                                         
  if (root == NULL){
    root = new node;
    root->data = newData;
    root->left = NULL;
    root->right = NULL;

  }
  else{ //remember to include string                                                                                                        

    if(root->data.compare(newData) < 0){

      // Add to left                                                                                                                        

      addRecursive(root->left, newData);

    }
    else{
      // Add to right                                                                                                                       
      addRecursive(root->right, newData);
    }
  }
}


void BST::addRecursive(node *currentNode, string newData)
{
  if (currentNode == NULL){

    currentNode = new node;
    currentNode->data = newData;
    currentNode->left = NULL;
    currentNode->right = NULL;

  }
  else{ //remember to include string                                                                                                  

    if(currentNode->data.compare(newData) < 0){

      // Add to left                                                                                                                  

      addRecursive(currentNode->left, newData);

    }
    else{
      // Add to right                                                                                                                 
      addRecursive(currentNode->right, newData);
    }
  }
}

What's the deal?

Upvotes: 2

Views: 302

Answers (3)

Seth Carnegie
Seth Carnegie

Reputation: 75130

In add, when you do

root = new node;

root is a class variable, so that's not a problem and is the correct way to do it. However, in addRecursive, when you are doing

currentNode = new node;

currentNode is a pointer that is passed by value to your function, so you are only making the local variable currentNode point to another place in memory. You need to pass the pointers by reference so that when you modify the parameter, it modifies the original instead of just the local variable. Just make the signature of your function addRecursive to void addRecursive(node*& currentNode, const string& newData). This will make the pointers be passed to the function by reference.

Also notice that I changed string newData to const string& newData. That is so you avoid making a copy of the string in memory every time you call the function. You should make that change in all of your functions when you don't need to modify a copy of the string passed to the function, to improve efficiency.

Upvotes: 3

Mithrandir
Mithrandir

Reputation: 25337

Try this:

struct node {
  string data;
  node *left;
  node *right;
};



void BST::add(string newData)
{
      root = addRecursive(root, newData);
}


node* BST::addRecursive(node *currentNode, string newData)
{
  if (currentNode == NULL){

    currentNode = new node;
    currentNode->data = newData;
    currentNode->left = NULL;
    currentNode->right = NULL;

  }
  else{ //remember to include string                                                                                                  

    if(currentNode->data.compare(newData) < 0){

      // Add to left                                                                                                                  

      currentNode->left = addRecursive(currentNode->left, newData);

    }
    else{
      // Add to right                                                                                                                 
      currentNode->right = addRecursive(currentNode->right, newData);
    }
  }
  return currentNode;
}

Upvotes: 0

Pheonix
Pheonix

Reputation: 6052

In your current implementation :

void BST::addRecursive(node *currentNode, string newData)
{
  if (currentNode == NULL){

    currentNode = new node;
    currentNode->data = newData;
    currentNode->left = NULL;
    currentNode->right = NULL;
//WHERE DOES currentNode GOES ??         <----------- ??? currentNode was originally NULL, so 0... All NULL's are same.
  }
  else{ //remember to include string                                                                                                  

    if(currentNode->data.compare(newData) < 0){

      // Add to left                                                                                                                  

      addRecursive(currentNode->left, newData);

    }
    else{
      // Add to right                                                                                                                 
      addRecursive(currentNode->right, newData);
    }
  }
}

try something like this :

    struct node {
      string data;
      node *left;
      node *right;
    };


    void BST::add(string newData)
    {
      //Find a position                                                                                                                         
      if (root == NULL){
        root = new node;
        root->data = newData;
        root->left = NULL;
        root->right = NULL;

      }
      else{ //remember to include string                                                                                                        
           addRecursive(root, newData);    
      }
    }


    void BST::addRecursive(node *currentNode, string newData)
    {

       if(root->data.compare(newData) < 0){

          // Add to left                                                                                                                        
        if(currentNode->left != NULL){
           addRecursive(currentNode->left, newData);
        }else{
         currentNode->left = new node;
         currentNode->left->data = newData;
         currentNode->left->left = NULL;
         currentNode->left->right = NULL;
        }

       }
        else{
          // Add to right                                                                                                                       
          //Implementation Symmetric to Left
       }
     }
   }

Upvotes: 0

Related Questions