diff --git a/librerie/exercise3/Exercise3.pdf b/librerie/exercise3/Exercise3.pdf new file mode 100644 index 0000000..aa82f80 Binary files /dev/null and b/librerie/exercise3/Exercise3.pdf differ diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 37c673b..fd21f92 100644 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -5,7 +5,185 @@ namespace lasd { /* ************************************************************************** */ -// ... +// checks if two trees which have the same root node are identical +template +bool BinaryTree::Node::operator==(const Node& toEvaluate){ + return EqualNodes(this, &toEvaluate); +} + +bool BinaryTree::Node::EqualNodes(Node* n1, Node* n2){ + if(n1->IsLeaf() && n2->IsLeaf() && n1->data == n2->data) return true; + if( (n1->HasLeftChild() && !n2->HasLeftChild()) || (n1->HasRightChild() && !n2->HasRightChild()) ) return false; + return( n1->data == n2->data && + EqualNodes(n1->LeftChild(),n2->LeftChild()) && + EqualNodes(n1->RightChild(),n2->RightChild()) + ) +} + +template +bool BinaryTree::Node::operator!=(const Node& toEvaluate){ + return !(*this == toEvaluate) +} + +template +Data& BinaryTree::Node::Element(){ + return this->data; +} + +template +const Data& BinaryTree::Node::Element(){ + return this->data; +} + +template +bool BinaryTree::operator==(const BinaryTree& toCompare) const noexcept{ + return(Root() == toCompare.Root()); +} + +template +bool BinaryTree::operator!=(const BinaryTree& toCompare) const noexcept{ + return(Root() != toCompare.Root()); +} + +template +void BinaryTree::MapPreOrder(const MapFunctor function, void* par){ + MapPreOrder(function, par, &Root()); +} + +template +void BinaryTree::MapPostOrder(const MapFunctor function, void* par){ + MapPostOrder(function, par, &Root()); +} + +template +void BinaryTree::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){ + FoldPreOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par, void* acc) const{ + FoldPostOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::MapInOrder(const MapFunctor function, void* par){ + MapInOrder(function, par, &Root()); +} + +template +void BinaryTree::FoldInOrder(const MapFunctor function, const void* par, void* acc) const{ + FoldInOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::MapBreadth(const MapFunctor function, void* par){ + MapBreadth(function, par, &Root()); +} + +template +void BinaryTree::FoldBreadth(const FoldFunctor function, const void* par, void* acc) const{ + FoldBreadth(function, par, acc, &Root()); +} + +/* ----- AUX ----- */ + +template +void BinaryTree::MapPreOrder(const MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + function(node->Element(), par); + if(node->HasLeftChild()){ + MapPreOrder(function, par, node->LeftChild()); + } + if(node->HasRightChild()){ + MapPreOrder(function, par, node->RightChild()); + } + } +} + +template +void BinaryTree::MapPostOrder(const MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + if(node->HasLeftChild()){ + MapPreOrder(function, par, node->LeftChild()); + } + if(node->HasRightChild()){ + MapPreOrder(function, par, node->RightChild()); + } + function(node->Element(), par); + } +} + +template +void BinaryTree::FoldPreOrder(const FoldFunctor function, const void* par, void* acc, const Node* node){ + if(node != nullptr){ + function(node->Element(), par, acc); + if(node->HasLeftChild()){ + FoldPreOrder(function, par, acc, node->LeftChild()); + } + if(node->HasRightChild()){ + FoldPreOrder(function, par, acc, node->RightChild()); + } + } +} + +template +void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par, void* acc, const Node* node){ + if(node != nullptr){ + if(node->HasLeftChild()){ + FoldPreOrder(function, par, acc, node->LeftChild()); + } + if(node->HasRightChild()){ + MapPreOrder(function, par, acc, node->RightChild()); + } + function(node->Element(), par, acc); + } +} +template +void BinaryTree::MapInOrder(const 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 FoldInOrder(const FoldFunctor function, const void* par, void* acc, const Node* node) const{ + if(node != nullptr){ + if(node->HasLeftChild()){ + FoldInOrder(function, par, acc, node->LeftChild()); + } + function(node->Element(), par, acc); + if(node->HasRightChild()){ + FoldPreOrder(function, par, acc, node->RightChild()); + } + } +} + +template +void MapBreadth(const MapFunctor function, void* par, Node* node){ + QueueLst toVisit; + if(node != nullptr){ + toVisit.Enqueue(node); + while(!toVisit.Empty()){ + func(toVisit.Head()->Element(), par); + + if(toVisit.Head()->HasLeftChild()){ + toVisit.Enqueue(&(toVisit.Head()->LeftChild())); + } + if(toVisit.Head()->HasRightChild()){ + toVisit.Enqueue(&(toVisit.Head()->RightChild())); + } + ql.Dequeue(); + } + } +} + + /* ************************************************************************** */ diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp index f3333a5..9d885e4 100644 --- a/librerie/exercise3/binarytree/binarytree.hpp +++ b/librerie/exercise3/binarytree/binarytree.hpp @@ -8,7 +8,13 @@ #include "../iterator/iterator.hpp" -// #include "..." +#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" /* ************************************************************************** */ @@ -17,17 +23,16 @@ namespace lasd { /* ************************************************************************** */ template -class BinaryTree { // Must extend InOrder/BreadthMappableContainer and InOrder/BreadthFoldableContainer +class BinaryTree : virtual public InOrderMappableContainer, + virtual public BreadthMappableContainer, + virtual public InOrderFoldableContainer, + virtual public BreadthFoldableContainer{ // Must extend InOrder/BreadthMappableContainer and InOrder/BreadthFoldableContainer private: - // ... - protected: - // using InOrder/BreadthMappableContainer::???; - - // ... + using BreadthMappableContainer::size; public: @@ -35,399 +40,392 @@ public: private: - // ... - protected: + Data data; + + // Comparison operators + bool operator==(const Node&) const noexcept; // Comparison of abstract types is possible, but should not be visible. + bool operator!=(const Node&) const noexcept; // Comparison of abstract types is possible, but should not be visible. + + bool EqualNodes(Node*, Node*); - // ... public: - // friend class BinaryTree; + friend class BinaryTree; /* ********************************************************************** */ // Destructor - // ~Node() specifiers + virtual ~Node() = default; /* ********************************************************************** */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Node& operator=(const Node&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Node& operator=(Node&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ********************************************************************** */ - // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types is possible, but should not be visible. - // type operator!=(argument) specifiers; // Comparison of abstract types is possible, but should not be visible. - /* ********************************************************************** */ // Specific member functions - // type Element() specifiers; // Mutable access to the element (concrete function should not throw exceptions) - // type Element() specifiers; // Immutable access to the element (concrete function should not throw exceptions) + Data& Element(); // Mutable access to the element (concrete function should not throw exceptions) + const Data& Element() const; // Immutable access to the element (concrete function should not throw exceptions) - // type IsLeaf() specifiers; // (concrete function should not throw exceptions) - // type HasLeftChild() specifiers; // (concrete function should not throw exceptions) - // type HasRightChild() specifiers; // (concrete function should not throw exceptions) + virtual bool IsLeaf() const noexcept = 0; // (concrete function should not throw exceptions) + virtual bool HasLeftChild() const noexcept = 0; // (concrete function should not throw exceptions) + virtual bool HasRightChild() const noexcept = 0; // (concrete function should not throw exceptions) - // type LeftChild() specifiers; // (concrete function must throw std::out_of_range when not existent) - // type RightChild() specifiers; // (concrete function must throw std::out_of_range when not existent) + virtual Node& LeftChild() const = 0; // (concrete function must throw std::out_of_range when not existent) + virtual Node& RightChild() const = 0; // (concrete function must throw std::out_of_range when not existent) }; /* ************************************************************************ */ // Destructor - // ~BinaryTree() specifiers + virtual ~BinaryTree() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + BinaryTree& operator=(const BinaryTree&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + BinaryTree& operator=(BinaryTree&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract binary tree is possible. - // type operator!=(argument) specifiers; // Comparison of abstract binary tree is possible. + 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 - // type Root() specifiers; // (concrete function must throw std::length_error when empty) + virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from MappableContainer) - // using typename MappableContainer::MapFunctor; + using typename MappableContainer::MapFunctor; - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member + void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member + void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member /* ************************************************************************ */ // Specific member functions (inherited from FoldableContainer) - // using typename FoldableContainer::FoldFunctor; + using typename FoldableContainer::FoldFunctor; - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member /* ************************************************************************ */ // Specific member functions (inherited from InOrderMappableContainer) - // type MapInOrder(arguments) specifiers; // Override InOrderMappableContainer member + void MapInOrder(const MapFunctor, void*) override; // Override InOrderMappableContainer member /* ************************************************************************ */ // Specific member functions (inherited from InOrderFoldableContainer) - // type FoldInOrder(arguments) specifiers; // Override InOrderFoldableContainer member + void FoldInOrder(const FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member /* ************************************************************************ */ // Specific member functions (inherited from BreadthMappableContainer) - // type MapBreadth(arguments) specifiers; // Override BreadthMappableContainer member + MapBreadth(const MapFunctor, void*) override; // Override BreadthMappableContainer member /* ************************************************************************ */ // Specific member functions (inherited from BreadthFoldableContainer) - // type FoldBreadth(arguments) specifiers; // Override BreadthFoldableContainer member + void FoldBreadth(const FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member protected: // Auxiliary member functions (for MappableContainer) - // type MapPreOrder(arguments) specifiers; // Accessory function executing from one node of the tree - // type MapPostOrder(arguments) specifiers; // Accessory function executing from one node of the tree + void MapPreOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree + void MapPostOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree /* ************************************************************************ */ // Auxiliary member functions (for FoldableContainer) - // type FoldPreOrder(arguments) specifiers; // Accessory function executing from one node of the tree - // type FoldPostOrder(arguments) specifiers; // Accessory function executing from one node of the tree + void FoldPreOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree + void FoldPostOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree /* ************************************************************************ */ // Auxiliary member functions (for InOrderMappableContainer) - // type MapInOrder(arguments) specifiers; // Accessory function executing from one node of the tree + void MapInOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree /* ************************************************************************ */ // Auxiliary member functions (for InOrderFoldableContainer) - // type FoldInOrder(arguments) specifiers; // Accessory function executing from one node of the tree + void FoldInOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree /* ************************************************************************ */ // Auxiliary member functions (for BreadthMappableContainer) - // type MapBreadth(arguments) specifiers; // Accessory function executing from one node of the tree + void MapBreadth(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree /* ************************************************************************ */ // Auxiliary member functions (for BreadthFoldableContainer) - // type FoldBreadth(arguments) specifiers; // Accessory function executing from one node of the tree + void FoldBreadth(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree }; /* ************************************************************************** */ template -class BTPreOrderIterator { // Must extend ForwardIterator +class BTPreOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator private: - // ... - protected: - - // ... + struct BinaryTree::Node* curr; + StackLst stack; public: // Specific constructors - // BTPreOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTPreOrderIterator(const BinaryTree&); // An iterator over a given binary tree /* ************************************************************************ */ // Copy constructor - // BTPreOrderIterator(argument) specifiers; + BTPreOrderIterator(const BTPreOrderIterator&); // Move constructor - // BTPreOrderIterator(argument) specifiers; + BTPreOrderIterator(BTPreOrderIterator&&) noexcept; /* ************************************************************************ */ // Destructor - // ~BTPreOrderIterator() specifiers; + virtual ~BTPreOrderIterator(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + BTPreOrderIterator& operator=(const BTPreOrderIterator&); // Move assignment - // type operator=(argument) specifiers; + BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const BTPreOrderIterator&) const noexcept; + bool operator!=(const BTPreOrderIterator&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) + struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) - // type Terminated() specifiers; // (should not throw exceptions) + bool Terminated() noexcept; // (should not throw exceptions) /* ************************************************************************ */ // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; /* ************************************************************************** */ template -class BTPostOrderIterator { // Must extend ForwardIterator +class BTPostOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator private: - // ... - protected: - - // ... + struct BinaryTree::Node* curr; + StackLst stack; public: // Specific constructors - // BTPostOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTPostOrderIterator(const BinaryTree&); // An iterator over a given binary tree /* ************************************************************************ */ // Copy constructor - // BTPostOrderIterator(argument) specifiers; + BTPostOrderIterator(const BTPostOrderIterator&); // Move constructor - // BTPostOrderIterator(argument) specifiers; + BTPostOrderIterator(BTPostOrderIterator&&) noexcept; /* ************************************************************************ */ // Destructor - // ~BTPostOrderIterator() specifiers; + virtual ~BTPostOrderIterator(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + BTPostOrderIterator& operator=(const BTPostOrderIterator&); // Move assignment - // type operator=(argument) specifiers; + BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const BTPostOrderIterator&) const noexcept; + bool operator!=(const BTPostOrderIterator&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) + struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) - // type Terminated() specifiers; // (should not throw exceptions) + bool Terminated() noexcept; // (should not throw exceptions) /* ************************************************************************ */ // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; /* ************************************************************************** */ template -class BTInOrderIterator { // Must extend ForwardIterator +class BTInOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator private: - // ... - protected: - - // ... + struct BinaryTree::Node* curr; + StackLst stack; public: // Specific constructors - // BTInOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTInOrderIterator(const BinaryTree&); // An iterator over a given binary tree /* ************************************************************************ */ // Copy constructor - // BTInOrderIterator(argument) specifiers; + BTInOrderIterator(const BTInOrderIterator&); // Move constructor - // BTInOrderIterator(argument) specifiers; + BTInOrderIterator(BTInOrderIterator&&) noexcept; /* ************************************************************************ */ // Destructor - // ~BTInOrderIterator() specifiers; + virtual ~BTInOrderIterator(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + BTInOrderIterator& operator=(const BTInOrderIterator&); // Move assignment - // type operator=(argument) specifiers; + BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const BTInOrderIterator&) const noexcept; + bool operator!=(const BTInOrderIterator&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) + struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) - // type Terminated() specifiers; // (should not throw exceptions) + bool Terminated() noexcept; // (should not throw exceptions) /* ************************************************************************ */ // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; /* ************************************************************************** */ template -class BTBreadthIterator { // Must extend ForwardIterator +class BTBreadthIterator : virtual public ForwardIterator{ // Must extend ForwardIterator private: - // ... - protected: - // ... + struct BinaryTree::Node* curr; + QueueVec queue; public: // Specific constructors - // BTBreadthIterator(argument) specifiers; // An iterator over a given binary tree + BTBreadthIterator(const BinaryTree&); // An iterator over a given binary tree /* ************************************************************************ */ // Copy constructor - // BTBreadthIterator(argument) specifiers; + BTBreadthIterator(const BTBreadthIterator&); // Move constructor - // BTBreadthIterator(argument) specifiers; + BTBreadthIterator(BTBreadthIterator&&) noexcept; /* ************************************************************************ */ // Destructor - // ~BTBreadthIterator() specifiers; + virtual ~BTBreadthIterator(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + BTBreadthIterator& operator=(const BTBreadthIterator&); // Move assignment - // type operator=(argument) specifiers; + BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const BTBreadthIterator&) const noexcept; + bool operator!=(const BTBreadthIterator&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) + struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) - // type Terminated() specifiers; // (should not throw exceptions) + bool Terminated() noexcept; // (should not throw exceptions) /* ************************************************************************ */ // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; diff --git a/librerie/exercise3/container/container.cpp b/librerie/exercise3/container/container.cpp index d374ceb..6bbe4bf 100644 --- a/librerie/exercise3/container/container.cpp +++ b/librerie/exercise3/container/container.cpp @@ -1,10 +1,18 @@ namespace lasd { -/* ************************************************************************** */ +template +void AuxFoldExists(const Data& dat, const void* val, void* exists){ + if(dat == *((Data*)val)){ + *((bool*)exists) = true; + } +} -// ... - -/* ************************************************************************** */ +template +bool FoldableContainer::Exists(const Data& dat) const noexcept{ + bool exists = false; + FoldPreOrder(&AuxFoldExists, &dat, &exists); + return exists; +} }