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;
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;
}
}
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