'Floor of a BST with recursive function in C
I'm trying to find the floor of a BST recursively. This is the code:
node_t* floor_impl(node_t *node, const void *key, comparator_t key_cmp)
{
if(node == NULL)
{
return NULL;
}
if(key_cmp(key, node->key) == 0)
{
return node->key;
}
else if(key_cmp(key, node->key) < 0)
{
return floor_impl(node->left, key, key_cmp);
}
else
{
node_t *floor = floor_impl(node->right, key, key_cmp);
if(floor == NULL) { return NULL; }
return key_cmp(node->key, floor->key) >= 0 ? node : floor;
}
}
This function must return a node, which contains the floor.
But the result is a segmentation fault error. I understood the theory behind the search of the floor, but not how to do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
