Library 3

cleaner code
This commit is contained in:
Alessandro Ferro
2021-05-05 22:37:45 +02:00
parent 2b6dcf6700
commit 8e56f33808
8 changed files with 138 additions and 271 deletions

View File

@ -2,8 +2,6 @@
#ifndef BINARYTREE_HPP
#define BINARYTREE_HPP
/* ************************************************************************** */
#include "../container/container.hpp"
#include "../iterator/iterator.hpp"
@ -16,24 +14,14 @@
#include "../stack/lst/stacklst.hpp"
#include "../stack/vec/stackvec.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename 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 BreadthMappableContainer<Data>::size;
public:
struct Node {
@ -76,21 +64,14 @@ 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 ----- */
virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty)
/* ----- Map and fold functions ----- */
@ -105,9 +86,10 @@ public:
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:
using BreadthMappableContainer<Data>::size;
/* ----- Auxiliary map and fold functions ----- */
void MapPreOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
@ -121,59 +103,34 @@ protected:
void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, Node*) const; // Accessory function executing from one node of the tree
};
/* ************************************************************************** */
template <typename Data>
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
protected:
struct BinaryTree<Data>::Node* curr;
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
public:
// Specific constructors
BTPreOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
/* ************************************************************************ */
// Copy constructor
BTPreOrderIterator(const BTPreOrderIterator&);
// Move constructor
BTPreOrderIterator(BTPreOrderIterator&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BTPreOrderIterator();
/* ************************************************************************ */
BTPreOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
BTPreOrderIterator(const BTPreOrderIterator&);
BTPreOrderIterator(BTPreOrderIterator&&) noexcept;
// Copy assignment
BTPreOrderIterator& operator=(const BTPreOrderIterator&);
// Move assignment
BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BTPreOrderIterator&) const noexcept;
bool operator!=(const BTPreOrderIterator&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from Iterator)
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const noexcept; // (should not throw exceptions)
/* ************************************************************************ */
// Specific member functions (inherited from ForwardIterator)
void operator++(); // (throw std::out_of_range when terminated)
@ -185,54 +142,33 @@ public:
template <typename Data>
class BTPostOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
protected:
struct BinaryTree<Data>::Node* curr;
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* DeepestLeftLeaf(struct BinaryTree<Data>::Node*);
public:
// Specific constructors
BTPostOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
/* ************************************************************************ */
// Copy constructor
BTPostOrderIterator(const BTPostOrderIterator&);
// Move constructor
BTPostOrderIterator(BTPostOrderIterator&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BTPostOrderIterator();
/* ************************************************************************ */
// Copy assignment
BTPostOrderIterator& operator=(const BTPostOrderIterator&);
// Move assignment
BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BTPostOrderIterator&) const noexcept;
bool operator!=(const BTPostOrderIterator&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from Iterator)
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const noexcept; // (should not throw exceptions)
/* ************************************************************************ */
// Specific member functions (inherited from ForwardIterator)
void operator++(); // (throw std::out_of_range when terminated)
@ -244,122 +180,71 @@ public:
template <typename Data>
class BTInOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
private:
protected:
struct BinaryTree<Data>::Node* curr;
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* MostLeftNode(struct BinaryTree<Data>::Node&);
public:
// Specific constructors
BTInOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
/* ************************************************************************ */
// Copy constructor
BTInOrderIterator(const BTInOrderIterator&);
// Move constructor
BTInOrderIterator(BTInOrderIterator&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BTInOrderIterator();
/* ************************************************************************ */
// Copy assignment
BTInOrderIterator& operator=(const BTInOrderIterator&);
// Move assignment
BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BTInOrderIterator&) const noexcept;
bool operator!=(const BTInOrderIterator&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from Iterator)
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const noexcept; // (should not throw exceptions)
/* ************************************************************************ */
// Specific member functions (inherited from ForwardIterator)
void operator++(); // (throw std::out_of_range when terminated)
};
/* ************************************************************************** */
template <typename Data>
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
private:
protected:
struct BinaryTree<Data>::Node* curr;
struct BinaryTree<Data>::Node* curr = nullptr;
QueueVec<struct BinaryTree<Data>::Node*> queue;
public:
// Specific constructors
BTBreadthIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
/* ************************************************************************ */
// Copy constructor
BTBreadthIterator(const BTBreadthIterator&);
// Move constructor
BTBreadthIterator(BTBreadthIterator&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BTBreadthIterator();
/* ************************************************************************ */
// Copy assignment
BTBreadthIterator& operator=(const BTBreadthIterator&);
// Move assignment
BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BTBreadthIterator&) const noexcept;
bool operator!=(const BTBreadthIterator&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from Iterator)
Data& operator*() const; // (throw std::out_of_range when terminated)
bool Terminated() const noexcept; // (should not throw exceptions)
/* ************************************************************************ */
// Specific member functions (inherited from ForwardIterator)
void operator++(); // (throw std::out_of_range when terminated)
};
/* ************************************************************************** */
}
#include "binarytree.cpp"