Attempt to write function search()

This is a discussion on Attempt to write function search() within the C Programming forums, part of the General Programming Boards category; I am doing a binary tree , I need to search for a specific element these are the structures and ...

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

    Attempt to write function search()

    I am doing a binary tree , I need to search for a specific element
    these are the structures and the function itself( not completed)
    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;
    Here is the search function I wrote, it won't compile because I am not
    really sure what is it that I'm doing, what is wrong? how could i
    modify this function to search for an employee by comparing the first
    name (fname) with the key and return all information of the employee if
    there is a match?
    Code:
    treeNode *search(char *key, treeNode *leaf)
    {
        //printf("Enter Employee name:");
       //gets(key);
        //If the leaf is pointing to something
        if(leaf!=NULL)
        {
            if(strcmp(key, leaf->value) == 0)
            {
                //If the values are equal, return the node
                return leaf;
            }
            else if(strcmp(key, leaf->value) < 0)
            {
                //If the inputed value is less then the leaf's value, search left side for the inputed value
                 return search(key,leaf->left);
            }
            else
            {
                //If the inputed value is greater then the leaf's value, search right side for the inputed value
                return search(key,leaf->right);
            }
        }
        else
        {
            //If the leaf isn't pointing to anything, return 0;
            return NULL;
        }
    }
    this is how function is called in main:
    Code:
    EMP *foundNode = NULL;
    case 2:
    system("clear");
    char key[20];
    printf("Please enter first name to search tree: ");
    gets(key);
    foundNode = search(key, &tmpItem);
    
    getchar();
    
    break;

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by elsewhere View Post
    Here is the search function I wrote, it won't compile because I am not
    really sure what is it that I'm doing, what is wrong?
    if it doesnt compile, tell us what the compiler error is! sure, we could waste our time looking through this code while the error may not even be in this code. provide us with the complete code, not fragments of the code (that arent even complete fragments). not being rude, just trying to 'help me help you'

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    treenodes have a pointer to an EMP, not a char pointer called value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Mats is having an allergic reaction to unnecessary use of strcmp:
    Code:
            if(strcmp(key, leaf->value) == 0)
            {
                //If the values are equal, return the node
                return leaf;
            }
            else if(strcmp(key, leaf->value) < 0)
    could be replaced with:
    Code:
    int cmp;
    
    ...
            cmp = strcmp(key, leaf->value);
            if(cmp == 0)
            {
                //If the values are equal, return the node
                return leaf;
            }
            else if(cmp < 0)
    thus halving the amount of strcmp done within the loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

    ok, down to two compiling errors

    I made a couple of changes in the code, basically replacing the search function prototype,
    Code:
    TreeNodePtr search(char *key, TreeNodePtr leaf)
    now its ok, I am getting two syntax errors in the function search()
    here is the function:
    Code:
    TreeNodePtr search(char *key, TreeNodePtr leaf)
    {
        //printf("Enter Employee name:");
       //gets(key);
        //If the leaf is pointing to something
        if(leaf!=NULL)
        {
      /* Allocate a temporary structure to help with our compareData() call */
      EMP tmpData;
      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;
        }
    }
    errors:
    operations.c:109: error: invalid type argument of ‘->’
    Code:
    strcpy(tmpData->fname, key);
    operations.c:110: error: incompatible type for argument 2 of ‘compareData’
    Code:
    switch (compareData(leaf->data, tmpData))
    and here is the compareData function:
    Code:
    int compareData( EMP *firstVal, EMP *secondVal)
    {
    
    switch (strcmp(firstVal->fname, secondVal->fname))
    {
     /* The two first names match */
     case 0:
      switch (strcmp(firstVal->lname, secondVal->lname))
      {
       /* The last name ALSO matches */
       case 0:
        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;
    }
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that strcmp is by no means guaranteed to return 1, 0 or -1, It could for example return -23 or 18 as a result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

    ok, I fixed the issues it compiles now

    Concerning another function that has to be implemented the delete_entry() function, which has to call the search function to locate the entry(if it exist) and delete( one occurance of it)
    any ideas, suggestion, will be highly appreciated
    will post the function if I have any issues with it

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21