I have the following recursive function which is supposed to call itself passing a pointer by reference.
How can I dereference tmproot->left
and tmproot->right
so that I can pass it to tree_input()
?
typedef struct node_s {
int value;
struct node_s *left;
struct node_s *right;
} node_t;
node_t new_node() {
node_t *new_node = (node_t*)malloc(sizeof(node_t));
new_node->left = NULL;
new_node->right = NULL;
return *new_node;
}
void tree_input(int value, node_t **tmproot)
{
if (*tmproot == nullptr) {
node_t *node = &new_node();
node->value = value;
}
else if (value < (*tmproot)->value) {
tree_input(value, tmproot->left);
}
else if (value >= value) {
tree_input(value, tmproot->right);
}
return;
}
tree_input()
is called the first time using tree_input(new_value, &root);
I am sure I am missing a simple trick.