[英]Insert in Binary search tree recursively using comparison function in C
I am using nested structures to create a BST but I have a problem while inserting because I use a comparison function to do so!我正在使用嵌套结构来创建 BST,但插入时出现问题,因为我使用了比较 function 来执行此操作! here is my comparison function
这是我的比较 function
int compare_doubles(const void* a, const void* b)
{
const double* a_ = (const double*)a;
const double* b_ = (const double*)b;
return (*a_ > *b_) - (*a_ < *b_);
}
int compare_int(const void* a, const void* b)
{
const int* a_ = (const int*)a;
const int* b_ = (const int*)b;
return (*a_ > *b_) - (*a_ < *b_);
}
and here is the structures这是结构
typedef struct tree_t BinarySearchTree; //opaque structure declared on BinarySearchTree.h
struct tree_t{
int (*comparison)(const void *, const void *);
struct tree_t* lchild;
struct tree_t* rchild;
struct t_node* noeud;
};
struct t_node{
const void *key;
const void *data;
City *city;
};
and this is my function to create a new BST这是我的 function 来创建一个新的 BST
BinarySearchTree* newBST(int comparison_fn_t(const void *, const void *))
{
BinarySearchTree *t = (struct tree_t *)malloc(sizeof(struct tree_t));
t->comparison = comparison_fn_t;
t->lchild = t->rchild = NULL;
t->noeud = NULL;
return t;
}
This is my insertion function这是我的插入 function
BinarySearchTree* insertInBST(BinarySearchTree* bst, const void* key, const void* value) {
BinarySearchTree *t = bst;
if (bst->noeud == NULL)
{
struct t_node* n = (struct t_node *)malloc(sizeof(struct t_node));
t->noeud = n;
t->noeud->data = value;
t->noeud->key = key;
return t;
}
if ((bst->comparison(&key,&(bst->noeud->key))) < 0){
bst->lchild = insertInBST(bst->lchild, key, value);
}
else if ((bst->comparison(&key,&(bst->noeud->key))) >= 0){ // handle duplicate keys
bst->rchild = insertInBST(bst->rchild, key, value);
}
return bst;
}
when I try to run my code using these tests I get segfault (core dumped) this is my main function当我尝试使用这些测试运行我的代码时,我得到了段错误(核心转储)这是我的主要 function
int main()
{
int *t =15;
int *g = 13;
int *j =15;
int *k = 13;
BinarySearchTree *root = newBST(&compare_doubles);
insertInBST(root, k,j);
insertInBST(root, t, g);
}```
Your comparison function is overly complicated (*a_ > *b_) - (*a_ < *b_)
as you mix comparison (<, >) and arithmetic (-) operations.当您混合比较 (<, >) 和算术 (-) 运算时,您的比较 function 过于复杂
(*a_ > *b_) - (*a_ < *b_)
。 @WhozCraig suggested (a < b)? @WhozCraig 建议(a < b)? -1: (b < a).
-1:(b < a)。
You need to define struct City.您需要定义结构城市。
You need to #include <stdlib.h>
for malloc.您需要为 malloc
#include <stdlib.h>
。
In main you are casting integers to pointers.在 main 中,您将整数转换为指针。 The compiler warning for the first case is:
第一种情况的编译器警告是:
warning: initialization of 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]警告:从 'int' 初始化 'int *' 使指针来自 integer 没有强制转换 [-Wint-conversion]
As in int t = 15; int g = 13
如
int t = 15; int g = 13
int t = 15; int g = 13
and later insertInBST(root, &t, &g)
int t = 15; int g = 13
和更高版本insertInBST(root, &t, &g)
In main you use the wrong compare function (doubles instead of int).在 main 中,您使用了错误的比较 function (双精度而不是 int)。
I ran your code through gdb and it crashes in if (bst->noeud == 0)
because insertInBST(root, t, g)
invokes bst->rchild = insertInBST(bst->rchild, key, value)
but bst->rchild is NULL.我通过 gdb 运行您的代码,它在
if (bst->noeud == 0)
中崩溃,因为insertInBST(root, t, g)
调用bst->rchild = insertInBST(bst->rchild, key, value)
但 bst-> rchild 是 NULL。
In insertInBST, you only need to do the comparison once to figure out if you need the left or right branch.在 insertInBST 中,您只需要进行一次比较即可确定您需要左分支还是右分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.