Library 3

added node-level operations in mytests and fixed error
This commit is contained in:
Alessandro Ferro 2021-05-09 22:23:09 +02:00
parent 33106bcee2
commit a66d40aab3
4 changed files with 111 additions and 30 deletions

View File

@ -245,7 +245,10 @@ void BinaryTree<Data>::FoldBreadth(const typename FoldableContainer<Data>::FoldF
template <typename Data> template <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){ BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = &tree.Root(); curr = &tree.Root();
else
curr = nullptr;
} }
template <typename Data> template <typename Data>
@ -345,7 +348,10 @@ struct BinaryTree<Data>::Node* BTPostOrderIterator<Data>::DeepestLeftLeaf(struct
template <typename Data> template <typename Data>
BTPostOrderIterator<Data>::BTPostOrderIterator(const BinaryTree<Data>& tree){ BTPostOrderIterator<Data>::BTPostOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = DeepestLeftLeaf(&tree.Root()); curr = DeepestLeftLeaf(&tree.Root());
else
curr = nullptr;
} }
template <typename Data> template <typename Data>
@ -441,7 +447,10 @@ struct BinaryTree<Data>::Node* BTInOrderIterator<Data>::MostLeftNode(struct Bina
template <typename Data> template <typename Data>
BTInOrderIterator<Data>::BTInOrderIterator(const BinaryTree<Data>& tree){ BTInOrderIterator<Data>::BTInOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = MostLeftNode(tree.Root()); curr = MostLeftNode(tree.Root());
else
curr = nullptr;
} }
template <typename Data> template <typename Data>
@ -518,7 +527,10 @@ void BTInOrderIterator<Data>::operator++(){
template <typename Data> template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){ BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = &(tree.Root()); curr = &(tree.Root());
else
curr = nullptr;
} }
template <typename Data> template <typename Data>

View File

@ -1,7 +1,7 @@
#! /bin/bash #! /bin/bash
g++ -g -o main \ g++ -O3 -o main \
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \ zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \ zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \ zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \

View File

