Thread: Search function not working fully

  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Another thing to note is that your compareData won't admit that it's found a match unless every single data element matches; but your temporary struct only has valid data in the fname field. (Edit: Never mind -- the memset 0 should make strlen of lname 0, allowing the shortcut to happen.)

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    ok tabstop, I think it just looks at the first element and forgets about the leafs, What do you think?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by elsewhere View Post
    ok tabstop, I think it just looks at the first element and forgets about the leafs, What do you think?
    As long as your function returns -1, 0, or 1 one of your cases will trigger.

    As an aid to debugging, you should add a default case to all of your switches with an appropriate message (such as which switch you're in, and what the value of the switch variable is) -- and see what happens.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    As long as your function returns -1, 0, or 1 one of your cases will trigger.

    As an aid to debugging, you should add a default case to all of your switches with an appropriate message (such as which switch you're in, and what the value of the switch variable is) -- and see what happens.
    ok, I will try that out and post the results, I have been thinking and it make sense that the cal
    l foundNode = search(key, rootPtr);
    only deals with the root and doesn't touch the leaves, What do you think about that Tabstop?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by elsewhere View Post
    ok, I will try that out and post the results, I have been thinking and it make sense that the cal
    l foundNode = search(key, rootPtr);
    only deals with the root and doesn't touch the leaves, What do you think about that Tabstop?
    That doesn't particularly make any sense at all, unless you are not building your tree correctly in the first place.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    9

    Search function not working fully

    My program, compiles and works fine, however there is an issue with the search function, When I try to search for a specific element in the binary tree, It doesn't work! for example: when i enter only one entry, then search for that same entry, it function works perfectly, but when i add in another entry and try search for it , nothing results.
    Something to note, is that when i have multiple entries in the binary tree, The search function will only work for the first entry entered!

    here are the structures:
    Code:
    struct employee{
    
       char fname[SIZE];
    
       char lname[SIZE];
    
       double socialnum;
    
       float rate;
    
       float workhrs;
    
       float pay;
    
       struct employee *next; 
    
       struct employee *prev;
    
    };
    
    
    
    typedef struct employee EMP;
    
    
    
    
    
    struct treeNode{
    
    struct treeNode *leftPtr;
    
    EMP *data;
    
    struct treeNode *rightPtr;
    
    };
    
    
    
    typedef struct treeNode TreeNode;
    
    typedef TreeNode *TreeNodePtr;
    The search function:
    Code:
    TreeNodePtr search(char *key, TreeNodePtr leaf)
    {
          if(leaf!=NULL)
        {
      /* Allocate a temporary structure to help with our compareData() call */
      EMP tmpData;
      memset(&tmpData, 0, sizeof(EMP));  
      strcpy(tmpData.fname, key);
            switch (compareData(leaf->data, &tmpData))
            {
      case 0:
                //If the values are equal, return the node
                return leaf;
      case -1:
                //If the inputed value is less then the leaf's value, search left side for the inputed value
                 return search(key,leaf->leftPtr);
      case 1:
                //If the inputed value is greater then the leaf's value, search right side for the inputed value
                return search(key,leaf->rightPtr);
            }
        }
        else
        {
           //If the leaf isn't pointing to anything, return 0;
            return NULL;
        }
    }
    This is recursively called in the search function
    Code:
    int compareData( EMP *firstVal, EMP *secondVal)
    {
    
    switch (strcmp(firstVal->fname, secondVal->fname))
    {
     /* The two first names match */
     case 0:
    if ((strlen(firstVal->lname) == 0) || (strlen(secondVal->lname) == 0))
    
             return 0;
          else
          {
             /* The second switch() statement should be in here */
    switch (strcmp(firstVal->lname, secondVal->lname))
      {
       /* The last name ALSO matches */
       case 0:
    /* Determine if SSN match */
        if (firstVal->socialnum == secondVal->socialnum)
        {
            /* Now, compare the rate */
            if (firstVal->rate == secondVal->rate)
               /* rates are equal */
               return 0;
            else if (firstVal->rate > secondVal->rate)
               /* first rate is greater than second rate */
               return 1;
            else
               /* first rate is less than second rate */
               return -1;
        }
        else if (firstVal->socialnum > secondVal->socialnum)
            /* First value SSN is greater than second value SSN */
            return 1;
        else
            /* First value SSN is less than second value SSN */
            return -1;
        break;
       /* The firstVal's lastname is greater than secondVal's lastname */
       case 1:
        return 1;
        break;
       /* The firstVal's lastname is less than secondVal's lastname */
       case -1:
        return -1;
        break;
      }
      break;
     /* The firstVal's firstname is greater than secondVal's firstname */
     case 1:
      return 1;
      break;
     /* The firstVal's firstname is less than secondVal's firstname */
     case -1:
     return -1;
     break;
    }
          }
      
    }
    Call in main:
    Code:
    TreeNodePtr foundNode = NULL;
    case 2:
    system("clear");
    char key[SIZE];
    printf("Please enter first name to search tree: ");
    scanf("%s",key);
    foundNode = search(key, rootPtr);
    
    if (foundNode)
    displayOneRecord(foundNode->data);
    Please I hope I made myself clear
    thanks for your time

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you believe that strcmp will always return -1, 0, or 1?

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    9

    Well, that is another concern

    I have tried it out, I wrote the function and tested it with a simpler function with a linked list not a binary tree and it worked well, How can i change this to be more sure?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM