Denys Korovets
Denys Korovets

Reputation: 75

How to sort and search BST by name(string)?

I have to write a program that reads a .txt file into the tree and then it allows to perform specific operations with it. I'm stuck on the part where I need to sort tree by names and search by name as well, any input would be awesome. So, my input file is in the format :

3800 Lee, Victor; 2.8 
3000 Brown, Joanne; 4.0

And, my binary tree is in the format of:

typedef struct
 {
 int   id;
 char  name[MAX_NAME_LEN];
float gpa;
 } STUDENT;

typedef struct node
{
 STUDENT*        dataPtr;
 struct node* left;
 struct node* right;
} NODE;

typedef struct
{
 int   count;
 int  (*compare) (void* argu1, void* argu2); // Was provided by teacher, not really sure    how this works
 NODE*  root;
} BST_TREE;

ReadFile and insert functions are working just great, but I don't know how to implement search by name(string). I know that I have to use this part of code, but I'm really lost on how to do this.

/*  ====================== compareStu ======================
    Compare two student names's and return low, equal, high.
    Pre  stu1 and stu2 are valid pointers to students
    Post return low (-1), equal (0), or high (+1)
*/

int  compareStu   (void* stu1, void* stu2)
{
//  Local Definitions
STUDENT s1 = *((STUDENT*)stu1);
    STUDENT s2 = *((STUDENT*)stu2);;

//  Statements
if ( s1.name < s2.name)
      return -1;

if ( s1.name == s2.name)
      return 0;

return +1;
}   // compareStu

I've been told that I need to do specific set of actions, but I didn't understand them as well, any detailed explanation or step-by step would be very useful:

"You simply set the compare element of the BST_TREE to compareStu: BST_TREE root = { 0, cmp_by_name, 0 }; You then call your functions with &root as the root of the tree. This is instead of the comparison function you were provided with. You should build and search the tree with a single comparison function; using different comparators at different times will cause chaos." (c)Jonathan Leffler

"The code you show already will use the cmp_by_name() function if you set it as the compare in the tree when you create the tree. I can't show you any more; you've not shown all your code and I'm not prepared to write your code for you just to demonstrate how I'd do it. I'll note that the name _retrieve() is not a good one for you to use; you should treat names starting with _ as 'reserved for the implementation' (it is more nuanced than that, but not by much and the simpler rule is easier to remember). You could simplify the code by using if (root == NULL) return 0; else if (…)"(c)Jonathan Leffler

Therefore:

1) How do I search for a name in a tree?

/*  ====================== findStu ======================
    Finds a student and prints id and gpa.
    Pre  student id
    Post student data printed or error message
*/

void findStu (BST_TREE* tree)
{
//  Local Definitions
STUDENT  s;
STUDENT* stuPtr;

//  Statements
printf("Enter student name that you want to find: ");
fflush(stdin);
gets(s.name);
fflush(stdin);

stuPtr = (STUDENT*)BST_Retrieve (tree, &s);
if (stuPtr)
   {
    printf("Student id:   %04d\n",  stuPtr->id);
    printf("Student name: %s\n",    stuPtr->name);
    printf("Student gpa:  %4.1f\n", stuPtr->gpa);
   } // if
else
   printf("Student %s has NOT been found in the file\n", s.name);
}   // findStu

2) How to sort a BST by name? I know how the sorting by integers works and how values are compared with each others, but with names I have no clue.

Upvotes: 0

Views: 1923

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

Sorting by name is as easy as sorting by integer with the strcmp function:

int strcmp ( const char * str1, const char * str2 );

it returns 0 if the strings are equal, a number bigger 0 if the first string is lexically "bigger" and -1 otherwise.

Your compareStu function should use strcmp to return a value.

Upvotes: 1

Related Questions