Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<ctype.h>
- typedef struct node {
- int id;
- char *name;
- struct node *right;
- struct node *left;
- } Node;
- void menu(void);
- void Print(Node**,int);
- Node* Insert(Node**,char*,int);
- char* lower_all(char*);
- int Load(Node**,char*);
- int Save(Node**,FILE*);
- Node* Search(Node**,char*);
- Node* Delete(Node**,char*);
- int isNumeric(const char*);
- Node* FindMin(Node**);
- int main(void){
- Node* root=NULL;
- int id,c;
- char name[50];
- menu();
- while(1){
- printf("\n[I]Choice [Main Menu] > ");
- scanf("%d",&c); //REPAIR
- system("clear");
- switch(c){
- case 0:
- menu();break;
- case 1:
- //load data
- printf("\n\n[~]Loading Data from an external file \n\n");
- printf("[I]File to load data from : ");getchar();
- fgets(name,sizeof(name)-1,stdin);name[strlen(name)-1]='\0';
- if(Load(&root,name))printf("[~]Done loading data from %s .\n\n",name);
- else printf("[!]File not found !\n\n");
- break;
- case 2:
- printf("\n\n[~]Data Insertion \n\n[I]Student's Name : ");getchar();
- fgets(name,sizeof(name)-1,stdin);name[strlen(name)-1]='\0';
- printf("[I]Student's ID : ");
- scanf("%d",&id);
- Insert(&root,name,id);
- printf("[~]Done !\n\n");
- break;
- case 3:
- printf("[~]Data Look up \n\n");
- //Search entry
- printf("[~]Search :\n\n[1]By Name .\n[2]By ID .\n\n[I]Choice [Search] > ");scanf("%2s",name);getchar();
- if(atoi(name)>=1&&atoi(name)<=2){printf("[I]Student's %s",(atoi(name)==1)?("Name : "):("ID : "));scanf("%30[^\n]s",name);printf("\n%-20s%-20s\n%-40s\n","Name","ID","_________________________");Node* lol=Search(&root,name);
- printf("\n\n\t%-20s%-20d\n",lol->name,lol->id);}
- else printf("\n[!]Invalid Choice !\n\n");
- break;
- case 4:
- printf("[~]Data Deletion \n\n");
- //Delete entry
- printf("[~]Delete :\n\n[1]By Name .\n[2]By ID .\n\n[I}Choice [Delete] > ");scanf("%2s",name);getchar();
- if(atoi(name)>=1&&atoi(name)<=2){printf("[I]Student's %s",(atoi(name)==1)?("Name : "):("ID : "));scanf("%30[^\n]s",name);Delete(&root,name);}
- else printf("\n[!]Invalid Choice !\n\n");
- break;
- case 5:
- printf("[~]Displaying Entries \n\n");
- printf("[~]Display in :\n[1]Pre-Order\n[2]Post-Order\n[3]In-Order\n\n[I]Choice [Display] > ");
- scanf("%d",&id); //REPAIR
- if(id>=1 && id<=3){
- printf("\n%-20s%-20s\n%-40s\n","Name","ID","_________________________");
- Print(&root,id);}
- else printf("[!]Invalid Choice !\n\n");
- printf("\n#####################################\n\n");break;
- case 6:
- printf("\n\n[~]Saving Data to file \n\n");
- printf("[~]Please name the file you want to save data at : ");
- scanf("%20s",name);
- if(Save(&root,fopen(name,"w")))printf("\n[~]Done !\n\n");
- else printf("\n[!]File cannot be created due to directory permissions or invalid renaming characters used !\n\n");
- break;
- case 7:
- printf("[~]Exiting ...\n\n");
- return 0;
- default:
- printf("[!]Invalid Choice !\n\n");
- }}}
- void menu(void){
- printf("###########################################################\n");
- printf("### BST Database v0.1 ###\n");
- printf("###########################################################\n\n");
- printf("Usage :\n");
- printf("*******\n");
- printf("1)Load Data from an external file .\n");
- printf("2)Insert Data entry .\n");
- printf("3)Search for an entry .\n");
- printf("4)Delete an existing entry .\n");
- printf("5)Display current B-tree .\n");
- printf("6)Save Data into a file .\n");
- printf("7)Exit application .\n");
- printf("0)Re-print menu .\n\n");
- }
- int isNumeric (const char * s){
- if (s == NULL || *s == '\0' || isspace(*s))return 0;
- char *p;
- strtod (s, &p);
- return *p == '\0';
- }
- Node* FindMin(Node** temp){
- if((*temp)==NULL) return NULL;
- else if((*temp)->left==NULL) return *temp;
- else return FindMin(&(*temp)->left);
- }
- /*
- Node* Delete(Node** tmp,char* data){
- Node* temp;
- if(*tmp){
- if(isNumeric(data)){
- if((*tmp)->id==atoi(data)){
- //Deletion process
- if(((*tmp)->left) && ((*tmp)->right )){
- temp=FindMin(&(*tmp));
- strcpy((*tmp)->name,(temp)->name);
- (*tmp)->id=(temp)->id;
- temp=Delete(&(*tmp)->right,(*tmp)->name);
- }else{
- temp=(*tmp);
- if(!((*tmp)->left))(*tmp)=(*tmp)->right;
- else if(!((*tmp)->right))(*tmp)=(*tmp)->left;
- //temp=NULL;
- }}
- Delete(&(*tmp)->left,data);Delete(&(*tmp)->right,data);
- }else{
- if(!strcmp((*tmp)->name,data)){
- //Deletion process
- if(((*tmp)->left) && ((*tmp)->right )){
- temp=FindMin(&(*tmp));
- strcpy((*tmp)->name,(temp)->name);
- (*tmp)->id=(temp)->id;
- temp=Delete(&(*tmp)->right,(*tmp)->name);
- }else{
- temp=(*tmp);
- if(!((*tmp)->left))(*tmp)=(*tmp)->right;
- else if(!((*tmp)->right))(*tmp)=(*tmp)->left;
- //temp=NULL;
- }}
- Delete(&(*tmp)->left,data);Delete(&(*tmp)->right,data);
- }
- }return (*tmp);}
- */
- Node* Delete(Node** tmp,char* data){
- if(isNumeric(data)){
- //Delete using id
- if(!(*tmp));
- else if(atoi(data)>(*tmp)->id) (*tmp)->right=Delete(&(*tmp)->right,data);
- else if(atoi(data)<(*tmp)->id) (*tmp)->left=Delete(&(*tmp)->left,data);
- else if((*tmp)->left==NULL) (*tmp)=(*tmp)->left;
- else if((*tmp)->right==NULL) (*tmp)=(*tmp)->right;
- else {
- Node* temp=FindMin(&(*tmp)->right);
- (*tmp)->id=temp->id;
- strcpy((*tmp)->name,temp->name);
- char buff[10]={};
- sprintf(buff,"%d",temp->id);
- (*tmp)->right=Delete(&(*tmp)->right,buff);
- }
- return (*tmp);
- }//isNumeric If
- else{
- //Delete using name
- if(!(*tmp));
- else if(atoi(data)>(*tmp)->id) (*tmp)->right=Delete(&(*tmp)->right,data);
- //else if(atoi(data)<(*tmp)->id) (*tmp)->left=Delete(&(*tmp)->left,data);
- else if((*tmp)->left==NULL) (*tmp)=(*tmp)->left;
- else if((*tmp)->right==NULL) (*tmp)=(*tmp)->right;
- else {
- Node* temp=FindMin(&(*tmp)->right);
- (*tmp)->id=temp->id;
- strcpy((*tmp)->name,temp->name);
- char buff[10]={};
- sprintf(buff,"%d",temp->id);
- (*tmp)->right=Delete(&(*tmp)->right,buff);
- }
- return (*tmp);
- }//isNumeric Else
- }//Function End
- Node* Search(Node** tmp,char* data){
- if(*tmp){
- if(isNumeric(data)){
- if((*tmp)->id==atoi(data)){
- printf("%-20s%-20d\n",(*tmp)->name,(*tmp)->id);}
- Search(&(*tmp)->left,data);Search(&(*tmp)->right,data);
- }else{
- if(!strcmp((*tmp)->name,data)){
- printf("%-20s%-20d\n",(*tmp)->name,(*tmp)->id);}
- Search(&(*tmp)->left,data);Search(&(*tmp)->right,data);
- }}return (*tmp);}
- int Save(Node** tmp,FILE* fname){
- if(!fname)return 0;
- else{
- if(*tmp){
- fprintf(fname,"%s ,%d\n",(*tmp)->name,(*tmp)->id);
- Save(&(*tmp)->left,fname);Save(&(*tmp)->right,fname);
- }}return 1;}
- int Load(Node** tmp,char* fname){
- FILE *fp=fopen(fname,"r");
- if(!fp)return 0;
- else{
- char name[50],id[10];
- while(!feof(fp)){
- fscanf(fp,"%30[^,]s",name);
- fscanf(fp,",%4s\n",id);
- Insert(&(*tmp),name,atoi(id));
- }return 1;}}
- Node* Insert(Node** temp,char *N,int I)
- {
- Node* create_node(char *N,int I)
- {
- Node *temp=(Node*)malloc(sizeof(Node));
- temp->name=(char*)malloc(strlen(N)+1); //HERE I AM
- strcpy(temp->name,N);
- temp->id=I;
- temp->left=NULL,temp->right=NULL;
- return temp;
- }
- if (!(*temp))*temp=create_node(N,I);
- else if ((*temp)->id >= I) (*temp)->left=Insert(&(*temp)->left,N,I);
- else (*temp)->right=Insert(&(*temp)->right,N,I);
- return *temp;
- }
- void Print(Node** temp,int c)
- {
- if(c==1){
- if (*temp)
- {
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- Print(&(*temp)->left,c);Print(&(*temp)->right,c);
- }}
- else if(c==2){
- if (*temp)
- {
- Print(&(*temp)->left,c);Print(&(*temp)->right,c);
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- }}
- else {
- if (*temp)
- {
- Print(&(*temp)->left,c);
- printf("\n%-20s%-20d\n",(*temp)->name,(*temp)->id);
- Print(&(*temp)->right,c);
- }}}
- char* lower_all(char *temp)
- {
- for(int i=0;i<strlen(temp);temp[i]=tolower(temp[i]),i++); return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement