C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2008, 10:22 PM   #1
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
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.)
tabstop is offline   Reply With Quote
Old 12-03-2008, 10:27 PM   #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?
elsewhere is offline   Reply With Quote
Old 12-03-2008, 10:30 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
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.
tabstop is offline   Reply With Quote
Old 12-03-2008, 10:37 PM   #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?
elsewhere is offline   Reply With Quote
Old 12-03-2008, 10:49 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
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.
tabstop is offline   Reply With Quote
Old 12-04-2008, 02:52 AM   #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
elsewhere is offline   Reply With Quote
Old 12-04-2008, 02:55 AM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
Why do you believe that strcmp will always return -1, 0, or 1?
tabstop is offline   Reply With Quote
Old 12-04-2008, 02:57 AM   #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?
elsewhere is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
string not fully outputted when a type bool function is called. robstr12 C++ Programming 6 04-21-2006 08:49 PM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
Function not working sloopy C Programming 31 11-12-2005 08:08 PM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM


All times are GMT -6. The time now is 04:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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