So i have this university project and one of my tasks is to make a Binary Search Tree that does some operations !
Write a program in C that will process data for student IT graduates. The data should be loaded from a text file containing the following information for each student: Student Identity, Student Name, Surname, Grade in the Data Structure course.
The application initially reads the file and creates a BST in which each node maintains a record of the file. The BST is arranged in terms of student identity and implemented with dynamic memory management. After the BST has been created, the application displays a menu with the following options
1) Imaging the BST with in-order intersection
2) Search the student based on his / her identity
3) Modification of the student's registered data based on his / her identity
4) Delete a student record based on his / her identity
5) Exit the application
Specifically from what you see in my code I have trouble importing the data in BST correctly, I can not find a way to create a function that will process the data as well as I can not find a function that will delete a record from BST . Finally, the program crashes when I ask her to search for a student on the basis of his identity.
Here is the code :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 20
struct record
{
char id[10];
char firstName[20];
char lastName[20];
char score[10];
};
struct node
{
char *value;
struct node *leftptr;
struct node *rightptr;
};
typedef int (*Compare)(const char *, const char *);
void insert(char* key, struct node** leaf, Compare cmp)
{
int res;
if( *leaf == NULL )
{
*leaf = (struct node*) malloc( sizeof( struct node ) );
(*leaf)->value = malloc( strlen (key) +1 );
strcpy ((*leaf)->value, key);
(*leaf)->leftptr = NULL;
(*leaf)->rightptr = NULL;
}
else
{
res = cmp (key, (*leaf)->value);
if( res < 0)
{
insert( key, &(*leaf)->leftptr, cmp);
}
else if( res > 0)
{
insert( key, &(*leaf)->rightptr, cmp);
}
else
{
printf ("Key '%s' already in tree\n", key);
}
}
}
int CmpStr(const char *a, const char *b)
{
return (strcmp (a, b));
}
void in_order(struct node *root)
{
if( root != NULL )
{
in_order(root->leftptr);
printf(" %s\n", root->value);
in_order(root->rightptr);
}
}
void search(char* key, struct node* leaf, Compare cmp)
{
int res;
if( leaf != NULL )
{
res = cmp(key, leaf->value);
if( res < 0)
{
search( key, leaf->leftptr, cmp);
}
else if( res > 0)
{
search( key, leaf->rightptr, cmp);
}
else
{
printf("\n'%s' found!\n", key);
}
}
else
{
printf("\nNot in tree\n");
}
return;
}
char *input()
{
static char line[MAXLEN+1];
printf("Please enter a string : ");
fgets(line, sizeof line, stdin);
return (strtok(line, "\n" ));
}
void menu()
{
printf("Please select one of the following options : a) Display students in order\tb) Search a student via it's id\tc)Edit students info\td)Delete a student\td)EXIT\n");
}
int main()
{
FILE *fileptr;
struct node *p_root=NULL;
int i=0,idx=0;
fileptr=fopen("students.txt","r");
struct record *pinakas;
pinakas=(struct record *)malloc(17*sizeof(struct record));
if(fileptr)
{
while(!feof(fileptr))
{
fscanf(fileptr,"%[^ ] %[^ ] %[^ ] %[^ \n]\n", pinakas[idx].id, pinakas[idx].firstName, pinakas[idx].lastName, pinakas[idx].score);
insert(pinakas[idx].id, &p_root, (Compare)CmpStr);
idx++;
}
}
char *searchID;
char option;
option='x';
while(option!='e')
{
menu();
option=getch();
if(option=='a')
{
in_order(p_root);
}
else if(option=='b')
{
searchID=input();
search(searchID, &p_root, (Compare)CmpStr);
}
else if(option=='c')
{
printf("Edit student info\n");
}
else if(option=='d')
{
printf("Delete student from archives\n");
}
else if(option=='e')
{
exit(1);
}
}
fclose(fileptr);
free(pinakas);
return 0;
}
The .txt file is like this:
AB123456 ANNA SMITH 10.0
AB234567 BILL WHITE 3.0
AB345678 GEORGE REDD 7.0
...