Library 4

added previous libreries
This commit is contained in:
Alessandro Ferro
2021-05-11 06:52:44 +02:00
parent 25dadb7de6
commit 97407c2a31
23 changed files with 2366 additions and 962 deletions

424
librerie/exercise4/binarytree/binarytree.hpp Normal file → Executable file
View File

@ -2,32 +2,25 @@
#ifndef BINARYTREE_HPP
#define BINARYTREE_HPP
/* ************************************************************************** */
#include "../container/container.hpp"
#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"
namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BinaryTree { // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
private:
// ...
protected:
// using InOrder/BreadthMappableContainer<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>
public:
@ -35,404 +28,221 @@ 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(const Node&, const Node&) const;
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.
/* ********************************************************************** */
// 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.
/* ********************************************************************** */
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)
// 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)
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 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)
// 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)
};
/* ************************************************************************ */
virtual ~BinaryTree() = default;
// Destructor
// ~BinaryTree() specifiers
BinaryTree& operator=(const BinaryTree&) = delete; // Copy assignment of abstract types should not be possible.
BinaryTree& operator=(BinaryTree&&) noexcept = delete; // Move assignment of abstract types should not be 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.
// Copy assignment
// type operator=(argument); // Copy assignment of abstract types should not be possible.
virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty)
// Move assignment
// type operator=(argument); // Move assignment of abstract types should not be possible.
/* ----- Map and fold functions ----- */
/* ************************************************************************ */
virtual void MapPreOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapPostOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapInOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override InOrderMappableContainer member
virtual void MapBreadth(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override BreadthMappableContainer member
// Comparison operators
// type operator==(argument) specifiers; // Comparison of abstract binary tree is possible.
// type operator!=(argument) specifiers; // Comparison of abstract binary tree is possible.
/* ************************************************************************ */
// Specific member functions
// type Root() specifiers; // (concrete function must throw std::length_error when empty)
/* ************************************************************************ */
// Specific member functions (inherited from MappableContainer)
// using typename MappableContainer<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from InOrderMappableContainer)
// type MapInOrder(arguments) specifiers; // Override InOrderMappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from InOrderFoldableContainer)
// type FoldInOrder(arguments) specifiers; // Override InOrderFoldableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from BreadthMappableContainer)
// type MapBreadth(arguments) specifiers; // Override BreadthMappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from BreadthFoldableContainer)
// type FoldBreadth(arguments) specifiers; // Override BreadthFoldableContainer member
virtual void FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member
virtual void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member
protected:
// Auxiliary member functions (for MappableContainer)
using BreadthMappableContainer<Data>::size;
// 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
/* ----- Auxiliary map and fold functions ----- */
/* ************************************************************************ */
void MapPreOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapPostOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapInOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapBreadth(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
// Auxiliary member functions (for FoldableContainer)
void FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, Node*) const; // Accessory function executing from one node of the tree
};
// 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
/* ************************************************************************ */
template <typename Data>
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
// Auxiliary member functions (for InOrderMappableContainer)
protected:
// type MapInOrder(arguments) specifiers; // Accessory function executing from one node of the tree
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
/* ************************************************************************ */
public:
// Auxiliary member functions (for InOrderFoldableContainer)
virtual ~BTPreOrderIterator();
// type FoldInOrder(arguments) specifiers; // Accessory function executing from one node of the tree
BTPreOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
BTPreOrderIterator(const BTPreOrderIterator&);
BTPreOrderIterator(BTPreOrderIterator&&) noexcept;
/* ************************************************************************ */
BTPreOrderIterator& operator=(const BTPreOrderIterator&);
BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept;
// Auxiliary member functions (for BreadthMappableContainer)
bool operator==(const BTPreOrderIterator&) const noexcept;
bool operator!=(const BTPreOrderIterator&) const noexcept;
// type MapBreadth(arguments) specifiers; // Accessory function executing from one node of the tree
// Specific member functions (inherited from Iterator)
/* ************************************************************************ */
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const noexcept; // (should not throw exceptions)
// Auxiliary member functions (for BreadthFoldableContainer)
// Specific member functions (inherited from ForwardIterator)
// type FoldBreadth(arguments) specifiers; // Accessory function executing from one node of the tree
void operator++(); // (throw std::out_of_range when terminated)
};
/* ************************************************************************** */
template <typename Data>
class BTPreOrderIterator { // Must extend ForwardIterator<Data>
private:
// ...
class BTPostOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* DeepestLeftLeaf(struct BinaryTree<Data>::Node*);
public:
// Specific constructors
// BTPreOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTPostOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
BTPostOrderIterator(const BTPostOrderIterator&);
BTPostOrderIterator(BTPostOrderIterator&&) noexcept;
/* ************************************************************************ */
virtual ~BTPostOrderIterator();
// Copy constructor
// BTPreOrderIterator(argument) specifiers;
// Move constructor
// BTPreOrderIterator(argument) specifiers;
BTPostOrderIterator& operator=(const BTPostOrderIterator&);
BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept;
/* ************************************************************************ */
// Destructor
// ~BTPreOrderIterator() specifiers;
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
// Move assignment
// type operator=(argument) specifiers;
/* ************************************************************************ */
// 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)
// type Terminated() specifiers; // (should not throw exceptions)
/* ************************************************************************ */
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const 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>
private:
// ...
class BTInOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* MostLeftNode(struct BinaryTree<Data>::Node&);
public:
// Specific constructors
// BTPostOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTInOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
BTInOrderIterator(const BTInOrderIterator&);
BTInOrderIterator(BTInOrderIterator&&) noexcept;
/* ************************************************************************ */
virtual ~BTInOrderIterator();
// Copy constructor
// BTPostOrderIterator(argument) specifiers;
BTInOrderIterator& operator=(const BTInOrderIterator&);
BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept;
// Move constructor
// BTPostOrderIterator(argument) specifiers;
/* ************************************************************************ */
// Destructor
// ~BTPostOrderIterator() specifiers;
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
// Move assignment
// type operator=(argument) specifiers;
/* ************************************************************************ */
// 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)
// type Terminated() specifiers; // (should not throw exceptions)
/* ************************************************************************ */
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const 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>
private:
// ...
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
QueueVec<struct BinaryTree<Data>::Node*> queue;
public:
// Specific constructors
// BTInOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTBreadthIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
BTBreadthIterator(const BTBreadthIterator&);
BTBreadthIterator(BTBreadthIterator&&) noexcept;
/* ************************************************************************ */
virtual ~BTBreadthIterator();
// Copy constructor
// BTInOrderIterator(argument) specifiers;
BTBreadthIterator& operator=(const BTBreadthIterator&);
BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept;
// Move constructor
// BTInOrderIterator(argument) specifiers;
/* ************************************************************************ */
// Destructor
// ~BTInOrderIterator() specifiers;
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
// Move assignment
// type operator=(argument) specifiers;
/* ************************************************************************ */
// 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)
// type Terminated() specifiers; // (should not throw exceptions)
/* ************************************************************************ */
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const 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>
private:
// ...
protected:
// ...
public:
// Specific constructors
// BTBreadthIterator(argument) specifiers; // An iterator over a given binary tree
/* ************************************************************************ */
// Copy constructor
// BTBreadthIterator(argument) specifiers;
// Move constructor
// BTBreadthIterator(argument) specifiers;
/* ************************************************************************ */
// Destructor
// ~BTBreadthIterator() specifiers;
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
// Move assignment
// type operator=(argument) specifiers;
/* ************************************************************************ */
// Comparison operators
// type operator==(argument) specifiers;
// type operator!=(argument) specifiers;
/* ************************************************************************ */
// Specific member functions (inherited from Iterator)
// type operator*() specifiers; // (throw std::out_of_range when terminated)
// type Terminated() specifiers; // (should not throw exceptions)
/* ************************************************************************ */
// Specific member functions (inherited from ForwardIterator)
// type operator++() specifiers; // (throw std::out_of_range when terminated)
};
/* ************************************************************************** */
}
#include "binarytree.cpp"