Thread: Student Records Using Binary Search Tree

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Student Records Using Binary Search Tree

    I need to make a student database where I can enter, edit specific student, delete specific student, view all students, and view a specific student using a BST. I'm really lost and this is what I have so far.

    Code:
    #include <stdio.h>
     #include <stdbool.h>
     #include <conio.h>
     #include <windows.h>
     typedef struct{
            char studID[6];
            char fname[30];
            char lname[30];
            char mname[3];
            float qpa;
    }STUD;
    
     typedef struct node *nd;
     struct node
     {
            STUD stu;
            nd left;
            nd rigth;
    }NODE;
    
    
    void inputStud(nd *);
    bool treeEmpty(nd *);
    void deleteNode(nd *);
    void editStud(nd *);
    void viewAll(nd);
    nd searchNode(nd *,nd *,char[]);
    
    int main(void)
    {
        int ans,c;
        bool e;
        nd root = NULL;
    
        do
        {
             system("cls");
             printf("1] Add New Student Record\n");
             printf("2] Edit Student Record\n");
             printf("3] View Specific Record\n");
             printf("4] Display All\n");
             printf("5] Delete Student Record\n");
             printf("6] Exit\n");
             printf("Pick your choice: ");
             scanf("%d",&ans);
             switch(ans)
             {
                  case 1:     inputStud(&root);
                              break;
                  case 2:     editStud(&root);
                              break;
                  case 3:     viewAll(root);
                              getch();
                              break;
                  case 4:     viewAll(root);
                              getch();
                              break;
                  case 5:     deleteNode(&root);
                              break;
                  case 6:     printf("\n\nclosing the program in 5 seconds....");
                              sleep(5000);
    
             }
        }while (ans != 6);
        return 0;
    }
    
    
    void inputStud(nd *root)
    {
         nd t, t1, temp;
         char sID[6];
         char lnam[20];
         char fnam[30];
         char mnam[3];
         float qp;
         STUD st;
    
         printf("Enter Student ID: /n");
         gets(sID);
         printf("Enter Student's Last Name: /n");
         gets(lnam);
         printf("Enter Student's First Name: /n");
         gets(fnam);
         printf("Enter Student's Middle Initial(s): /n");
         gets(mnam);
         printf("Enter Student's QPA: /n");
         scanf("%f", &qp);
         strcpy(st.studID, sID);
         strcpy(st.lname, lnam);
         strcpy(st.fname, fnam);
         strcpy(st.mname, mnam);
         st.qpa=qp;
    
         temp = malloc(sizeof(NODE));
         temp -> stu = st;
         temp -> left = NULL;
         temp -> rigth = NULL;
    
         if (*root == NULL)
         {
              *root = temp;
         }
         else
         {
              t = *root;
              while (t != NULL)
              {
                   t1 = t;
                   if (strcmp(temp -> stu.studID,t -> stu.studID)==0)
                        t = t -> left;
                   else
                        t = t -> rigth;
              }
              if (strcmp(temp -> stu.studID,t1 -> stu.studID) < 0)
                  t1 -> left = temp;
              else
                  t1 -> rigth = temp;
         }
         t1 = NULL;
         t = NULL;
         temp = NULL;
         return;
    }
    void editStud(nd *root)
    {
         char sID[6];
         char lnam[20];
         char fnam[30];
         char mnam[3];
         float qp;
         STUD st;
         
         printf("Please enter the ID of the Student you would like to edit: ");
         gets(sID);
         
         if(strcmp(sID,st.studID)==0)
         {
             printf("Enter Student ID: /n");
             gets(sID);
             printf("Enter Student's Last Name: /n");
             gets(lnam);
             printf("Enter Student's First Name: /n");
             gets(fnam);
             printf("Enter Student's Middle Initial(s): /n");
             gets(mnam);
             printf("Enter Student's QPA: /n");
             scanf("%f", &qp);
             strcpy(st.studID, sID);
             strcpy(st.lname, lnam);
             strcpy(st.fname, fnam);
             strcpy(st.mname, mnam);
             st.qpa=qp;
         }
         else
         {
             printf("Student not found..");
         }
         return;
    }                
         
    void deleteNode(nd *root)
    {
         char iD[6];
         bool e,lmoved = false;
         nd t,t1,t2;
         STUD st;
    
         e = treeEmpty(root);
         if (e)
         {
               printf("\n\nSorry, the tree is empty...");
               sleep(3000);
         }
         else
         {
             printf("Please enter ID of student to be deleted: ");
             gets(iD);
             t = searchNode(root,&t1,iD[]);
             if (t == NULL)
             {
                   printf("\n\n%s not found...",iD);
                   sleep(2000);
             }
             else
             {
                    
                    if ((t -> left == NULL)&&(t -> rigth == NULL))
                    {
                           if (strcmp( iD, t1 -> stu.studID) <0)
                               t1 -> left = NULL;
                           else
                               t1 -> rigth = NULL;
                    }
                    else if ((t -> left == NULL)&&(t -> rigth != NULL))
                    {
                     
                           if (strcmp( iD,t1 -> stu.studID) <0)
                               t1 -> left = t -> rigth;
                           else
                               t1 -> rigth = t -> rigth;
                    }
                    else if ((t -> left != NULL)&&(t -> rigth == NULL))
                    {
    
                           if (strcmp( iD,t1 -> stu.studID) <0)
                               t1 -> left = t -> left;
                           else
                               t1 -> rigth = t -> left;
                    }
                    else
                    {
    
                       t1 = t;
                       t = t1 -> rigth;
                       while(t -> left != NULL)
                       {
                            t2 = t;
                            t = t -> left;
                            lmoved = true;
                       }
                       t1 -> stu.studID = t -> stu.studID;
                       if (t -> rigth == NULL)
                       {
                            if (lmoved)
                                t2 -> left = NULL;
                            else
                                t1 -> rigth = NULL;
                       }
                       else
                       {
                             if (lmoved)
                                t2 -> left = t -> rigth;
                             else
                                 t1 -> rigth = t -> rigth;
                       }
                    }
                    free(t);
                    t1 = NULL;
                    t2 = NULL;
    
             }
         }
         return;
    }
    
    nd searchNode(nd *r,nd *t1,char iD[], nd root)
    {
          nd t;
          t = *r;
          STUD st;
          while((t != NULL) && (strcmp(t -> stu.studID, iD)== 0)
          {
               *t1 = t;
               if ((strcmp( iD,t1 -> stu.studID) <0)
                  t = t -> left;
               else
                   t = t -> rigth;
          }
          return t;
    }
    
    bool treeEmpty(nd *r)
    {
         bool e = false;
         if (*r == NULL)
               e = true;
         return e;
    }
    
    void viewAll(nd ptr)
    {
         if (ptr != NULL)
         {
              printf("Student ID: %s", stu.studID );
              printf("First Name: %s", stu.fName);
              printf("Middle Initial: %s", stu.mName);
              printf("Last Name: %s", stu.lName);
              printf("QPA: %f", stu.qpa);
    
        }
         return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    How much code did you write before you tried compiling? All 283 lines? That is the wrong way to program. Plan everything out on paper, and implement in small chunks. Literally, 5-10 lines at a time. Compile (at maximum warning level) and fix all errors and warnings. Test your code. Don't move onto the next 5-10 lines until everything works perfectly. My 3 minute analysis of your code so far (in no particular order):

    • conio.h is an ancient and non-standard (and non-portable) header. Don't use it if you can avoid it. The only function you're using from it is getch(). Use the standard function getchar() instead.
    • You don't really need to sleep, and thus could remove those and do without windows.h, which is also non-standard and non-portable. I removed those since they were preventing me from actually helping you (I use Linux and can't help people with Windows specific code).
    • You need to #include the right headers for all the functions you use. You're missing at least stdlib.h and string.h, maybe more.
    • You shouldn't typedef a pointer (i.e. typedef struct node* as nd), especially as a relative newbie. It is too easy for you to lose track of how many levels of indirection there is in a type/variable.
    • You have some egregious syntax errors, missing/excess parentheses in your searchNode function, incorrect array usage when you call searchNode.
    • Your definition of searchNode is different from the prototype and how you call it. All 3 of those must match in return type as well as type, order and number of parameters.
    • You chose poor names, like "nd" for a type name. What's wrong with node? What does e stand for? t? r? It makes your code difficult to read/follow. If it's hard to follow, it's easy to make mistakes and hard to find or fix them. Never sacrifice clarity to save a few keystrokes.
    • Don't use magic numbers, define some constants, like #define STUDENT_ID_LEN 6 or #define LAST_NAME_LEN 20.
    • Don't use gets. Read this link.
    • You spelled "right" wrong (swapped the t and the h). Everywhere. At least you're consistent .
    • You can't assign arrays with =. You do this around line 226. You need to use something like strcpy.
    • You have your struct and struct pointers screwed up in searchNode. nd is of type node *, so nd* is of type node **. This is likely due to the typedef'ing a pointer.
    • You have at least one undeclared variable


    If there's more, it's hard for me to tell, amidst all the above issues. Fix those, it should keep you busy for a while.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    If your viewall() is supposed to view all the student records, you can add recursion to travel down the branches. Normally you could I mean, I think it might be the case here if everything else were to work correctly.

    Code:
    void viewAll(nd ptr)
    {
         if (ptr != NULL)
         {
              viewall(ptr->left);
              printf("Student ID: %s", stu.studID );
              printf("First Name: %s", stu.fName);
              printf("Middle Initial: %s", stu.mName);
              printf("Last Name: %s", stu.lName);
              printf("QPA: %f", stu.qpa);
              viewall(ptr->right);
     
        }
         return;
    }
    


    You should really break your program down per function and create sample input for each one. Add tons of error checking, then go through it with a debugger with the warnings turned up high.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. implementing a student database using a binary tree
    By atchy6 in forum C Programming
    Replies: 1
    Last Post: 05-02-2012, 08:44 AM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM