BST有字符串

时间:2014-12-14 18:48:14

标签: c binary-search-tree realloc

我正在尝试修改BST以使用我的书中与整数一起使用的模型中的字符串。由于字符串的mallocing间距(我相信),我很难修改。我测试了代码,它在需要时走向正确的方向但在处理字符串时失败了。我有所有的功能工作和测试,但不能删除运行,所以如果有人能指出我正确的方向,我将不胜感激。

我的struct treeNode很适合树的节点。

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

   }treeNode;

我的删除函数接收了一个节点和字符串

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

}

再次感谢您的时间和帮助。

编辑:我已经实现了这个更改,现在它在没有实际删除节点的情况下循环。我认为在设置没有子项的情况下的节点值时,它会破碎。

EDIT2:此代码工作错误在代码的不同部分!谢谢大家的帮助。

2 个答案:

答案 0 :(得分:0)

您在之前使用strcmp(string, node->data); 检查if(node==NULL)。在这种情况下,您会遇到分段错误。

答案 1 :(得分:0)

删除仍在其中的第一个strcmp()。在重新定位的{之后删除大括号}(并匹配else?)和strcmp()

if(node==NULL) {
    printf("Element Not Found");
} else {
    compare = strcmp(string, node->data);
    if(compare > 0) {
    ...
}