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);
}
/* given two nodes, checks if the subtree is the same */
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;
return( n1->data == n2->data &&
EqualNodes(n1->LeftChild(),n2->LeftChild()) &&
return( EqualNodes(n1->LeftChild(),n2->LeftChild()) &&
EqualNodes(n1->RightChild(),n2->RightChild())
)
}else{
return false;
}
}
template <typename Data>
@ -45,6 +49,8 @@ bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
return(Root() != toCompare.Root());
}
/* ----- Map and fold functions ----- */
template <typename Data>
void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par){
MapPreOrder(function, par, &Root());
@ -55,6 +61,16 @@ void BinaryTree<Data>::MapPostOrder(const MapFunctor function, void* par){
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>
void BinaryTree<Data>::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){
FoldPreOrder(function, par, acc, &Root());
@ -65,27 +81,17 @@ void BinaryTree<Data>::FoldPostOrder(const FoldFunctor function, const void* par
FoldPostOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapInOrder(const MapFunctor function, void* par){
MapInOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldInOrder(const MapFunctor function, const void* par, void* acc) const{
FoldInOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapBreadth(const MapFunctor function, void* par){
MapBreadth(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldBreadth(const FoldFunctor function, const void* par, void* acc) const{
FoldBreadth(function, par, acc, &Root());
}
/* ----- AUX ----- */
/* ----- Auxiliary map and fold functions ----- */
template <typename Data>
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);
}
}
template <typename Data>
void BinaryTree<Data>::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();
}
}
}

View File

@ -54,25 +54,16 @@ public:
friend class BinaryTree<Data>;
/* ********************************************************************** */
// 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<Data>::MapFunctor;
using typename FoldableContainer<Data>::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<Data>::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
};
/* ************************************************************************** */