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