void Print_opr_prod_tree(struct unexp_tnode *Pntr) { while (Pntr != NULL) { printf(","); Print_tree(Pntr); Pntr = Pntr->next; } }
void Print_art_word(struct unexp_tnode *Pntr) { while (Pntr != NULL) { printf(","); Print_tree(Pntr); Pntr = Pntr->next; } }
int test_BTDispose(tBTNodePtr* TempTree) { solved=TRUE; BTDisposeTree(TempTree); if (!solved) { printf("Operace BTDispose() nebyla implementovana \n"); return(FALSE); } else { Print_tree(*TempTree); return(TRUE); } }
int test_BSTInsert (tBSTNodePtr* TempTree, char K, int Content) { solved=TRUE; BSTInsert(TempTree, K, Content); if (!solved) { printf("Operace BSTInsert() nebyla implementovana \n"); return(FALSE); } else { Print_tree(*TempTree); return(TRUE); } }
int test_BSTDelete (tBSTNodePtr* TempTree, char K) { solved=TRUE; BSTDelete(TempTree,K); if (!solved) { printf("Operace BSTDelete() nebyla implementovana \n"); return(FALSE); } else { Print_tree(*TempTree); return(TRUE); } }
int test_BSTInit(tBSTNodePtr *TempTree) { solved=TRUE; BSTInit(TempTree); if (!solved) { printf("Operace InitList() nebyla implementovana \n"); return(FALSE); } else { Print_tree(*TempTree); return(TRUE); } }
void Print_tree(struct unexp_tnode *Pntr) { if (Pntr != NULL) { if ((Pntr->op != SMALL_LETTER) && (Pntr->op != SCALAR) && (Pntr->op != OPERATOR_PROD) && (Pntr->op != ARTIFICIAL_WORD)) { printf("%c(",OPR_SYMBOL[Pntr->op]); Print_tree(Pntr->operand1); if (Pntr->operand2 != NULL) { printf(","); Print_tree(Pntr->operand2); if (Pntr->operand3 != NULL) { printf(","); Print_tree(Pntr->operand3); } } printf(")"); } else if (Pntr->op == SMALL_LETTER) printf("%c",Pntr->s_letter); else if (Pntr->op == SCALAR) printf("%d",Pntr->scalar_num); else if (Pntr->op == OPERATOR_PROD) { printf("%c(",OPR_SYMBOL[Pntr->op]); Print_tree(Pntr->operand1); Print_opr_prod_tree(Pntr->operand2); printf(")"); } else if (Pntr->op == ARTIFICIAL_WORD) { printf("%c(",OPR_SYMBOL[Pntr->op]); Print_tree(Pntr->operand1); Print_art_word(Pntr->operand2); printf(")"); } } }
int main(int argc, char *argv[]) { printf("Nerekurzivni operace nad binarnim stromem\n"); printf("=========================================\n"); printf("[TEST01]\n"); printf("Test inicializace....\n"); test_BTInit(&TempTree); printf("[TEST02]\n"); printf("Zkusime zrusit strom\n"); printf("~~~~~~~~~~~~~~~~~~~~\n"); test_BTDispose(&TempTree); printf("[TEST03]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order na prazdny strom\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BTPreorder(TempTree); test_BTPostorder(TempTree); test_BTInorder(TempTree); printf("[TEST04]\n"); printf("Vlozime prvek s hodnotou 8\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); Content=8; test_BTInsert(&TempTree,Content); Print_tree(TempTree); printf("[TEST05]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order na prazdny strom\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BTPreorder(TempTree); test_BTPostorder(TempTree); test_BTInorder(TempTree); printf("[TEST06]\n"); printf("Zkusime zrusit strom\n"); printf("~~~~~~~~~~~~~~~~~~~~\n"); test_BTDispose(&TempTree); printf("[TEST07]\n"); printf("Vlozime prvek s hodnotou 8\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); Content=8; test_BTInsert(&TempTree,Content); Print_tree(TempTree); printf("[TEST08]\n"); printf("Vlozeni dalsich prvku\n"); printf("~~~~~~~~~~~~~~~~~~~~~\n"); printf("Vlozime prvek 4\n"); Content=4; test_BTInsert(&TempTree,Content); printf("Vlozime prvek 12\n"); Content=12; test_BTInsert(&TempTree,Content); Print_tree(TempTree); printf("[TEST09]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order na prazdny strom\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BTPreorder(TempTree); test_BTPostorder(TempTree); test_BTInorder(TempTree); printf("[TEST10]\n"); printf("Pokus o opetovne vlozeni prvku 8 (nesmi dojit ke zmene stromu) \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); Content=8; test_BTInsert(&TempTree, Content); Print_tree(TempTree); printf("[TEST11]\n"); printf("Pokus o opetovne vlozeni prvku 12 (nesmi dojit ke zmene stromu) \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); Content=12; test_BTInsert(&TempTree, Content); Print_tree(TempTree); printf("[TEST12]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); test_BTPreorder(TempTree); test_BTInorder(TempTree); test_BTPostorder(TempTree); printf("[TEST13]\n"); printf("Vlozeni dalsich prvku ve vhodnem poradi\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("Vlozime prvek 2\n"); Content=2; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 6\n"); Content=6; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 10\n"); Content=10; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 14\n"); Content=14; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 1\n"); Content=1; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 3\n"); Content=3; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 5\n"); Content=5; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 7\n"); Content=7; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 9\n"); Content=9; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 11\n"); Content=11; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 13\n"); Content=13; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 15\n"); Content=15; test_BTInsert(&TempTree, Content); Print_tree(TempTree); printf("[TEST14]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); test_BTPreorder(TempTree); test_BTInorder(TempTree); test_BTPostorder(TempTree); printf("[TEST15]\n"); printf("Zrusime cely strom\n"); printf("~~~~~~~~~~~~~~~~~~\n"); test_BTDispose(&TempTree); printf("[TEST16]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); test_BTPreorder(TempTree); test_BTInorder(TempTree); test_BTPostorder(TempTree); printf("[TEST17]\n"); printf("Nyni si vytvorime jiny strom.\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("Vlozime prvek 10\n"); Content=10; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 8\n"); Content=8; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 12\n"); Content=12; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 14\n"); Content=14; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 1\n"); Content=1; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 2\n"); Content=2; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 3\n"); Content=3; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 4\n"); Content=4; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 5\n"); Content=5; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 6\n"); Content=6; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 13\n"); Content=13; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 15\n"); Content=15; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 7\n"); Content=7; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 9\n"); Content=9; test_BTInsert(&TempTree, Content); printf("Vlozime prvek 11\n"); Content=11; test_BTInsert(&TempTree, Content); Print_tree(TempTree); printf("[TEST18]\n"); printf("Provedeme pruchod Pre-order, In-order a Post-order \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); test_BTPreorder(TempTree); test_BTInorder(TempTree); test_BTPostorder(TempTree); printf("[TEST19]\n"); printf("Pokus o opetovne vlozeni prvku 9 (nesmi dojit ke zmene stromu) \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); Content=9; test_BTInsert(&TempTree, Content); Print_tree(TempTree); printf("[TEST20]\n"); printf("Zrusime cely strom\n"); printf("~~~~~~~~~~~~~~~~~~\n"); test_BTDispose(&TempTree); printf("------------------------------ konec -------------------------------------\n"); return(0); }
struct polynomial *Create_poly(char In_str[], int *In_str_len, int *Out_of_memory) { /*static struct unexp_tnode *temp;*/ struct unexp_tnode *Unexp_tree1; struct unexp_tnode *Unexp_tree2; struct unexp_tnode *Exp_tree1; struct unexp_tnode *Exp_tree2; struct unexp_tnode *Exp_tree3; struct unexp_tnode *Exp_tree4; struct polynomial *poly; char *exp_poly_str; static char **Exp_poly_str_ptr; /* Exp_poly_str may be expanded! */ static int Maxsize_exp_poly_str; int Modified = FALSE; /* Has tree been changed in this call? */ #if DEBUG_EXP int count; /* Debugging information. No of calls to Simplify */ #endif /*int i;*/ int first_time = TRUE; /* Allocate space for exp_poly_str first_time */ if (first_time) { exp_poly_str = (char*) Mymalloc(EXP_STR_LEN); Maxsize_exp_poly_str = EXP_STR_LEN; first_time = FALSE; } exp_poly_str[0] = '\0'; Exp_poly_str_ptr = &exp_poly_str; Unexp_tree1 = NULL; Unexp_tree2 = NULL; Exp_tree1 = NULL; Exp_tree2 = NULL; Exp_tree3 = NULL; Exp_tree4 = NULL; if (setjmp(env) != 0) { Free_tnode_tree(Unexp_tree1); Free_tnode_tree(Unexp_tree2); Free_tnode_tree(Exp_tree1); Free_tnode_tree(Exp_tree2); Free_tnode_tree(Exp_tree3); Free_tnode_tree(Exp_tree4); *Out_of_memory = TRUE; printf("Command unsuccessfull.\n"); return(NULL); } /* Parse tree for the polynomial string In_str entered by user */ Unexp_tree1 = Create_parse_tree(In_str); if (Unexp_tree1 == NULL) return(NULL); #if DEBUG_EXP Print_tree(Unexp_tree1); printf("\n"); #endif /* Expand the Parse tree by using formulae for operators */ Unexp_tree2 = Expand_parse_tree(Unexp_tree1); #if DEBUG_EXP Print_tree(Unexp_tree2); printf("\n"); #endif /* Simplify the Expanded tree i.e apply distributive laws. */ Exp_tree1 = Simplify_parse_tree(Unexp_tree2,&Modified); #if DEBUG_EXP count = 1; printf("simplified tree %d\n",count); Print_tree(Exp_tree1); printf("\n"); #endif while (Modified) { Modified = FALSE; Exp_tree2 = Simplify_parse_tree(Exp_tree1,&Modified); #if DEBUG_EXP count++; printf("count = %d Modified = %d\n",count,Modified); printf("simplified tree\n"); Print_tree(Exp_tree2); printf("\n"); printf("freeing Exp_tree1\n"); #endif Free_tnode_tree(Exp_tree1); Exp_tree1 = Exp_tree2; Exp_tree2 = NULL; } /* No more simplifications needed! */ Modified = FALSE; /* Get rid of Unary minus by multiplying that subtree by -1. */ Exp_tree3 = Elim_subtraction(Exp_tree1,&Modified); #if DEBUG_EXP count = 1; Print_tree(Exp_tree3); printf("\n"); #endif while (Modified) { Modified = FALSE; Exp_tree4 = Elim_subtraction(Exp_tree3,&Modified); #if DEBUG_EXP count++; printf("count = %d Modified = %d\n",count,Modified); Print_tree(Exp_tree4); printf("\n"); printf("freeing Exp_tree3\n"); #endif Free_tnode_tree(Exp_tree3); Exp_tree3 = Exp_tree4; Exp_tree4 = NULL; } /* Traverse the tree in inorder and write it as a string */ Create_exp_str(Exp_tree3,Exp_poly_str_ptr,&Maxsize_exp_poly_str); Free_tnode_tree(Unexp_tree1); Free_tnode_tree(Unexp_tree2); Free_tnode_tree(Exp_tree1); Free_tnode_tree(Exp_tree3); #if DEBUG_EXP printf("%s",exp_poly_str); printf("\n"); #endif /* Parse the string to create the polynomial. */ poly = Parse_exptext(*Exp_poly_str_ptr); *In_str_len = strlen(exp_poly_str); free(exp_poly_str); return(poly); }
int main(int argc, char *argv[]) { printf("Binarni vyhledavaci strom\n"); printf("=========================\n"); printf("[TEST01]\n"); printf("Test inicializace....\n"); test_BSTInit(&TempTree); printf("[TEST02]\n"); printf("Vypocitame vysku stromu\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTHeight(TempTree); printf("[TEST03]\n"); printf("Zkusime zrusit strom\n"); printf("~~~~~~~~~~~~~~~~~~~~\n"); test_BSTDispose(&TempTree); printf("[TEST04]\n"); printf("Pokusime se vyhledat polozku s klicem 'A' -- nenalezne se\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'A'; test_BSTSearch(TempTree,K,&Content_of_Search); printf("[TEST05]\n"); printf("Vlozime prvni prvek (H,1)\n"); K = 'H'; Content_of_Insert=1; test_BSTInsert(&TempTree,K,Content_of_Insert); printf("[TEST06]\n"); printf("Vypocitame vysku stromu\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTHeight(TempTree); printf("[TEST07]\n"); printf("Pokusime se vyhledat polozku s klicem H\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTSearch(TempTree,K,&Content_of_Search); printf("[TEST08]\n"); printf("Vlozime prvek (H,8) - pouze zmena hodnoty\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); Content_of_Insert=8; test_BSTInsert(&TempTree,K,Content_of_Insert); printf("[TEST09]\n"); printf("Vlozime dalsi prvky a vytvorime tak symetricky binarni strom \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); BSTInsert(&TempTree,'D',4); BSTInsert(&TempTree,'L',12); BSTInsert(&TempTree,'B',2); BSTInsert(&TempTree,'F',6); BSTInsert(&TempTree,'J',10); BSTInsert(&TempTree,'N',14); BSTInsert(&TempTree,'A',1); BSTInsert(&TempTree,'C',3); BSTInsert(&TempTree,'E',5); BSTInsert(&TempTree,'G',7); BSTInsert(&TempTree,'I',9); BSTInsert(&TempTree,'K',11); BSTInsert(&TempTree,'M',13); BSTInsert(&TempTree,'O',16); Print_tree(TempTree); printf("[TEST10]\n"); printf("Vypocitame vysku stromu\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTHeight(TempTree); printf("[TEST11]\n"); printf("Pokusime se vyhledat polozky s klici 'A', 'B'\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K='A'; test_BSTSearch(TempTree,K,&Content_of_Search); K='B'; test_BSTSearch(TempTree,K,&Content_of_Search); printf("[TEST12]\n"); printf("Pokusime se vyhledat polozky s klici 'X', 'Y'\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K='X'; test_BSTSearch(TempTree,K,&Content_of_Search); K='Y'; test_BSTSearch(TempTree,K,&Content_of_Search); printf("[TEST13]\n"); printf("Pridame vhodne jeste dalsi prvky \n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); BSTInsert(&TempTree,'S',10); BSTInsert(&TempTree,'R',10); BSTInsert(&TempTree,'Q',10); BSTInsert(&TempTree,'P',10); BSTInsert(&TempTree,'X',10); BSTInsert(&TempTree,'Y',10); BSTInsert(&TempTree,'Z',10); Print_tree(TempTree); printf("[TEST14]\n"); printf("Vypocitame vysku stromu\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTHeight(TempTree); printf("[TEST15]\n"); printf("Zrusime listovy uzel (A,1)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'A'; test_BSTDelete(&TempTree,K); printf("[TEST16]\n"); printf("Zrusime uzel, ktery ma jen levy podstrom (R,10)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'R'; test_BSTDelete(&TempTree, K); printf("[TEST17]\n"); printf("Vypocitame vysku stromu\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTHeight(TempTree); printf("[TEST18]\n"); printf("Zrusime uzel, ktery ma jen pravy podstrom (X,10)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"); K = 'X'; test_BSTDelete(&TempTree, K); printf("[TEST19]\n"); printf("Zrusime uzel, ktery ma oba podstromy (L,12)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'L'; test_BSTDelete(&TempTree, K); printf("[TEST20]\n"); printf("Pokusime se zrusit uzel, ktery neexistuje (U,?)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'U'; test_BSTDelete(&TempTree, K); printf("[TEST21]\n"); printf("Zrusime korenovy uzel (H,8)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'H'; test_BSTDelete(&TempTree, K); printf("[TEST22]\n"); printf("Zrusime dalsi uzel (K,11)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'K'; test_BSTDelete(&TempTree, K); printf("[TEST23]\n"); printf("Zrusime dalsi uzel (D,4)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'D'; test_BSTDelete(&TempTree, K); printf("[TEST24]\n"); printf("Zrusime dalsi uzel (S,10)\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~\n"); K = 'S'; test_BSTDelete(&TempTree, K); printf("[TEST25]\n"); printf("Nakonec zrusime cely strom\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); test_BSTDispose(&TempTree); printf("------------------------------ konec -------------------------------------\n"); return(0); }