@ -116,8 +116,9 @@ void IntegerFunctions(T& bt){
std::cout<<" 3. Product of integers less than 'n' "<<std::endl; std::cout<<" 3. Product of integers less than 'n' "<<std::endl;
std::cout<<" 4. Multiply each element by 3"<<std::endl; std::cout<<" 4. Multiply each element by 3"<<std::endl;
std::cout<<" 5. Iterate over the tree"<<std::endl; std::cout<<" 5. Iterate over the tree"<<std::endl;
std::cout<<" 6. Go back"<<std::endl; std::cout<<" 6. Node-level operations"<<std::endl;
std::cout<<" 7. Quit"<<std::endl; std::cout<<" 7. Go back"<<std::endl;
std::cout<<" 8. Quit"<<std::endl;
cout<<endl<<" -> "; cout<<endl<<" -> ";
std::cin>>std::ws; std::cin>>std::ws;
std::cin>>choice; std::cin>>choice;
@ -139,10 +140,16 @@ void IntegerFunctions(T& bt){
Iterators(bt); Iterators(bt);
break; break;
case 6: case 6:
if(bt.Size() > 0)
NodeOperations(bt.Root());
else
cout<<"\nThe tree does not have any nodes...";
break;
case 7:
menu(); menu();
break; break;
} }
}while(choice!=6 && choice!=7); }while(choice!=7 && choice!=8);
} }
template <typename T> template <typename T>
@ -156,8 +163,9 @@ void FloatFunctions(T& bt){
std::cout<<" 3. Sum of floats greater than 'n' "<<std::endl; std::cout<<" 3. Sum of floats greater than 'n' "<<std::endl;
std::cout<<" 4. Cube elements"<<std::endl; std::cout<<" 4. Cube elements"<<std::endl;
std::cout<<" 5. Iterate over the tree"<<std::endl; std::cout<<" 5. Iterate over the tree"<<std::endl;
std::cout<<" 6. Go back"<<std::endl; std::cout<<" 6. Node-level operations"<<std::endl;
std::cout<<" 7. Quit"<<std::endl; std::cout<<" 7. Go back"<<std::endl;
std::cout<<" 8. Quit"<<std::endl;
cout<<endl<<" -> "; cout<<endl<<" -> ";
std::cin>>std::ws; std::cin>>std::ws;
std::cin>>choice; std::cin>>choice;
@ -179,10 +187,16 @@ void FloatFunctions(T& bt){
Iterators(bt); Iterators(bt);
break; break;
case 6: case 6:
if(bt.Size() > 0)
NodeOperations(bt.Root());
else
cout<<"\nThe tree does not have any nodes...";
break;
case 7:
menu(); menu();
break; break;
} }
}while(choice!=6 && choice!=7); }while(choice!=7 && choice!=8);
} }
template <typename T> template <typename T>
@ -196,8 +210,9 @@ void StringFunctions(T& bt){
std::cout<<" 3. Concatenate strings whose dimension is less or equal than 'n' "<<std::endl; std::cout<<" 3. Concatenate strings whose dimension is less or equal than 'n' "<<std::endl;
std::cout<<" 4. Head concatenation of a string"<<std::endl; std::cout<<" 4. Head concatenation of a string"<<std::endl;
std::cout<<" 5. Iterate over the tree"<<std::endl; std::cout<<" 5. Iterate over the tree"<<std::endl;
std::cout<<" 6. Go back"<<std::endl; std::cout<<" 6. Node-level operations"<<std::endl;
std::cout<<" 7. Quit"<<std::endl; std::cout<<" 7. Go back"<<std::endl;
std::cout<<" 8. Quit"<<std::endl;
cout<<endl<<" -> "; cout<<endl<<" -> ";
std::cin>>std::ws; std::cin>>std::ws;
std::cin>>choice; std::cin>>choice;
@ -219,10 +234,16 @@ void StringFunctions(T& bt){
Iterators(bt); Iterators(bt);
break; break;
case 6: case 6:
if(bt.Size() > 0)
NodeOperations(bt.Root());
else
cout<<"\nThe tree does not have any nodes...";
break;
case 7:
menu(); menu();
break; break;
} }
}while(choice!=6 && choice!=7); }while(choice!=7 && choice!=8);
} }
/* ----- integer functions ----- */ /* ----- integer functions ----- */
@ -388,29 +409,29 @@ void Iterators(Tree<DTType>& tree){
break; break;
} }
} }
template <typename Iter>
template <template <typename...> class Iter, typename ItrType> void NavigateWithIterator(Iter& itr){
void NavigateWithIterator(Iter<ItrType>& itr){
uint choice=0; uint choice=0;
ItrType element;
while(!itr.Terminated() && choice!=3){ while(!itr.Terminated() && choice!=3){
cout<<endl<<" *** Current position: "<<*itr<<" *** "<<endl<<endl; cout<<endl<<" *** Current position: "<<*itr<<" *** "<<endl<<endl;
cout<<" 1. Go ahead (++)\n"; cout<<" 1. Go ahead (++)\n";
cout<<" 2. Edit the value\n"; cout<<" 2. Edit value\n";
cout<<" 3. Go to main menu\n"; cout<<" 3. Go to main menu\n";
cout<<endl<<" -> "; cout<<endl<<" -> ";
cin>>ws; cin>>ws;
cin>>choice; cin>>choice;
if(choice == 1){ switch(choice){
case 1:
++itr; ++itr;
} break;
else if(choice == 2){ case 2:
cout<<"\n Overwrite with: "; cout<<"\n Overwrite with: ";
cin>>ws; cin>>ws;
cin>>*itr; cin>>*itr;
break;
} }
} }
if(itr.Terminated()) cout<<"\n *** Iterator is terminated ***\n"; if(itr.Terminated()) cout<<"\n *** Iterator is terminated ***\n";
@ -425,6 +446,51 @@ void CheckExistence(Tree<DTType>& tree){
cout<<"The element " << ( (!tree.Exists(elementToLookFor))? "does not " : "") << "exists\n\n"; cout<<"The element " << ( (!tree.Exists(elementToLookFor))? "does not " : "") << "exists\n\n";
} }
template <typename T>
void NodeOperations(T& currentNode){
uint choice;
cout<<endl<<endl;
cout<<" *** Element in the current node: "<<currentNode.Element()<<" ***"<<endl;
cout<<" 1. Go left"<<endl;
cout<<" 2. Go Right"<<endl;
cout<<" 3. Is this a leaf?"<<endl;
cout<<" 4. Edit value"<<endl;
cout<<" 5. Go back"<<endl;
cout<<endl<<" -> ";
cin>>ws;
cin>>choice;
switch(choice){
case 1:
if(currentNode.HasLeftChild()) NodeOperations(currentNode.LeftChild());
else{
cout<<"\nThe node does not have a left child, operation aborted.";
NodeOperations(currentNode);
}
break;
case 2:
if(currentNode.HasRightChild()) NodeOperations(currentNode.RightChild());
else {
cout<<"\nThe node does not have a right child, operation aborted.";
NodeOperations(currentNode);
}
break;
case 3:
if(currentNode.IsLeaf()) cout<<"\n Yes, the current node is a leaf";
else cout<<"\n No, the current node is not a leaf";
NodeOperations(currentNode);
break;
case 4:
cout<<"Overwrite with: ";
cin>>ws;
cin>>currentNode.Element();
NodeOperations(currentNode);
break;
}
}
/* ----- generator functions ----- */ /* ----- generator functions ----- */

View File

@ -68,8 +68,11 @@ void CheckExistence(Tree<DTType>&);
template <template <typename...> class Tree, typename DTType> template <template <typename...> class Tree, typename DTType>
void Iterators(Tree<DTType>&); void Iterators(Tree<DTType>&);
template <template <typename...> class Iter, typename ItrType> template <typename Iter>
void NavigateWithIterator(Iter<ItrType>&); void NavigateWithIterator(Iter&);
template <typename T>
void NodeOperations(T&);
/* ----- Generator functions ----- */ /* ----- Generator functions ----- */
DataType ChooseDataType(); //choose data type DataType ChooseDataType(); //choose data type