title: How to sort and search BST by name(string)?
body: <p>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 :</p>
  
  <pre><code>3800 Lee, Victor; 2.8 
  3000 Brown, Joanne; 4.0
  </code></pre>
  
  <p>And, my binary tree is in the format of:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>/*  ====================== 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 &lt; s2.name)
        return -1;
  
  if ( s1.name == s2.name)
        return 0;
  
  return +1;
  }   // compareStu
  </code></pre>
  
  <p>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:</p>
  
  <p>"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 &amp;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</p>
  
  <p>"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</p>
  
  <p>Therefore:</p>
  
  <p>1) How do I search for a name in a tree?</p>
  
  <pre><code>/*  ====================== 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, &amp;s);
  if (stuPtr)
     {
      printf("Student id:   %04d\n",  stuPtr-&gt;id);
      printf("Student name: %s\n",    stuPtr-&gt;name);
      printf("Student gpa:  %4.1f\n", stuPtr-&gt;gpa);
     } // if
  else
     printf("Student %s has NOT been found in the file\n", s.name);
  }   // findStu
  </code></pre>
  
  <p>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. </p>