I can't believe I spent so long racking my brain because I messed up my brackets. I'm sure it wont be the last time though, its annoying how you can read something so many times and yet not see it.
I have a function that creates a node in a binary tree:
Code:
void insert_node ( struct node **root, char data ) {
if ( *root == NULL ) {
// a new leaf is needed
struct node *newnode = (struct node*) malloc( sizeof( struct node ) );
newnode->data = data;
newnode->left = NULL;
newnode->right= NULL;
*root = newnode;
} else {
// decide which way to go
if ( data < (*root)->data ) {
insert_node( &(*root)->left, data );
} else {
insert_node( &(*root)->right, data );
}
}
}
( Thanks all who ironed out my errors with it )
If I call it myself multiple times it works fine:
insert_node (&tree,'c')
insert_node (&tree,'d')
insert_node (&tree,'e')
Inside my loop however it will only create one node, despite how many times its called:
Code:
while( (read=fgetc(fptr)) != EOF ) {
if ((read >= 65) && (read <= 90)) {
read = convert (read);};
insert_node (&tree,read);
Do you think this be caused by the value of read not being updated properly or maybe it's my insersion function that doesn't like being called multiple times?
Thanks