0

I am trying to modify a BST to work with strings from the model in my book that works with integers. I am having a hard time with the modifying because of mallocing spacing for the string (i believe). I tested the code and it goes the proper direction when needed but fails when handling the strings. I have the all the functions working and testing but cant get delete running so if anyone could point me in the right direction I'd be grateful.

My struct treeNode is well a node for a tree.

  typedef struct treeNode
   {
    char* data;
    struct treeNode *left;
    struct treeNode *right;

   }treeNode;

And my delete function that takes in a node and string to be found

////////////////////////////////////////////////////////////////////////////
/////////////////////Delete function////////////////////////////////
/////////////////////////////////////////////////////////////////////
treeNode * Delete(treeNode *node, char *string)
{
     //printf("broke before temp?");          //testing
     treeNode *temp;                         //temporary node;
     int compare;                            //do string cmp instead of int compare.
     //printf("broke before compare");

//printf("broke after repair");
    if(node==NULL)                          //if nothing in tree, can't delete (or aint here)
    { 
            printf("Element Not Found");
    }
else if(node != NULL){

    compare = strcmp(string, node->data);   //comparison variable to see if we traverse left or     right
         }
     else if(compare > 0)                   //if we need to go right
    {
    //printf("going right");
            node->right = Delete(node->right,string);  //recursive with right subtree as root
    }
    else if(compare < 0)                              //if we need to go left
    {
    //printf("going left");
            node->left = Delete(node->left,string);     //recursive with left subtree as root
    }
    ///////////////////////////////////////////////////
//////////IF we have found the element to delete///
//////////////////////////////////////////////////

    else if(compare == 0)                   
    {
            //if the node has both a left and right subtree

            if(node->right && node->left)
            {
                    /* Here we will replace with minimum element in the right sub tree */
                    temp = FindMin(node->right);  //sets temp node to smallest node
                    //printf("broke on find min");
                      free(node -> data);        //free the data that is on the node being deleted
                   //printf("broke on free");
                   node->data = (char *)malloc(strlen(temp->data)+1);   //allocates new memory for temp string
                 //printf("broke on malloc");
                  strcpy(node->data, temp->data);               //copys temp string to the node
                 //printf("broke on strcpy");

                    node -> right = Delete(node->right,temp->data);     //have to move down chain to replace
            }
            else   //if it only have one child, find the child and replace
            {

            temp = node;                //set node to temp for free
                    if(node->left == NULL)          //if no left child set node to right
                            node = node->right;
                    else if(node->right == NULL)        //if no right child set node to left
                            node = node->left;
            printf("freeing %s",temp->data);    //free string
           free(temp->data);            //free data
           free(temp);              //free node
            }
    }
    return node;

}

Thank you again for your time and help.

EDIT: I have implemented this changes, and now it loops through without actually deleting a node. I think it is breaking when i set the node values where the case with no children is found.

EDIT2: This code works error was in different section of code! Thank you all for your help.

2 Answers2

0

You are using strcmp(string, node->data); before you check if(node==NULL). In which case you get a segmentation fault.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

Remove the first strcmp() which is still in there. Remove the brace { (and matching } ?) and the else just following the relocated strcmp().

if(node==NULL) {
    printf("Element Not Found");
} else {
    compare = strcmp(string, node->data);
    if(compare > 0) {
    ...
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56