For starters this structure declaration
struct Node {
int value = -1;
struct Node* left = NULL;
struct Node* right = NULL;
};
is invalid in C. You may not specify initializers.
So the declaration shall look like
struct Node {
int value;
struct Node* left;
struct Node* right;
};
The function insert_tree gets the pointer to the root node by value. That is the function deals with a copy of the original pointer, So changing a copy of the original pointer within the function does not influence on the value of the original pointer.
You need to pass the pointer by reference. In C passing by reference means passing an object indirectly through pointer. Take into account that memory allocation can fail. It is desirable that the function would report whether insertion of a new node was successful.
Also it will be more competently to make the first parameter the pointer to the pointer to the root node and the second parameter the value that should be inserted.
In any case this function declaration
void insert_tree(int value, Node* root) {
^^^^^^^^^^
is an invalid C function declaration because opposite to C++ in C you have to specify the keyword struct as
void insert_tree(int value, struct Node* root) {
^^^^^^^^^^^^^^^^^
The recursive function insert_tree can be defined the following way
int insert_tree( struct Node **root, int value )
{
if ( *root == NULL )
{
*root = (struct Node*) malloc( sizeof( struct Node ) );
if ( *root != NULL )
{
( *root )->value = value;
( *root )->left = NULL;
( *root )->right = NULL;
}
return *root != NULL;
}
else
{
if ( value < ( *root )->value )
{
return insert_tree( &( *root )->left, value );
}
else
{
return insert_tree( &( *root )->right, value );
}
}
}
And the function can be called like
int main( void )
{
struct Node *root = NULL;
insert_tree( &root, 30 );
printTree( root );
}
Also as the function printTree does not change the tree then its parameter should have the qualifier const.
void printTree( const struct Node* root ) {
if (root != NULL) {
printf("%i\n", root->value);
printTree(root->left);
printTree(root->right);
}
}
Here is a demonstrative C program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Node
{
int value;
struct Node *left;
struct Node *right;
};
int insert_tree( struct Node **root, int value )
{
if ( *root == NULL )
{
*root = (struct Node*) malloc( sizeof( struct Node ) );
if ( *root != NULL )
{
( *root )->value = value;
( *root )->left = NULL;
( *root )->right = NULL;
}
return *root != NULL;
}
else
{
if ( value < ( *root )->value )
{
return insert_tree( &( *root )->left, value );
}
else
{
return insert_tree( &( *root )->right, value );
}
}
}
void printTree( const struct Node* root ) {
if (root != NULL) {
printf("%i\n", root->value);
printTree(root->left);
printTree(root->right);
}
}
int main(void)
{
struct Node *root = NULL;
const int N = 10;
srand( ( unsigned int )time( NULL ) );
for ( int i = 0; i < N; i++ )
{
insert_tree( &root, rand() % N );
}
printTree( root );
return 0;
}
Its output might look like
1
0
9
4
3
7
5
6
5
8