diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index fd21f92..5778d20 100644 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -11,13 +11,17 @@ bool BinaryTree::Node::operator==(const Node& toEvaluate){ return EqualNodes(this, &toEvaluate); } +/* given two nodes, checks if the subtree is the same */ 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()) - ) + if(n1->data == n2->data){ + if(n1->IsLeaf() && n2->IsLeaf()) return true; + if( (n1->HasLeftChild() && !n2->HasLeftChild()) || (n1->HasRightChild() && !n2->HasRightChild()) ) return false; + return( EqualNodes(n1->LeftChild(),n2->LeftChild()) && + EqualNodes(n1->RightChild(),n2->RightChild()) + ) + }else{ + return false; + } } template @@ -45,6 +49,8 @@ bool BinaryTree::operator!=(const BinaryTree& toCompare) const noexcept{ return(Root() != toCompare.Root()); } +/* ----- Map and fold functions ----- */ + template void BinaryTree::MapPreOrder(const MapFunctor function, void* par){ MapPreOrder(function, par, &Root()); @@ -55,6 +61,16 @@ void BinaryTree::MapPostOrder(const MapFunctor function, void* par){ MapPostOrder(function, par, &Root()); } +template +void BinaryTree::MapInOrder(const MapFunctor function, void* par){ + MapInOrder(function, par, &Root()); +} + +template +void BinaryTree::MapBreadth(const MapFunctor function, void* par){ + MapBreadth(function, par, &Root()); +} + template void BinaryTree::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){ FoldPreOrder(function, par, acc, &Root()); @@ -65,27 +81,17 @@ void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par 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 ----- */ +/* ----- Auxiliary map and fold functions ----- */ template void BinaryTree::MapPreOrder(const MapFunctor function, void* par, Node* node){ @@ -138,6 +144,7 @@ void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par function(node->Element(), par, acc); } } + template void BinaryTree::MapInOrder(const MapFunctor function, void* par, Node* node){ if(node != nullptr){ @@ -178,7 +185,7 @@ void MapBreadth(const MapFunctor function, void* par, Node* node){ if(toVisit.Head()->HasRightChild()){ toVisit.Enqueue(&(toVisit.Head()->RightChild())); } - ql.Dequeue(); + toVisit.Dequeue(); } } } diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp index 9d885e4..b6e72fb 100644 --- a/librerie/exercise3/binarytree/binarytree.hpp +++ b/librerie/exercise3/binarytree/binarytree.hpp @@ -54,25 +54,16 @@ public: friend class BinaryTree; - /* ********************************************************************** */ - // Destructor virtual ~Node() = default; - /* ********************************************************************** */ - // Copy assignment Node& operator=(const Node&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment Node& operator=(Node&&) noexcept = delete; // Move assignment of abstract types should not be possible. - /* ********************************************************************** */ - - /* ********************************************************************** */ - // Specific member functions - 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) @@ -85,111 +76,52 @@ 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 + /* ----- Specific member functions ----- */ virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty) - /* ************************************************************************ */ - - // Specific member functions (inherited from MappableContainer) + /* ----- Map and fold functions ----- */ using typename MappableContainer::MapFunctor; + using typename FoldableContainer::FoldFunctor; 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; + void MapInOrder(const MapFunctor, void*) override; // Override InOrderMappableContainer member + void MapBreadth(const MapFunctor, void*) override; // Override BreadthMappableContainer 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) - - void MapInOrder(const MapFunctor, void*) override; // Override InOrderMappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from InOrderFoldableContainer) - void FoldInOrder(const FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from BreadthMappableContainer) - - MapBreadth(const MapFunctor, void*) override; // Override BreadthMappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from BreadthFoldableContainer) - void FoldBreadth(const FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member + protected: - // Auxiliary member functions (for MappableContainer) + /* ----- Auxiliary map and fold functions ----- */ 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) + void MapInOrder(const MapFunctor, void*, Node*) override; // 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 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) - - void MapInOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree - - /* ************************************************************************ */ - - // Auxiliary member functions (for InOrderFoldableContainer) - 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) - - void MapBreadth(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree - - /* ************************************************************************ */ - - // Auxiliary member functions (for BreadthFoldableContainer) - void FoldBreadth(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree - }; /* ************************************************************************** */