AVLTree AVL_Insertion(ElementType x, AVLTree T) { if(!T) { T = (AVLTree)malloc(sizeof(AVLTreeNode)); T->Data = x; T->Height = 0; T->Left = T->Right = NULL; } else if(x < T->Data) { T->Left = AVL_Insertion(x, T->Left); if(GetHeight(T->Left) - GetHeight(T->Right) == 2) { if(x < T->Left->Data) T = SingleLeftRotation(T); else T = DoubleLeftRightRotation(T); } } else if(x > T->Data) { T->Right = AVL_Insertion(x, T->Right); if(GetHeight(T->Right) - GetHeight(T->Left) == 2) { if(x > T->Right->Data) T = SingRightRotation(T); else T = DoubleRightLeftRotation(T); } } T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1; return T; }
/** * This function handles any error conditions in the command line arguments * USAGE: * hw3 avl [-displayall] [file] * hw3 avl_delete [-displayall] string1 string2 ... stringN file * @param1: argc is of type integer and represents the num of arguments * @param2: argv[][] is the array of cmd line params as strings * @param3: operation is of value 0:Insertion operation or 1:Deletion operation * exits if there are any errors **/ void processAVLArguments(int argc,char* argv[],int operation) { if(isDisplaySet(argc,argv)) { DisplayFlag = true; #ifdef DEBUG1 fprintf(stdout,"DISPLAY FLAG: SET\n"); #endif } else { #ifdef DEBUG1 fprintf(stdout,"DISPLAY FLAG: NOT SET\n"); #endif } switch(operation) { case 0: ///< AVL Insert operation AVL_Insertion(argc,argv); break; case 1: ///<AVL Delete operation #ifdef DEBUG1 fprintf(stdout,"Operation:Deletion\n"); #endif AVL_Deletion(argc,argv); break; default: ///< This CASE SHOULD NEVER HAPPEN!!!! fprintf(stderr, "Unknown case for AVL\n"); exit(-1); break; } }//end of processAVLArguments