Library 3

This commit is contained in:
Alessandro Ferro 2021-04-26 19:12:17 +02:00
parent 0d599672c4
commit c550ec0ef4
4 changed files with 301 additions and 117 deletions

Binary file not shown.

View File

@ -5,7 +5,185 @@ namespace lasd {
/* ************************************************************************** */
// ...
// checks if two trees which have the same root node are identical
template <typename Data>
bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate){
return EqualNodes(this, &toEvaluate);
}
bool BinaryTree<Data>::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 <typename Data>
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate){
return !(*this == toEvaluate)
}
template <typename Data>
Data& BinaryTree<Data>::Node::Element(){
return this->data;
}
template <typename Data>
const Data& BinaryTree<Data>::Node::Element(){
return this->data;
}
template <typename Data>
bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
return(Root() == toCompare.Root());
}
template <typename Data>
bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
return(Root() != toCompare.Root());
}
template <typename Data>
void BinaryTree<Data>::MapPreOrder(const MapFunctor function, void* par){
MapPreOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapPostOrder(const MapFunctor function, void* par){
MapPostOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){
FoldPreOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldPostOrder(const FoldFunctor function, const void* par, void* acc) const{
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 ----- */
template <typename Data>
void BinaryTree<Data>::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 <typename Data>
void BinaryTree<Data>::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 <typename Data>
void BinaryTree<Data>::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 <typename Data>
void BinaryTree<Data>::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 <typename Data>
void BinaryTree<Data>::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 <typename Data>
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 <typename Data>
void MapBreadth(const MapFunctor function, void* par, Node* node){
QueueLst<struct Node*> 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();
}
}
}
/* ************************************************************************** */

View File

@ -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 <typename Data>
class BinaryTree { // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
class BinaryTree : virtual public InOrderMappableContainer<Data>,
virtual public BreadthMappableContainer<Data>,
virtual public InOrderFoldableContainer<Data>,
virtual public BreadthFoldableContainer<Data>{ // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
private:
// ...
protected:
// using InOrder/BreadthMappableContainer<Data>::???;
// ...
using BreadthMappableContainer<Data>::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<Data>;
friend class BinaryTree<Data>;
/* ********************************************************************** */
// 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<Data>::MapFunctor;
using typename MappableContainer<Data>::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<Data>::FoldFunctor;
using typename FoldableContainer<Data>::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 <typename Data>
class BTPreOrderIterator { // Must extend ForwardIterator<Data>
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
// ...
protected:
// ...
struct BinaryTree<Data>::Node* curr;
StackLst<Data> stack;
public:
// Specific constructors
// BTPreOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTPreOrderIterator(const BinaryTree<Data>&); // 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<Data>::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 <typename Data>
class BTPostOrderIterator { // Must extend ForwardIterator<Data>
class BTPostOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
// ...
protected:
// ...
struct BinaryTree<Data>::Node* curr;
StackLst<Data> stack;
public:
// Specific constructors
// BTPostOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTPostOrderIterator(const BinaryTree<Data>&); // 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<Data>::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 <typename Data>
class BTInOrderIterator { // Must extend ForwardIterator<Data>
class BTInOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
// ...
protected:
// ...
struct BinaryTree<Data>::Node* curr;
StackLst<Data> stack;
public:
// Specific constructors
// BTInOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTInOrderIterator(const BinaryTree<Data>&); // 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<Data>::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 <typename Data>
class BTBreadthIterator { // Must extend ForwardIterator<Data>
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
private:
// ...
protected:
// ...
struct BinaryTree<Data>::Node* curr;
QueueVec<Data> queue;
public:
// Specific constructors
// BTBreadthIterator(argument) specifiers; // An iterator over a given binary tree
BTBreadthIterator(const BinaryTree<Data>&); // 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<Data>::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)
};

View File

@ -1,10 +1,18 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
void AuxFoldExists(const Data& dat, const void* val, void* exists){
if(dat == *((Data*)val)){
*((bool*)exists) = true;
}
}
// ...
/* ************************************************************************** */
template <typename Data>
bool FoldableContainer<Data>::Exists(const Data& dat) const noexcept{
bool exists = false;
FoldPreOrder(&AuxFoldExists<Data>, &dat, &exists);
return exists;
}
}