[英]Binary search tree SEGFAULT in C
I'm implementing BTS in C. I have implement the basic things like searching and inserting. 我正在用C实现BTS。我已经实现了诸如搜索和插入之类的基本功能。 But I have problem with finding the smallest element and largest element.
但是我在寻找最小的元素和最大的元素时遇到了问题。 Here is my code:
这是我的代码:
#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
The fault is here: 81 return temp->data; 问题出在这里:81 return temp-> data;
Ie the while loop in the findmin function 即findmin函数中的while循环
You are dereferencing NULL pointer in both findMin()
and findMax()
functions. 您在
findMin()
和findMax()
函数中都取消引用NULL指针。
int findMin(tree *root){
while(temp != NULL){
temp = temp->right;
}
return temp->data; <-- problem here
}
The while
loop exits when temp
becomes NULL. 当
temp
变为NULL时, while
循环退出。 But you are dereferencing the temp
after it becomes NULL
. 但是,在
temp
变为NULL
之后,您将取消引用该temp
。
Same problem with findMax()
too. findMax()
也有同样的问题。 You want to return the last node's value before it becomes NULL. 您要在最后一个节点的值变为NULL 之前返回它的值。 You can modify your functions to:
您可以将功能修改为:
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;
}
so as to return the min
and max
values. 以便返回
min
和max
。
You are not returning temp->data until you have tested that temp is null; 在测试temp为空之前,您不会返回temp-> data; (null)->data will get you a segmentation fault.
(null)-> data将为您带来细分错误。
Try fixing your loops like this: 尝试像这样修复循环:
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.