C中的二进制搜索树SEGFAULT

时间:2014-03-23 15:28:26

标签: c segmentation-fault binary-tree

我在C中实现BTS。我已经实现了搜索和插入等基本功能。但我找到最小的元素和最大的元素有问题。这是我的代码:

#include <stdio.h>
#include <stdlib.h>

//Begin
typedef struct tree{
    int data;
    struct tree *left;
    struct tree *right;
}tree;
//----------------------------------------------------------------------------------------
tree *insert(tree *root, int data);
tree *newnode(int data);
int search(tree *root, int data);
int findMin(tree *root);
int findMax(tree *root);
//-----------------------------------------------------------------------------------------
int main(void){
    tree *root = malloc(sizeof(root));
    root->left = NULL;
    root->right = NULL;
    insert(root, 15);
    insert(root, 10);
    insert(root, 20);
    printf("%i\n", search(root ,15));
    printf("%i\n", search(root ,20));
    printf("%i\n", search(root ,10));
    printf("%i\n", search(root ,11));
    printf("%i\n", search(root ,17));   
    printf("%i\n", search(root ,10));
    printf("Min: %i\n", findMin(root));
    printf("Max: %i\n", findMax(root));
    return 0;
}
//-----------------------------------------------------------------------------------------
tree *insert(tree *root, int data){
    if(root == NULL){
        root = newnode(data);
    }

    else if(root->data < data)
            root->right = insert(root->right,data);
    else if(root->data > data)
            root->left = insert(root->left,data);
    else{
        perror("the elements already exist!");
    }
    return root;
}
//-----------------------------------------------------------------------------------------
tree *newnode(int data){
    tree *new = malloc(sizeof(tree));
    new->data = data;
    new->left = NULL;
    new->right = NULL;
    return new;
}
//-----------------------------------------------------------------------------------------
int search(tree *root, int data){
    if(root == NULL){
        return 0;
    }
    else if(root->data == data){
        return root->data;
    }
    else if (root->data < data){
        return search(root->right,data);
    }
    else if (root->data > data){
        return search(root->left,data);
    }
    else{
        perror("Error");
    }
}

//-----------------------------------------------------------------------------------------
int findMin(tree *root){
    tree *temp = root;
    while(temp != NULL){
        temp = temp->left;
    }
    return temp->data;
}
//-----------------------------------------------------------------------------------------
int findMax(tree *root){
    tree *temp = root;
    while(temp != NULL){
        temp = temp->right;
    }
    return temp->data;
}
//End 

错误在这里: 81返回temp-&gt;数据;

即findmin函数中的while循环

2 个答案:

答案 0 :(得分:0)

您在findMin()findMax()函数中取消引用NULL指针。

int findMin(tree *root){
 while(temp != NULL){
        temp = temp->right;
    }
 return temp->data; <-- problem here
} 

while变为NULL时,temp循环退出。但是,在temp成为NULL后,您将取消引用findMax()

int findMin(tree *root){ int min; tree *temp = root; while(temp != NULL){ min = temp->data; temp = temp->left; } return min; } int findMax(tree *root){ int max; tree *temp = root; while(temp != NULL){ max=temp->data; temp = temp->right; } return max; } 同样存在问题。您希望在之前返回最后一个节点的值,它变为NULL。您可以将功能修改为:

min

以便返回max和{{1}}值。

答案 1 :(得分:0)

在测试temp为null之前,您不会返回temp-&gt;数据; (null) - &gt;数据会让您遇到分段错误。

尝试像这样修复你的循环:

int findMin(tree *root){
   tree *temp = root;
   while(temp -> left != NULL){
      temp = temp->left;
   }
   return temp->data;
}
int findMax(tree *root){
    tree *temp = root;
    while(temp->right != NULL){
        temp = temp->right;
    }
    return temp->data;
}