Question

I have 2 files: dictionary and sample.

void print_table (Table);
void print_stats (Table);
Was it helpful?

Solution

Several problems:
- You are always returning false from find Fixed by user
- 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 and key and head->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 use strcmp instead.
  • You don't want to compare key to head->left; you probably mean to compare to head->left->element. Otherwise you are comparing a char* to a node*. But again strcmp simplifies all of this, as you actually don't need to and shouldn't do this check here; particularly as head->left might itself be NULL.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top