#include #include #include #include #ifndef __WINAPI_CONSOLE_WRAPPER_H #define __WINAPI_CONSOLE_WRAPPER_H //#include #include int menue2(); int menue(); void gotoxy (int x, int y); void clrscr (void); #endif typedef struct avlnode *avltree; struct avlnode{ char word[15] ; char meaning[100]; avltree left; avltree right; int height; }; typedef avltree AvlTree; AvlTree find (AvlTree p, char* key); int Height(AvlTree P ); int Max( int Lhs, int Rhs ); AvlTree Insert( char word[],char meaning[], AvlTree T ); AvlTree FindMax( AvlTree T ); AvlTree FindMin( AvlTree T ); AvlTree Delete( char word[] , AvlTree T ); void postorder(AvlTree T); void preorder(AvlTree T); void inorder(AvlTree T); void readfromscreen(); void update(char word[]); void readfromfile(); void writetofile(AvlTree T,FILE*in); void converttosmall(char a[]); AvlTree mydic; // global variable int main() { mydic=NULL; int select; for(;;) {clrscr(); select=menue(); if(select==1){ readfromfile(); } else if(select==2){ readfromscreen(); } else if(select==3){ AvlTree node; printf("\n please enter the word u want to search \n"); char word[15]; scanf("%s",word); node=find (mydic, word); if(node!=NULL) printf(" %s : %s ",node->word,node->meaning); else printf("\n word not found "); getch(); } else if(select==4){ printf("\n enter the word u want to update it \n"); char word[15]; scanf("%s",word); update(word); } else if(select==5){ char word[15]; printf("\n enter the word u want to delet \n"); scanf("%s",word); Delete( word , mydic ); } else if(select==6){ clrscr(); int select2=menue2(); if(select2==1) preorder(mydic); else if(select2==2) inorder(mydic); else if(select2==3) postorder(mydic); getch(); } else if(select==7){ // write to file FILE *fp; char filename[15]; printf("\n please enter your file name to write in e.g (me.txt) "); scanf("%s",filename); fp=fopen(filename, "w"); writetofile(mydic,fp); } else if(select==8) break; // exit the program } return 0; } AvlTree find (AvlTree p, char* key) { AvlTree temp; temp = p; while(temp != NULL) { if(strcmp(temp->word, key)==0) return temp; else if(strcmp(temp->word, key)==1) temp = temp->left; else temp = temp->right; } return NULL; } int Height(AvlTree P ) { if( P == NULL ) return -1; else return P->height; } int Max( int Lhs, int Rhs ) { return Lhs > Rhs ? Lhs : Rhs; } AvlTree SingleRotateWithLeft( AvlTree K2 ) { AvlTree K1; K1 = K2->left; K2->left = K1->right; K1->right = K2; K2->height = Max( Height( K2->left ), Height( K2->right ) ) + 1; K1->height = Max( Height( K1->left ), K2->height ) + 1; return K1; /* New root */ } /* END */ /* This function can be called only if K1 has a right child */ /* Perform a rotate between a node (K1) and its right child */ /* Update heights, then return new root */ AvlTree SingleRotateWithRight( AvlTree K1 ) { AvlTree K2; K2 = K1->right; K1->right = K2->left; K2->left = K1; K1->height = Max( Height( K1->left ), Height( K1->right ) ) + 1; K2->height = Max( Height( K2->right ), K1->height ) + 1; return K2; /* New root */ } /* This function can be called only if K3 has a left */ /* child and K3's left child has a right child */ /* Do the left-right double rotation */ /* Update heights, then return new root */ AvlTree DoubleRotateWithLeft( AvlTree K3 ) { /* Rotate between K1 and K2 */ K3->left = SingleRotateWithRight( K3->left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); } /* END */ /* This function can be called only if K1 has a right */ /* child and K1's right child has a left child */ /* Do the right-left double rotation */ /* Update heights, then return new root */ AvlTree DoubleRotateWithRight( AvlTree K1 ) { /* Rotate between K3 and K2 */ K1->right = SingleRotateWithLeft( K1->right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); } AvlTree Insert( char word[],char meaning[], AvlTree T ) { if( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct avlnode ) ); if( T == NULL ) printf( "Out of space!!!" ); else { strcpy(T->word ,word); strcpy(T->meaning,meaning); T->height = 0; T->left = NULL; T->right =NULL; } } else if( strcmp(word, T->word )<0) { T->left = Insert(word,meaning, T->left ); if( Height( T->left ) - Height( T->right ) == 2 ) { if(strcmp(word , T->left->word )<0) T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); } } else if( strcmp(word, T->word )>0) { T->right = Insert( word,meaning, T->right ); if( Height( T->right ) - Height( T->left ) == 2 ) { if( strcmp(word , T->right->word )>0) T = SingleRotateWithRight( T ); else T = DoubleRotateWithRight( T ); } } /* Else X is in the tree already; we'll do nothing */ T->height = Max( Height( T->left ), Height( T->right ) ) + 1; return T; } /* END */ AvlTree Delete( char word[] , AvlTree T ) { AvlTree TmpCell; if( T == NULL ) printf( "Element not found" ); else if ( strcmp(word, T->word )<0) { /* Go left */ T->left = Delete(word, T->left ); if(Height(T->left)-Height(T->right)==2){ if(strcmp(word,T->left->word)<0) T=SingleRotateWithRight(T); else T=DoubleRotateWithRight(T); } } else if(strcmp(word, T->word ) >0) /* Go right */ T->right = Delete(word, T->right ); if(Height(T->right)-Height(T->left)==2) { if(strcmp(word,T->right->word)>0) T=SingleRotateWithLeft(T); else T=DoubleRotateWithLeft(T); } else { /* Found element to be deleted */ if( T->left && T->right ) /* Two children */ { /* Replace with smallest in right subtree */ TmpCell = FindMin( T->right ); strcpy(T->word , TmpCell->word); strcpy(T->meaning,TmpCell->meaning); T->right = Delete( T->word, T->right ); } else /* One or zero children */ { TmpCell = T; if( T->left == NULL ) /* Also handles 0 children */ T = T->right; else if( T->right == NULL ) T = T->left; free( TmpCell ); } } return T; } /* END */ AvlTree FindMin( AvlTree T ) { if( T == NULL ) return NULL; else if( T->left == NULL ) return T; else return FindMin( T->left ); } AvlTree FindMax( AvlTree T ) { if( T != NULL ) while( T->right != NULL ) T = T->right; return T; } void inorder(AvlTree T) { if(T!=NULL) { inorder(T->left); printf("\n %s\t%s\n ",T->word,T->meaning); inorder(T->right); } } void preorder(AvlTree T) { if(T!=NULL) { printf("\n %s\t%s\n",T->word,T->meaning); preorder(T->left); preorder(T->right); } } void postorder(AvlTree T) { if(T!=NULL) { postorder(T->left); postorder(T->right); printf("\n %s\t%s\n",T->word,T->meaning); } } void writetofile(AvlTree T,FILE*in) { if(T!=NULL) { writetofile(T->left,in); fprintf(in,"%s\t%s\n",T->word,T->meaning); writetofile(T->right,in); } } void readfromscreen(){ int i=0,s=0; printf("\n enter a word!\n"); char word[15]; char meaning[100]; char temp[18]; scanf("%s",word); converttosmall(word); AvlTree node; node=find (mydic, word); if(node!=NULL){ printf("\n this word is exist before \n you can't insert it again however you can update it from the main menue \n"); getch(); }else { printf("\n enter the meaning of this word << you may put more than one word >> \n when finish enter @ !\n"); scanf("%s",temp); // this is a condition to stop reading from screen while(strcmp(temp,"@")!=0){ for(s=0;s=65&&temp[s]<=90) temp[s]=(char)(temp[s]+32); meaning[i]=temp[s];} meaning[i]=' '; i++; scanf("%s",temp); } meaning[i]='\0'; mydic=Insert( word,meaning, mydic ); } } void update(char word[]){ AvlTree node; char temp[15],meaning[100]; int s=0,i=0; node=find (mydic, word); if(node==NULL) printf("no word in this name to update \n "); else { printf("enter the new meaning enter @ when finish \n "); scanf("%s",temp); // this is a condition to stop reading from screen while(strcmp(temp,"@")!=0){ for(s=0;smeaning,meaning); } } void readfromfile(){ int MAX_LEN=200; int i=0,s=0; FILE*fp ; char word[15],meaning[100]; char line[MAX_LEN], *result,*myword; char filename[15]; printf("\n please enter your file name e.g (me.txt) "); scanf("%s",filename); fp = fopen( filename ,"r" ) ; if( fp == NULL ) { // this when the file isn't exist printf("cannot open file" ) ; exit(0) ; } else{ // read a full line result = fgets(line,MAX_LEN,fp); while(result!=NULL){ i=0; myword=strtok(result," "); if(myword!=NULL){ if( myword[strlen(myword)-1]=='\n') myword[strlen(myword)-1]='\0'; converttosmall(myword); strcpy(word,myword);} myword=strtok(NULL," "); while(myword!=NULL) { if( myword[strlen(myword)-1]=='\n') myword[strlen(myword)-1]='\0'; { for(s=0;s=65&&myword[s]<=90) myword[s]=(char)(myword[s]+32); meaning[i]=myword[s];} meaning[i]=' '; i++; } myword=strtok(NULL," "); } meaning[i]='\0'; mydic=Insert( word,meaning, mydic ); result = fgets(line,MAX_LEN,fp); }} fclose(fp); } void converttosmall(char a[]){ int i=0; for(i=0;i=65&&a[i]<=90) a[i]=(char)(a[i]+32);} void gotoxy(int x, int y) { static HANDLE hStdout = NULL; COORD coord; coord.X = x; coord.Y = y; if(!hStdout) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); } SetConsoleCursorPosition(hStdout,coord); } void clrscr(void) { static HANDLE hStdout = NULL; static CONSOLE_SCREEN_BUFFER_INFO csbi; const COORD startCoords = {0,0}; DWORD dummy; if(!hStdout) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout,&csbi); } FillConsoleOutputCharacter(hStdout, ' ', csbi.dwSize.X * csbi.dwSize.Y, startCoords, &dummy); gotoxy(0,0); } int menue(){ char ch; do { gotoxy(38,10); printf(" Menue \n "); printf(" 1- read data from file \n "); printf(" 2- read data from screen \n "); printf(" 3- search \n "); printf(" 4- update \n "); printf(" 5- delet \n "); printf(" 6- print on a screen \n "); printf(" 7-write to file \n "); printf(" 8-Exit \n "); printf(" select a number "); gotoxy(60,20); ch=getch(); } while(!strchr("12345678",ch)); return (ch-48); } int menue2(){ char ch; do { gotoxy(38,10); printf(" select ur type of printing \n "); printf(" 1- preorder \n "); printf(" 2- inorder \n "); printf(" 3- postorder \n "); gotoxy(60,20); ch=getch(); } while(!strchr("12345678",ch)); return (ch-48); }