#include"test.hpp" #include #include #include #include "../zlasdtest/test.hpp" using namespace lasd; using namespace std; void menu(){ unsigned short int choice; DataType chosenDataType; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; }while(choice!=1 && choice!=2); switch(choice){ case 1: lasdtest(); break; case 2: chosenDataType = ChooseDataType(); UseChosenType(chosenDataType); break; } } DataType ChooseDataType(){ unsigned short int choice; do{ std::cout<<"\nChoose a data type:"< "; std::cin>>std::ws; std::cin>>choice; }while(!(choice>0 && choice<4)); if(choice==1) return DataType::integer; else if(choice==2) return DataType::ffloat; else if(choice==3) return DataType::sstring; } void UseChosenType(DataType chosenDataType){ if(chosenDataType == DataType::integer){ BST bst; bst = GenerateIntegerBST(bst); IntegerFunctions(bst); }else if(chosenDataType == DataType::ffloat){ BST bst; bst = GenerateFloatBST(bst); FloatFunctions(bst); }else if(chosenDataType == DataType::sstring){ BST bst; bst = GenerateStringsBST(bst); StringFunctions(bst); } } /* ----- integer functions ----- */ template void IntegerFunctions(BST& bst){ unsigned short int choice; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; std::cout< void ProductsElementsLessThan(BST& tree){ int n, acc=1; void (*func)(const int&, const void*, void*) = AccumulateProduct; cout<<"Multipy all elements of the tree whose value is less than "; cin>>ws>>n; tree.FoldBreadth(func, (void*)&n, (void*)&acc); cout<<"\nThe result is "< void FloatFunctions(BST& bst){ unsigned short int choice; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; std::cout< void SumElementsGreaterThan(BST& tree){ float n, acc = 0; void (*func)(const float&, const void*, void*) = AccumulateSum; cout<<"Sum all elements of the tree whose value is greater than "; cin>>ws>>n; tree.FoldBreadth(func, (void*)&n, (void*)&acc); cout<<"\nThe result is "< (*(float*)par)){ *(float*)acc += data; } } /* ----- string functions ----- */ template void StringFunctions(BST& bst){ unsigned short int choice; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; std::cout< void ConcatLessThan(BST& tree){ int n; string concatenated = ""; void (*func)(const string&, const void*, void*) = ConcatAString; cout<<"Concatenate all elements of the tree whose length is less or equal than "; cin>>ws>>n; tree.FoldBreadth(func, (void*)&n, (void*)&concatenated); cout<<"\nThe result is "<< concatenated << endl << endl; } void ConcatAString(const string& data, const void* par, void* acc){ if( ((int)data.length()) <= ((*(int*)par)) ){ *(string*)acc = *(string*)acc + "-" + data; } } /* ----- shared functions ----- */ template void PrintTree(BST& tree){ void (*PrinterFunction) (Data&, void*) = PrintSingleElement; cout<<"Pre order:\n"; tree.MapPreOrder(PrinterFunction, nullptr); cout< void PrintSingleElement(Data& data, void* _){ std::cout << data << " "; } template void CheckExistence(BST& tree){ Data elementToLookFor; cout<<"\n\nCheck existence in the tree of: "; cin>>ws; cin>>elementToLookFor; cout<<"The element " << ( (!tree.Exists(elementToLookFor))? "does not " : "") << "exists\n\n"; } template void InsertElement(BST& bst){ Data elementToInsert; cout<<"\n\nInsert in the BST the following element: "; cin>>ws; cin>>elementToInsert; bst.Insert(elementToInsert); } template void RemoveElement(BST& bst){ Data elementToRemove; cout<<"\n\nRemove from the BST the following element: "; cin>>ws; cin>>elementToRemove; bst.Remove(elementToRemove); } template void PrintMinimum(BST& bst){ if(bst.Size()>0) cout<<"\n\nThe minimum element in the BST is "< void PrintMinimumNDelete(BST& bst){ if(bst.Size()>0) cout<<"The minimum element in the BST ( "< void RemoveMin(BST& bst){ if(bst.Size()>0){ bst.RemoveMin(); cout<<"\n\nThe minimum element has been deleted"; } else cout<<"\n\nTree is empty."; } template void PrintMaximum(BST& bst){ if(bst.Size()>0) cout<<"\n\nThe maximum element in the BST is "< void PrintMaximumNDelete(BST& bst){ if(bst.Size()>0) cout<<"The maximum element in the BST ( "< void RemoveMax(BST& bst){ if(bst.Size()>0){ bst.RemoveMax(); cout<<"\n\nThe maximum element has been deleted"; } else cout<<"\n\nTree is empty."; } template void PrintPredecessor(BST& bst){ Data lookForPredecessor; cout<<"Print the predecessor of "; cin>>ws; cin>>lookForPredecessor; try{ cout<<"The predecessor of "< void PredecessorNRemove(BST& bst){ Data lookForPredecessor; cout<<"Print and delete the predecessor of "; cin>>ws; cin>>lookForPredecessor; try{ cout<<"The predecessor of "< void RemovePredecessor(BST& bst){ Data lookForPredecessor; cout<<"Delete the predecessor of "; cin>>ws; cin>>lookForPredecessor; try{ bst.RemovePredecessor(lookForPredecessor); cout<<"The predecessor of "< void PrintSuccessor(BST& bst){ Data lookForSuccessor; cout<<"Print the successor of "; cin>>ws; cin>>lookForSuccessor; try{ cout<<"The successor of "<< lookForSuccessor<<" is "< void SuccessorNRemove(BST& bst){ Data lookForSuccessor; cout<<"Print and delete the successor of "; cin>>ws; cin>>lookForSuccessor; try{ cout<<"The successor of "< void RemoveSuccessor(BST& bst){ Data lookForSuccessor; cout<<"Delete the successor of "; cin>>ws; cin>>lookForSuccessor; try{ bst.RemoveSuccessor(lookForSuccessor); cout<<"The successor of "< bool NodeOperations(T& currentNode){ uint choice; bool wantToExit = false; cout< "; cin>>ws; cin>>choice; switch(choice){ case 1: if(currentNode.HasLeftChild()){ wantToExit = NodeOperations(currentNode.LeftChild()); } else{ cout<<"\nThe node does not have a left child, operation aborted.\n"; } break; case 2: if(currentNode.HasRightChild()){ wantToExit = NodeOperations(currentNode.RightChild()); } else { cout<<"\nThe node does not have a right child, operation aborted.\n"; } break; case 3: if(currentNode.IsLeaf()) cout<<"\n Yes, the current node is a leaf\n"; else cout<<"\n No, the current node is not a leaf\n"; break; case 4: return false; } }while(choice!=5 && !wantToExit); return true; } /* ----- generator functions ----- */ BST GenerateIntegerBST(BST& bst){ ulong dim = getDimension(); Vector tmp(dim); default_random_engine gen(random_device{}()); uniform_int_distribution dist(0,1000); cout<<"\n\nElements in the binary search tree (in order of insertion):\n"; for(ulong i=0 ; i tree(tmp); return tree; } BST GenerateFloatBST(BST& bst){ ulong dim = getDimension(); Vector tmp(dim); default_random_engine gen(random_device{}()); uniform_real_distribution distr(0,5); cout<<"\n\nElements in the binary search tree (in order of insertion):\n"; for(unsigned long i = 0; i < dim; ++i){ tmp[i] = (round(distr(gen)*10000))/100; cout< tree(tmp); return tree; } BST GenerateStringsBST(BST& bst){ ulong dim = getDimension(); Vector tmp(dim); default_random_engine gen(random_device{}()); uniform_int_distribution dist(1,5); cout<<"\n\nElements in the binary search tree (in order of insertion):\n"; for(ulong i = 0; i < dim; ++i){ tmp[i] = generateRandomString(dist(gen)); cout< tree(tmp); return tree; } string generateRandomString(ulong dim){ default_random_engine gen(random_device{}()); uniform_int_distribution character('a','z'); char newString[dim+1]; for(int i=0;i>dimension; return dimension; }