/* * USAGE: * hw3 bst [-displayall] [file] * hw3 bst_delete [-displayall] string1 string2 ... stringN file * hw3 avl [-displayall] [file] * hw3 avl_delete [-displayall] string1 string2 ... stringN file * **/ int main (int argc, char *argv[]) { if(argc<2) { fprintf(stderr,"(malformed command)\n"); GUsage(); } if( !strcmp(argv[1],"bst") ) { #ifdef DEBUG1 fprintf(stdout,"----------BST----------------------\n"); #endif BST(argc,argv,0); ///<int operation = 0 = INSERT return 1; } if(!strcmp(argv[1],"bst_delete") ) { #ifdef DEBUG1 fprintf(stdout,"----------BST-------------\n"); #endif BST(argc,argv,1); ///< int operation = 1 = DELETE return 1; } if( !strcmp(argv[1],"avl") ) { #ifdef DEBUG1 fprintf(stdout,"----------AVL----------------------\n"); #endif AVL(argc,argv,0); return 1; } if(!strcmp(argv[1],"avl_delete") ) { #ifdef DEBUG1 fprintf(stdout,"----------AVL-------------\n"); #endif AVL(argc,argv,1); ///< 1 is delete operation return 1; } ///< TEST THE TREES #ifdef DEBUG0 if(!strcmp(argv[argc-1],"-LTest")) { int choice; cout<<"Enter some test case"<<endl; cin>>choice; switch(choice) { //case 1: cout<<"Test:CreateList and print"<<endl;test1(); break;//Just a createlist and print default:cout<<"please enter a valid testcase number"<<endl;break; } return 1; }
// -1 --- неуспешно включване, елементът вече го има // 0 --- успешно включване, но няма промяна във височината // 1 --- успешно включване и има увеличение на височината с 1 int insertAt(P p, T const& x) { if (!p) { // дъно BinaryTree<AVL>::assignFrom(p, AVL(x)); return 1; } // p --- валидна позиция if ((*p).data() == x) // Грешка! x вече го има return -1; // p && *p != x int result; if (x < (*p).data()) { // вмъкваме наляво result = insertAt(-p, x); if (result == 1) { (*p).balance()--; if ((*p).balance() == -2) { if ((*-p).balance() == 1) rotateLeft(-p); rotateRight(p); result = 0; } } } else { // вмъкваме надясно result = insertAt(+p, x); if (result == 1) { (*p).balance()++; if ((*p).balance() == 2) { if ((*+p).balance() == -1) rotateRight(+p); rotateLeft(p); result = 0; } } } // ако сме вмъкнали успешно и балансът се е получил 0 // значи нямаме промяна във височината if (result >= 0 && (*p).balance() == 0) result = 0; return result; }
static ret_t add_user (char *val, void *data) { return cherokee_avl_add_ptr (AVL(data), val, NULL); }