Several problems:
- You are always returning false from Fixed by userfind
- You are always adding a print out of 'not found' whenever the root of the search isn't a match.
else if(key<head->element) { printf("not found"); return search(key, head->left);
And the bigger two problems
- You are in
C
language andkey
andhead->element
are both const char* or char*; you can't use the==
operator on them. That will only work if the pointers point to the same address. You want to usestrcmp
instead. - You don't want to compare
key
tohead->left
; you probably mean to compare tohead->left->element
. Otherwise you are comparing a char* to a node*. But againstrcmp
simplifies all of this, as you actually don't need to and shouldn't do this check here; particularly as head->left might itself beNULL
.
As in below:
struct node *search( Key_Type key, struct node *head )
{
if ( head != NULL)
{
//note change to strcmp
int res = strcmp( key, head->element );
if ( !res )
{
printf("found");
return head;
}
else if( res < 0 ) //note change to strcmp.
{
printf( "not found" );
return search( key, head->left );
}
else
{
printf( "found3" );
return search( key, head->right );
}
}
else
return FALSE;
}