From 8e56f338081b33a07d0ca0c37bc389396b0b894d Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 5 May 2021 22:37:45 +0200 Subject: [PATCH] Library 3 cleaner code --- librerie/exercise3/binarytree/binarytree.cpp | 132 +++++++++------- librerie/exercise3/binarytree/binarytree.hpp | 147 ++---------------- .../binarytree/lnk/binarytreelnk.cpp | 9 +- .../binarytree/lnk/binarytreelnk.hpp | 45 +----- .../binarytree/vec/binarytreevec.cpp | 8 +- .../binarytree/vec/binarytreevec.hpp | 47 +----- librerie/exercise3/main.cpp | 2 +- librerie/exercise3/zmytest/test.hpp | 19 ++- 8 files changed, 138 insertions(+), 271 deletions(-) diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index acecce8..45ef9b8 100755 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -2,21 +2,27 @@ #include "../queue/queue.hpp" #include "../queue/vec/queuevec.hpp" #include "../queue/lst/queuelst.hpp" - #include "../stack/stack.hpp" #include "../stack/lst/stacklst.hpp" #include "../stack/vec/stackvec.hpp" #include + namespace lasd { -/* ************************************************************************** */ +/* ----- begin of class BinaryTree ----- */ + +/* ----- begin of struct Node ----- */ -// checks if two trees which have the same root node are identical template bool BinaryTree::Node::operator==(const Node& toEvaluate) const noexcept{ return EqualNodes(*this, toEvaluate); } +template +bool BinaryTree::Node::operator!=(const Node& toEvaluate) const noexcept{ + return !(*this == toEvaluate); +} + /* given two nodes, checks if the subtree is the same */ template bool BinaryTree::Node::EqualNodes(const Node& n1, const Node& n2) const{ @@ -42,11 +48,6 @@ bool BinaryTree::Node::EqualNodes(const Node& n1, const Node& n2) const{ } } -template -bool BinaryTree::Node::operator!=(const Node& toEvaluate) const noexcept{ - return !(*this == toEvaluate); -} - template Data& BinaryTree::Node::Element(){ return this->data; @@ -57,6 +58,8 @@ const Data& BinaryTree::Node::Element() const{ return this->data; } +/* ----- end of struct Node ----- */ + template bool BinaryTree::operator==(const BinaryTree& toCompare) const noexcept{ if(size!=toCompare.size) return false; @@ -65,7 +68,7 @@ bool BinaryTree::operator==(const BinaryTree& toCompare) const noexcept{ template bool BinaryTree::operator!=(const BinaryTree& toCompare) const noexcept{ - return(Root() != toCompare.Root()); + return !(*this == toCompare); } /* ----- Map and fold functions ----- */ @@ -129,15 +132,47 @@ template void BinaryTree::MapPostOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ if(node != nullptr){ if(node->HasLeftChild()){ - MapPreOrder(function, par, &(node->LeftChild())); + MapPostOrder(function, par, &(node->LeftChild())); } if(node->HasRightChild()){ - MapPreOrder(function, par, &(node->RightChild())); + MapPostOrder(function, par, &(node->RightChild())); } function(node->Element(), par); } } +template +void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + if(node->HasLeftChild()){ + MapInOrder(function, par, &(node->LeftChild())); + } + function(node->Element(), par); + if(node->HasRightChild()){ + MapInOrder(function, par, &(node->RightChild())); + } + } +} + +template +void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + QueueLst toVisit; + if(node != nullptr){ + toVisit.Enqueue(node); + while(!toVisit.Empty()){ + function(toVisit.Head()->Element(), par); + + if(toVisit.Head()->HasLeftChild()){ + toVisit.Enqueue(&(toVisit.Head()->LeftChild())); + } + if(toVisit.Head()->HasRightChild()){ + toVisit.Enqueue(&(toVisit.Head()->RightChild())); + } + toVisit.Dequeue(); + } + } +} + template void BinaryTree::FoldPreOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ if(node != nullptr){ @@ -164,19 +199,6 @@ void BinaryTree::FoldPostOrder(const typename FoldableContainer::Fol } } -template -void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ - if(node != nullptr){ - if(node->HasLeftChild()){ - MapInOrder(function, par, &(node->LeftChild())); - } - function(node->Element(), par); - if(node->HasRightChild()){ - MapInOrder(function, par, &(node->RightChild())); - } - } -} - template void BinaryTree::FoldInOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ if(node != nullptr){ @@ -185,26 +207,7 @@ void BinaryTree::FoldInOrder(const typename FoldableContainer::FoldF } function(node->Element(), par, acc); if(node->HasRightChild()){ - FoldPreOrder(function, par, acc, &(node->RightChild())); - } - } -} - -template -void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par, Node* node){ - QueueLst toVisit; - if(node != nullptr){ - toVisit.Enqueue(node); - while(!toVisit.Empty()){ - function(toVisit.Head()->Element(), par); - - if(toVisit.Head()->HasLeftChild()){ - toVisit.Enqueue(&(toVisit.Head()->LeftChild())); - } - if(toVisit.Head()->HasRightChild()){ - toVisit.Enqueue(&(toVisit.Head()->RightChild())); - } - toVisit.Dequeue(); + FoldInOrder(function, par, acc, &(node->RightChild())); } } } @@ -228,6 +231,10 @@ void BinaryTree::FoldBreadth(const typename FoldableContainer::FoldF } } +/* ----- end of class BinaryTree ----- */ + +/* ----- begin of class BTPreOrderIterator ----- */ + template BTPreOrderIterator::BTPreOrderIterator(const BinaryTree& tree){ curr = &tree.Root(); @@ -235,7 +242,6 @@ BTPreOrderIterator::BTPreOrderIterator(const BinaryTree& tree){ template BTPreOrderIterator::BTPreOrderIterator(const BTPreOrderIterator& itr){ - //curr = &(*itr); curr = itr.curr; stack = itr.stack; } @@ -254,7 +260,6 @@ BTPreOrderIterator::~BTPreOrderIterator(){ template BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator& itr){ - //curr = &(*itr); curr = itr.curr; stack = itr.stack; return *this; @@ -279,6 +284,7 @@ bool BTPreOrderIterator::operator!=(const BTPreOrderIterator& itr) const n template Data& BTPreOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); return curr->Element(); } @@ -311,6 +317,10 @@ void BTPreOrderIterator::operator++(){ } } +/* ----- end of class BTPreOrderIterator ----- */ + +/* ----- begin of class BTPostOrderIterator ----- */ + template struct BinaryTree::Node* BTPostOrderIterator::DeepestLeftLeaf(struct BinaryTree::Node* node){ if(node->HasLeftChild()){ @@ -374,6 +384,7 @@ bool BTPostOrderIterator::operator!=(const BTPostOrderIterator& itr) const template Data& BTPostOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); return curr->Element(); } @@ -384,8 +395,16 @@ bool BTPostOrderIterator::Terminated() const noexcept{ template void BTPostOrderIterator::operator++(){ + /* + * If we're coming from the left then we have to analyze the tree on the right + * (if existent). Otherwise we just top 'n' pop. + */ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); - try{ + + if(stack.Empty()){ + curr = nullptr; + }else{ if( curr == &((stack.Top())->LeftChild()) ){ if( (stack.Top())->HasRightChild() ){ curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); @@ -395,11 +414,13 @@ void BTPostOrderIterator::operator++(){ }else{ curr = stack.TopNPop(); } - }catch(std::length_error exc){ - curr = nullptr; } } +/* ----- end of class BTPostOrderIterator ----- */ + +/* ----- begin of class BTInOrderIterator ----- */ + template struct BinaryTree::Node* BTInOrderIterator::MostLeftNode(struct BinaryTree::Node& root){ if(root.HasLeftChild()){ @@ -459,6 +480,7 @@ bool BTInOrderIterator::operator!=(const BTInOrderIterator& itr) const noe template Data& BTInOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); return curr->Element(); } @@ -474,14 +496,18 @@ void BTInOrderIterator::operator++(){ if(curr->HasRightChild()){ curr = MostLeftNode(curr->RightChild()); }else{ - try{ - curr = stack.TopNPop(); - }catch(std::length_error exc){ + if(stack.Empty()){ curr = nullptr; + }else{ + curr = stack.TopNPop(); } } } +/* ----- end of class BTInOrderIterator ----- */ + +/* ----- begin of class BTBreadthIteratorOrderIterator ----- */ + template BTBreadthIterator::BTBreadthIterator(const BinaryTree& tree){ curr = &(tree.Root()); @@ -531,6 +557,7 @@ bool BTBreadthIterator::operator!=(const BTBreadthIterator& itr) const noe template Data& BTBreadthIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); return curr->Element(); } @@ -542,6 +569,7 @@ bool BTBreadthIterator::Terminated() const noexcept{ template void BTBreadthIterator::operator++(){ if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + if(curr->HasLeftChild()){ queue.Enqueue(&(curr->LeftChild())); } diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp index e71be10..df22063 100755 --- a/librerie/exercise3/binarytree/binarytree.hpp +++ b/librerie/exercise3/binarytree/binarytree.hpp @@ -2,8 +2,6 @@ #ifndef BINARYTREE_HPP #define BINARYTREE_HPP -/* ************************************************************************** */ - #include "../container/container.hpp" #include "../iterator/iterator.hpp" @@ -16,24 +14,14 @@ #include "../stack/lst/stacklst.hpp" #include "../stack/vec/stackvec.hpp" -/* ************************************************************************** */ - namespace lasd { -/* ************************************************************************** */ - template class BinaryTree : virtual public InOrderMappableContainer, virtual public BreadthMappableContainer, virtual public InOrderFoldableContainer, virtual public BreadthFoldableContainer{ // Must extend InOrder/BreadthMappableContainer and InOrder/BreadthFoldableContainer -private: - -protected: - - using BreadthMappableContainer::size; - public: struct Node { @@ -76,21 +64,14 @@ public: }; - // Destructor virtual ~BinaryTree() = default; - // Copy assignment BinaryTree& operator=(const BinaryTree&) = delete; // Copy assignment of abstract types should not be possible. - - // Move assignment BinaryTree& operator=(BinaryTree&&) noexcept = delete; // Move assignment of abstract types should not be possible. - // Comparison operators bool operator==(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible. bool operator!=(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible. - /* ----- Specific member functions ----- */ - virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty) /* ----- Map and fold functions ----- */ @@ -105,9 +86,10 @@ public: virtual void FoldInOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member virtual void FoldBreadth(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member - protected: + using BreadthMappableContainer::size; + /* ----- Auxiliary map and fold functions ----- */ void MapPreOrder(const typename MappableContainer::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree @@ -121,59 +103,34 @@ protected: void FoldBreadth(const typename FoldableContainer::FoldFunctor, const void*, void*, Node*) const; // Accessory function executing from one node of the tree }; -/* ************************************************************************** */ template class BTPreOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator -private: - protected: - struct BinaryTree::Node* curr; + + struct BinaryTree::Node* curr = nullptr; StackLst::Node*> stack; public: - // Specific constructors - BTPreOrderIterator(const BinaryTree&); // An iterator over a given binary tree - - /* ************************************************************************ */ - - // Copy constructor - BTPreOrderIterator(const BTPreOrderIterator&); - - // Move constructor - BTPreOrderIterator(BTPreOrderIterator&&) noexcept; - - /* ************************************************************************ */ - - // Destructor virtual ~BTPreOrderIterator(); - /* ************************************************************************ */ + BTPreOrderIterator(const BinaryTree&); // An iterator over a given binary tree + BTPreOrderIterator(const BTPreOrderIterator&); + BTPreOrderIterator(BTPreOrderIterator&&) noexcept; - // Copy assignment BTPreOrderIterator& operator=(const BTPreOrderIterator&); - - // Move assignment BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BTPreOrderIterator&) const noexcept; bool operator!=(const BTPreOrderIterator&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from Iterator) Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() const noexcept; // (should not throw exceptions) - /* ************************************************************************ */ - // Specific member functions (inherited from ForwardIterator) void operator++(); // (throw std::out_of_range when terminated) @@ -185,54 +142,33 @@ public: template class BTPostOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator -private: - protected: - struct BinaryTree::Node* curr; + + struct BinaryTree::Node* curr = nullptr; StackLst::Node*> stack; + struct BinaryTree::Node* DeepestLeftLeaf(struct BinaryTree::Node*); + public: - // Specific constructors BTPostOrderIterator(const BinaryTree&); // An iterator over a given binary tree - - /* ************************************************************************ */ - - // Copy constructor BTPostOrderIterator(const BTPostOrderIterator&); - - // Move constructor BTPostOrderIterator(BTPostOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Destructor virtual ~BTPostOrderIterator(); - /* ************************************************************************ */ - // Copy assignment BTPostOrderIterator& operator=(const BTPostOrderIterator&); - - // Move assignment BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BTPostOrderIterator&) const noexcept; bool operator!=(const BTPostOrderIterator&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from Iterator) Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() const noexcept; // (should not throw exceptions) - /* ************************************************************************ */ - // Specific member functions (inherited from ForwardIterator) void operator++(); // (throw std::out_of_range when terminated) @@ -244,122 +180,71 @@ public: template class BTInOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator -private: - protected: - struct BinaryTree::Node* curr; + + struct BinaryTree::Node* curr = nullptr; StackLst::Node*> stack; + struct BinaryTree::Node* MostLeftNode(struct BinaryTree::Node&); + public: - // Specific constructors BTInOrderIterator(const BinaryTree&); // An iterator over a given binary tree - - /* ************************************************************************ */ - - // Copy constructor BTInOrderIterator(const BTInOrderIterator&); - - // Move constructor BTInOrderIterator(BTInOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Destructor virtual ~BTInOrderIterator(); - /* ************************************************************************ */ - - // Copy assignment BTInOrderIterator& operator=(const BTInOrderIterator&); - - // Move assignment BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BTInOrderIterator&) const noexcept; bool operator!=(const BTInOrderIterator&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from Iterator) Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() const noexcept; // (should not throw exceptions) - /* ************************************************************************ */ - // Specific member functions (inherited from ForwardIterator) void operator++(); // (throw std::out_of_range when terminated) }; -/* ************************************************************************** */ - template class BTBreadthIterator : virtual public ForwardIterator{ // Must extend ForwardIterator -private: - protected: - struct BinaryTree::Node* curr; + struct BinaryTree::Node* curr = nullptr; QueueVec::Node*> queue; public: - // Specific constructors BTBreadthIterator(const BinaryTree&); // An iterator over a given binary tree - - /* ************************************************************************ */ - - // Copy constructor BTBreadthIterator(const BTBreadthIterator&); - - // Move constructor BTBreadthIterator(BTBreadthIterator&&) noexcept; - /* ************************************************************************ */ - - // Destructor virtual ~BTBreadthIterator(); - /* ************************************************************************ */ - - // Copy assignment BTBreadthIterator& operator=(const BTBreadthIterator&); - - // Move assignment BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BTBreadthIterator&) const noexcept; bool operator!=(const BTBreadthIterator&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from Iterator) Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() const noexcept; // (should not throw exceptions) - /* ************************************************************************ */ - // Specific member functions (inherited from ForwardIterator) void operator++(); // (throw std::out_of_range when terminated) }; -/* ************************************************************************** */ - } #include "binarytree.cpp" diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp index 5b269f9..c427a70 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp @@ -1,7 +1,8 @@ namespace lasd { -/* ************************************************************************** */ +/* ----- begin of struct NodeLnk ----- */ + template struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::operator=(const BinaryTreeLnk::NodeLnk& node){ data = node.data; @@ -43,6 +44,10 @@ struct BinaryTree::Node& BinaryTreeLnk::NodeLnk::RightChild() const{ return *right; } +/* ----- end of struct NodeLnk ----- */ + +/* ----- begin of class BinaryTreeLnk ----- */ + // creates a tree from a linear container in breadth template BinaryTreeLnk::BinaryTreeLnk(const LinearContainer& lc){ @@ -55,7 +60,6 @@ struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CreateTreeFromLinearC if(position >= lc.Size()) return nullptr; struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(lc[position]); - // if(position == 0) root = tmp; tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1); tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2); @@ -130,6 +134,7 @@ BinaryTreeLnk& BinaryTreeLnk::operator=(const BinaryTreeLnk& t template BinaryTreeLnk& BinaryTreeLnk::operator=(BinaryTreeLnk&& tree) noexcept{ + Clear(); std::swap(size, tree.size); std::swap(root, tree.root); return *this; diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp index f37aa8a..76ed992 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp @@ -1,30 +1,17 @@ - #ifndef BINARYTREELNK_HPP #define BINARYTREELNK_HPP -/* ************************************************************************** */ - #include "../binarytree.hpp" -/* ************************************************************************** */ - namespace lasd { -/* ************************************************************************** */ - template class BinaryTreeLnk : virtual public BinaryTree{ // Must extend BinaryTree -private: - protected: - using BinaryTree::size; - struct NodeLnk : virtual protected BinaryTree::Node { // Must extend Node - private: - protected: using BinaryTree::Node::data; @@ -46,64 +33,42 @@ protected: }; protected: + using BinaryTree::size; + struct BinaryTreeLnk::NodeLnk* root = nullptr; struct BinaryTreeLnk::NodeLnk* CreateNode(const Data& data); + public: // Default constructor BinaryTreeLnk() = default; - - /* ************************************************************************ */ - - // Specific constructors BinaryTreeLnk(const LinearContainer&); // A binary tree obtained from a LinearContainer - - /* ************************************************************************ */ - - // Copy constructor BinaryTreeLnk(const BinaryTreeLnk&); - - // Move constructor BinaryTreeLnk(BinaryTreeLnk&&) noexcept; - /* ************************************************************************ */ - - // Destructor virtual ~BinaryTreeLnk(); - /* ************************************************************************ */ - - // Copy assignment BinaryTreeLnk& operator=(const BinaryTreeLnk&); - - // Move assignment BinaryTreeLnk& operator=(BinaryTreeLnk&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BinaryTreeLnk&) const noexcept; bool operator!=(const BinaryTreeLnk&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from BinaryTree) struct BinaryTree::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) - /* ************************************************************************ */ - // Specific member functions (inherited from Container) void Clear() override; // Override Container member + // Specific functions + struct BinaryTreeLnk::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer&,ulong); struct BinaryTreeLnk::NodeLnk* CopyTree(struct BinaryTreeLnk::Node*); void DeleteTree(BinaryTreeLnk::NodeLnk* node); }; -/* ************************************************************************** */ - } #include "binarytreelnk.cpp" diff --git a/librerie/exercise3/binarytree/vec/binarytreevec.cpp b/librerie/exercise3/binarytree/vec/binarytreevec.cpp index dfb5a35..3d456a2 100755 --- a/librerie/exercise3/binarytree/vec/binarytreevec.cpp +++ b/librerie/exercise3/binarytree/vec/binarytreevec.cpp @@ -1,7 +1,8 @@ namespace lasd { -/* ************************************************************************** */ +/* ----- begin of struct NodeVec ----- */ + template BinaryTreeVec::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec* ref){ data = dat; @@ -64,6 +65,10 @@ struct BinaryTree::Node& BinaryTreeVec::NodeVec::RightChild() const{ throw std::out_of_range("Right child does not exist!"); } +/* ----- end of struct NodeVec ----- */ + +/* ----- begin of class BinaryTreeVec ----- */ + template BinaryTreeVec::BinaryTreeVec(const LinearContainer& lc){ tree.Resize(lc.Size()); @@ -137,6 +142,7 @@ bool BinaryTreeVec::operator!=(const BinaryTreeVec& bt) const noexcept{ template struct BinaryTree::Node& BinaryTreeVec::Root() const{ + if(size==0) throw std::length_error("Empty tree!"); return *(tree.Front()); } diff --git a/librerie/exercise3/binarytree/vec/binarytreevec.hpp b/librerie/exercise3/binarytree/vec/binarytreevec.hpp index ec4b0fd..858e20c 100755 --- a/librerie/exercise3/binarytree/vec/binarytreevec.hpp +++ b/librerie/exercise3/binarytree/vec/binarytreevec.hpp @@ -2,31 +2,18 @@ #ifndef BINARYTREEVEC_HPP #define BINARYTREEVEC_HPP -/* ************************************************************************** */ - #include "../binarytree.hpp" #include "../../vector/vector.hpp" -/* ************************************************************************** */ - namespace lasd { -/* ************************************************************************** */ - template class BinaryTreeVec : virtual public BinaryTree{ // Must extend BinaryTree -private: - protected: - using BinaryTree::size; - // using BinaryTree::Node; - struct NodeVec : virtual protected BinaryTree::Node { // Must extend Node - private: - protected: using BinaryTree::Node::data; ulong index; @@ -46,59 +33,35 @@ protected: }; protected: - Vector::NodeVec*> tree; + + using BinaryTree::size; + Vector::NodeVec*> tree; public: - - // Default constructor BinaryTreeVec() = default; - - /* ************************************************************************ */ - - // Specific constructors BinaryTreeVec(const LinearContainer&); // A binary tree obtained from a LinearContainer - - /* ************************************************************************ */ - - // Copy constructor BinaryTreeVec(const BinaryTreeVec&); - - // Move constructor BinaryTreeVec(BinaryTreeVec&&) noexcept; - /* ************************************************************************ */ - - // Destructor virtual ~BinaryTreeVec(); - /* ************************************************************************ */ - - // Copy assignment BinaryTreeVec& operator=(const BinaryTreeVec&); - - // Move assignment BinaryTreeVec& operator=(BinaryTreeVec&&) noexcept; - /* ************************************************************************ */ - - // Comparison operators bool operator==(const BinaryTreeVec&) const noexcept; bool operator!=(const BinaryTreeVec&) const noexcept; - /* ************************************************************************ */ - // Specific member functions (inherited from BinaryTree) struct BinaryTree::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) - /* ************************************************************************ */ - // Specific member functions (inherited from Container) void Clear() override; // Override Container member /* ----- override of map and fold in breadth ----- */ + using typename BinaryTree::MapFunctor; using typename BinaryTree::FoldFunctor; void MapBreadth(const MapFunctor, void*) override; @@ -106,8 +69,6 @@ public: }; -/* ************************************************************************** */ - } #include "binarytreevec.cpp" diff --git a/librerie/exercise3/main.cpp b/librerie/exercise3/main.cpp index 0c107e9..61e37e2 100755 --- a/librerie/exercise3/main.cpp +++ b/librerie/exercise3/main.cpp @@ -11,7 +11,7 @@ int main() { std::cout << "Lasd Libraries 2020" << std::endl; - //menu(); + menu(); lasdtest(); // To call in the menu of your library test! return 0; } diff --git a/librerie/exercise3/zmytest/test.hpp b/librerie/exercise3/zmytest/test.hpp index b42f0b9..bbe113e 100755 --- a/librerie/exercise3/zmytest/test.hpp +++ b/librerie/exercise3/zmytest/test.hpp @@ -17,10 +17,27 @@ #include"../binarytree/vec/binarytreevec.hpp" #include using namespace std; +using namespace lasd; /* ************************************************************************** */ void menu(){ - std::cout<<"MYTESTS\n"; + cout<<"MY TESTS\n"; + Vector vec(10); + vec[0] = "A"; + vec[1] = "B"; + vec[2] = "C"; + vec[3] = "D"; + vec[4] = "E"; + vec[5] = "F"; + vec[6] = "G"; + vec[7] = "H"; + vec[8] = "I"; + vec[9] = "L"; + + BinaryTreeLnk bt1(vec); + BinaryTreeLnk bt2(vec); +// if(bt1 == bt2) cout<<"uguali"; + }