![]() |
| | #1 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,862
| |
| tabstop is offline | |
| | #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 | |
| | #3 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,862
| Quote:
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 | |
| | #4 | |
| Registered User Join Date: Dec 2008
Posts: 9
| Quote:
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 | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,862
| That doesn't particularly make any sense at all, unless you are not building your tree correctly in the first place. |
| tabstop is offline | |
| | #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;
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;
}
}
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;
}
}
}
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);
thanks for your time |
| elsewhere is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,862
| Why do you believe that strcmp will always return -1, 0, or 1? |
| tabstop is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |