Exercise 3

Cleaner code
This commit is contained in:
Alessandro Ferro 2021-04-26 22:10:28 +02:00
parent ffe60f213b
commit 3997085c8f
2 changed files with 34 additions and 95 deletions

View File

@ -11,13 +11,17 @@ bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate){
return EqualNodes(this, &toEvaluate); return EqualNodes(this, &toEvaluate);
} }
/* given two nodes, checks if the subtree is the same */
bool BinaryTree<Data>::Node::EqualNodes(Node* n1, Node* n2){ bool BinaryTree<Data>::Node::EqualNodes(Node* n1, Node* n2){
if(n1->IsLeaf() && n2->IsLeaf() && n1->data == n2->data) return true; if(n1->data == n2->data){
if(n1->IsLeaf() && n2->IsLeaf()) return true;
if( (n1->HasLeftChild() && !n2->HasLeftChild()) || (n1->HasRightChild() && !n2->HasRightChild()) ) return false; if( (n1->HasLeftChild() && !n2->HasLeftChild()) || (n1->HasRightChild() && !n2->HasRightChild()) ) return false;
return( n1->data == n2->data && return( EqualNodes(n1->LeftChild(),n2->LeftChild()) &&
EqualNodes(n1->LeftChild(),n2->LeftChild()) &&
EqualNodes(n1->RightChild(),n2->RightChild()) EqualNodes(n1->RightChild(),n2->RightChild())
) )
}else{
return false;
}
} }
template <typename Data> template <typename Data>
@ -45,6 +49,8 @@ bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
return(Root() != toCompare.Root()); return(Root() != toCompare.Root());
} }
/* ----- Map and fold functions ----- */
template <typename Data> template <typename Data>
void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par){ void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par){
MapPreOrder(function, par, &Root()); MapPreOrder(function, par, &Root());
@ -55,6 +61,16 @@ void BinaryTree<Data>::MapPostOrder(const MapFunctor function, void* par){
MapPostOrder(function, par, &Root()); MapPostOrder(function, par, &Root());
} }
template <typename Data>
void BinaryTree<Data>::MapInOrder(const MapFunctor function, void* par){
MapInOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapBreadth(const MapFunctor function, void* par){
MapBreadth(function, par, &Root());
}
template <typename Data> template <typename Data>
void BinaryTree<Data>::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){ void BinaryTree<Data>::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){
FoldPreOrder(function, par, acc, &Root()); FoldPreOrder(function, par, acc, &Root());
@ -65,27 +81,17 @@ void BinaryTree<Data>::FoldPostOrder(const FoldFunctor function, const void* par
FoldPostOrder(function, par, acc, &Root()); FoldPostOrder(function, par, acc, &Root());
} }
template <typename Data>
void BinaryTree<Data>::MapInOrder(const MapFunctor function, void* par){
MapInOrder(function, par, &Root());
}
template <typename Data> template <typename Data>
void BinaryTree<Data>::FoldInOrder(const MapFunctor function, const void* par, void* acc) const{ void BinaryTree<Data>::FoldInOrder(const MapFunctor function, const void* par, void* acc) const{
FoldInOrder(function, par, acc, &Root()); FoldInOrder(function, par, acc, &Root());
} }
template <typename Data>
void BinaryTree<Data>::MapBreadth(const MapFunctor function, void* par){
MapBreadth(function, par, &Root());
}
template <typename Data> template <typename Data>
void BinaryTree<Data>::FoldBreadth(const FoldFunctor function, const void* par, void* acc) const{ void BinaryTree<Data>::FoldBreadth(const FoldFunctor function, const void* par, void* acc) const{
FoldBreadth(function, par, acc, &Root()); FoldBreadth(function, par, acc, &Root());
} }
/* ----- AUX ----- */ /* ----- Auxiliary map and fold functions ----- */
template <typename Data> template <typename Data>
void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par, Node* node){ void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par, Node* node){
@ -138,6 +144,7 @@ void BinaryTree<Data>::FoldPostOrder(const FoldFunctor function, const void* par
function(node->Element(), par, acc); function(node->Element(), par, acc);
} }
} }
template <typename Data> template <typename Data>
void BinaryTree<Data>::MapInOrder(const MapFunctor function, void* par, Node* node){ void BinaryTree<Data>::MapInOrder(const MapFunctor function, void* par, Node* node){
if(node != nullptr){ if(node != nullptr){
@ -178,7 +185,7 @@ void MapBreadth(const MapFunctor function, void* par, Node* node){
if(toVisit.Head()->HasRightChild()){ if(toVisit.Head()->HasRightChild()){
toVisit.Enqueue(&(toVisit.Head()->RightChild())); toVisit.Enqueue(&(toVisit.Head()->RightChild()));
} }
ql.Dequeue(); toVisit.Dequeue();
} }
} }
} }

View File

@ -54,25 +54,16 @@ public:
friend class BinaryTree<Data>; friend class BinaryTree<Data>;
/* ********************************************************************** */
// Destructor // Destructor
virtual ~Node() = default; virtual ~Node() = default;
/* ********************************************************************** */
// Copy assignment // Copy assignment
Node& operator=(const Node&) = delete; // 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 // Move assignment
Node& operator=(Node&&) noexcept = delete; // Move assignment of abstract types should not be possible. Node& operator=(Node&&) noexcept = delete; // Move assignment of abstract types should not be possible.
/* ********************************************************************** */
/* ********************************************************************** */
// Specific member functions // Specific member functions
Data& Element(); // Mutable 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) const Data& Element() const; // Immutable access to the element (concrete function should not throw exceptions)
@ -85,111 +76,52 @@ public:
}; };
/* ************************************************************************ */
// Destructor // Destructor
virtual ~BinaryTree() = default; virtual ~BinaryTree() = default;
/* ************************************************************************ */
// Copy assignment // Copy assignment
BinaryTree& operator=(const BinaryTree&) = delete; // 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 // Move assignment
BinaryTree& operator=(BinaryTree&&) noexcept = delete; // 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 // 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.
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) virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty)
/* ************************************************************************ */ /* ----- Map and fold functions ----- */
// Specific member functions (inherited from MappableContainer)
using typename MappableContainer<Data>::MapFunctor; using typename MappableContainer<Data>::MapFunctor;
using typename FoldableContainer<Data>::FoldFunctor;
void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member
void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member
void MapInOrder(const MapFunctor, void*) override; // Override InOrderMappableContainer member
/* ************************************************************************ */ void MapBreadth(const MapFunctor, void*) override; // Override BreadthMappableContainer member
// Specific member functions (inherited from FoldableContainer)
using typename FoldableContainer<Data>::FoldFunctor;
void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // 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 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 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 void FoldBreadth(const FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member
protected: 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 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 void MapPostOrder(const MapFunctor, void*, Node*) override; // 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
/* ************************************************************************ */ void MapBreadth(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree
// Auxiliary member functions (for FoldableContainer)
void FoldPreOrder(const FoldFunctor, const void*, void*, const Node*) const 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 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 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 void FoldBreadth(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree
}; };
/* ************************************************************************** */ /* ************************************************************************** */