[英]Binary search tree SEGFAULT in C
我正在用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 return temp-> data;
即findmin函数中的while循环
您在findMin()
和findMax()
函数中都取消引用NULL指针。
int findMin(tree *root){
while(temp != NULL){
temp = temp->right;
}
return temp->data; <-- problem here
}
当temp
变为NULL时, while
循环退出。 但是,在temp
变为NULL
之后,您将取消引用该temp
。
findMax()
也有同样的问题。 您要在最后一个节点的值变为NULL 之前返回它的值。 您可以将功能修改为:
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;
}
以便返回min
和max
。
在测试temp为空之前,您不会返回temp-> data; (null)-> data将为您带来细分错误。
尝试像这样修复循环:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.