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,21 +2,27 @@
#include "../queue/queue.hpp" #include "../queue/queue.hpp"
#include "../queue/vec/queuevec.hpp" #include "../queue/vec/queuevec.hpp"
#include "../queue/lst/queuelst.hpp" #include "../queue/lst/queuelst.hpp"
#include "../stack/stack.hpp" #include "../stack/stack.hpp"
#include "../stack/lst/stacklst.hpp" #include "../stack/lst/stacklst.hpp"
#include "../stack/vec/stackvec.hpp" #include "../stack/vec/stackvec.hpp"
#include<iostream> #include<iostream>
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ----- begin of class BinaryTree ----- */
/* ----- begin of struct Node ----- */
// checks if two trees which have the same root node are identical
template <typename Data> template <typename Data>
bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate) const noexcept{ bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate) const noexcept{
return EqualNodes(*this, toEvaluate); return EqualNodes(*this, toEvaluate);
} }
template <typename Data>
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate) const noexcept{
return !(*this == toEvaluate);
}
/* given two nodes, checks if the subtree is the same */ /* given two nodes, checks if the subtree is the same */
template <typename Data> template <typename Data>
bool BinaryTree<Data>::Node::EqualNodes(const Node& n1, const Node& n2) const{ bool BinaryTree<Data>::Node::EqualNodes(const Node& n1, const Node& n2) const{
@ -42,11 +48,6 @@ bool BinaryTree<Data>::Node::EqualNodes(const Node& n1, const Node& n2) const{
} }
} }
template <typename Data>
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate) const noexcept{
return !(*this == toEvaluate);
}
template <typename Data> template <typename Data>
Data& BinaryTree<Data>::Node::Element(){ Data& BinaryTree<Data>::Node::Element(){
return this->data; return this->data;
@ -57,6 +58,8 @@ const Data& BinaryTree<Data>::Node::Element() const{
return this->data; return this->data;
} }
/* ----- end of struct Node ----- */
template <typename Data> template <typename Data>
bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{ bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
if(size!=toCompare.size) return false; if(size!=toCompare.size) return false;
@ -65,7 +68,7 @@ bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
template <typename Data> template <typename Data>
bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{ bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
return(Root() != toCompare.Root()); return !(*this == toCompare);
} }
/* ----- Map and fold functions ----- */ /* ----- Map and fold functions ----- */
@ -129,15 +132,47 @@ template <typename Data>
void BinaryTree<Data>::MapPostOrder(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){ void BinaryTree<Data>::MapPostOrder(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
if(node != nullptr){ if(node != nullptr){
if(node->HasLeftChild()){ if(node->HasLeftChild()){
MapPreOrder(function, par, &(node->LeftChild())); MapPostOrder(function, par, &(node->LeftChild()));
} }
if(node->HasRightChild()){ if(node->HasRightChild()){
MapPreOrder(function, par, &(node->RightChild())); MapPostOrder(function, par, &(node->RightChild()));
} }
function(node->Element(), par); function(node->Element(), par);
} }
} }
template <typename Data>
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::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 BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
QueueLst<struct Node*> toVisit;
if(node != nullptr){
toVisit.Enqueue(node);
while(!toVisit.Empty()){
function(toVisit.Head()->Element(), par);
if(toVisit.Head()->HasLeftChild()){
toVisit.Enqueue(&(toVisit.Head()->LeftChild()));
}
if(toVisit.Head()->HasRightChild()){
toVisit.Enqueue(&(toVisit.Head()->RightChild()));
}
toVisit.Dequeue();
}
}
}
template <typename Data> template <typename Data>
void BinaryTree<Data>::FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{ void BinaryTree<Data>::FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{
if(node != nullptr){ if(node != nullptr){
@ -164,19 +199,6 @@ void BinaryTree<Data>::FoldPostOrder(const typename FoldableContainer<Data>::Fol
} }
} }
template <typename Data>
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::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> template <typename Data>
void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{ void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{
if(node != nullptr){ if(node != nullptr){
@ -185,26 +207,7 @@ void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldF
} }
function(node->Element(), par, acc); function(node->Element(), par, acc);
if(node->HasRightChild()){ if(node->HasRightChild()){
FoldPreOrder(function, par, acc, &(node->RightChild())); FoldInOrder(function, par, acc, &(node->RightChild()));
}
}
}
template <typename Data>
void BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
QueueLst<struct Node*> toVisit;
if(node != nullptr){
toVisit.Enqueue(node);
while(!toVisit.Empty()){
function(toVisit.Head()->Element(), par);
if(toVisit.Head()->HasLeftChild()){
toVisit.Enqueue(&(toVisit.Head()->LeftChild()));
}
if(toVisit.Head()->HasRightChild()){
toVisit.Enqueue(&(toVisit.Head()->RightChild()));
}
toVisit.Dequeue();
} }
} }
} }
@ -228,6 +231,10 @@ void BinaryTree<Data>::FoldBreadth(const typename FoldableContainer<Data>::FoldF
} }
} }
/* ----- end of class BinaryTree ----- */
/* ----- begin of class BTPreOrderIterator ----- */
template <typename Data> template <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){ BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
curr = &tree.Root(); curr = &tree.Root();
@ -235,7 +242,6 @@ BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
template <typename Data> template <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(const BTPreOrderIterator& itr){ BTPreOrderIterator<Data>::BTPreOrderIterator(const BTPreOrderIterator& itr){
//curr = &(*itr);
curr = itr.curr; curr = itr.curr;
stack = itr.stack; stack = itr.stack;
} }
@ -254,7 +260,6 @@ BTPreOrderIterator<Data>::~BTPreOrderIterator(){
template <typename Data> template <typename Data>
BTPreOrderIterator<Data>& BTPreOrderIterator<Data>::operator=(const BTPreOrderIterator& itr){ BTPreOrderIterator<Data>& BTPreOrderIterator<Data>::operator=(const BTPreOrderIterator& itr){
//curr = &(*itr);
curr = itr.curr; curr = itr.curr;
stack = itr.stack; stack = itr.stack;
return *this; return *this;
@ -279,6 +284,7 @@ bool BTPreOrderIterator<Data>::operator!=(const BTPreOrderIterator& itr) const n
template <typename Data> template <typename Data>
Data& BTPreOrderIterator<Data>::operator*() const{ Data& BTPreOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element(); return curr->Element();
} }
@ -311,6 +317,10 @@ void BTPreOrderIterator<Data>::operator++(){
} }
} }
/* ----- end of class BTPreOrderIterator ----- */
/* ----- begin of class BTPostOrderIterator ----- */
template <typename Data> template <typename Data>
struct BinaryTree<Data>::Node* BTPostOrderIterator<Data>::DeepestLeftLeaf(struct BinaryTree<Data>::Node* node){ struct BinaryTree<Data>::Node* BTPostOrderIterator<Data>::DeepestLeftLeaf(struct BinaryTree<Data>::Node* node){
if(node->HasLeftChild()){ if(node->HasLeftChild()){
@ -374,6 +384,7 @@ bool BTPostOrderIterator<Data>::operator!=(const BTPostOrderIterator& itr) const
template <typename Data> template <typename Data>
Data& BTPostOrderIterator<Data>::operator*() const{ Data& BTPostOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element(); return curr->Element();
} }
@ -384,8 +395,16 @@ bool BTPostOrderIterator<Data>::Terminated() const noexcept{
template <typename Data> template <typename Data>
void BTPostOrderIterator<Data>::operator++(){ void BTPostOrderIterator<Data>::operator++(){
/*
* If we're coming from the left then we have to analyze the tree on the right
* (if existent). Otherwise we just top 'n' pop.
*/
if(Terminated()) throw std::out_of_range("Iterator is terminated!"); if(Terminated()) throw std::out_of_range("Iterator is terminated!");
try{
if(stack.Empty()){
curr = nullptr;
}else{
if( curr == &((stack.Top())->LeftChild()) ){ if( curr == &((stack.Top())->LeftChild()) ){
if( (stack.Top())->HasRightChild() ){ if( (stack.Top())->HasRightChild() ){
curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); curr = DeepestLeftLeaf(&((stack.Top())->RightChild()));
@ -395,11 +414,13 @@ void BTPostOrderIterator<Data>::operator++(){
}else{ }else{
curr = stack.TopNPop(); curr = stack.TopNPop();
} }
}catch(std::length_error exc){
curr = nullptr;
} }
} }
/* ----- end of class BTPostOrderIterator ----- */
/* ----- begin of class BTInOrderIterator ----- */
template <typename Data> template <typename Data>
struct BinaryTree<Data>::Node* BTInOrderIterator<Data>::MostLeftNode(struct BinaryTree<Data>::Node& root){ struct BinaryTree<Data>::Node* BTInOrderIterator<Data>::MostLeftNode(struct BinaryTree<Data>::Node& root){
if(root.HasLeftChild()){ if(root.HasLeftChild()){
@ -459,6 +480,7 @@ bool BTInOrderIterator<Data>::operator!=(const BTInOrderIterator& itr) const noe
template <typename Data> template <typename Data>
Data& BTInOrderIterator<Data>::operator*() const{ Data& BTInOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element(); return curr->Element();
} }
@ -474,14 +496,18 @@ void BTInOrderIterator<Data>::operator++(){
if(curr->HasRightChild()){ if(curr->HasRightChild()){
curr = MostLeftNode(curr->RightChild()); curr = MostLeftNode(curr->RightChild());
}else{ }else{
try{ if(stack.Empty()){
curr = stack.TopNPop();
}catch(std::length_error exc){
curr = nullptr; curr = nullptr;
}else{
curr = stack.TopNPop();
} }
} }
} }
/* ----- end of class BTInOrderIterator ----- */
/* ----- begin of class BTBreadthIteratorOrderIterator ----- */
template <typename Data> template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){ BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){
curr = &(tree.Root()); curr = &(tree.Root());
@ -531,6 +557,7 @@ bool BTBreadthIterator<Data>::operator!=(const BTBreadthIterator& itr) const noe
template <typename Data> template <typename Data>
Data& BTBreadthIterator<Data>::operator*() const{ Data& BTBreadthIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element(); return curr->Element();
} }
@ -542,6 +569,7 @@ bool BTBreadthIterator<Data>::Terminated() const noexcept{
template <typename Data> template <typename Data>
void BTBreadthIterator<Data>::operator++(){ void BTBreadthIterator<Data>::operator++(){
if(Terminated()) throw std::out_of_range("Iterator is terminated!"); if(Terminated()) throw std::out_of_range("Iterator is terminated!");
if(curr->HasLeftChild()){ if(curr->HasLeftChild()){
queue.Enqueue(&(curr->LeftChild())); queue.Enqueue(&(curr->LeftChild()));
} }

View File

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

View File

@ -1,7 +1,8 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ----- begin of struct NodeLnk ----- */
template <typename Data> template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const BinaryTreeLnk<Data>::NodeLnk& node){ struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const BinaryTreeLnk<Data>::NodeLnk& node){
data = node.data; data = node.data;
@ -43,6 +44,10 @@ struct BinaryTree<Data>::Node& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
return *right; return *right;
} }
/* ----- end of struct NodeLnk ----- */
/* ----- begin of class BinaryTreeLnk ----- */
// creates a tree from a linear container in breadth // creates a tree from a linear container in breadth
template <typename Data> template <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(const LinearContainer<Data>& lc){ BinaryTreeLnk<Data>::BinaryTreeLnk(const LinearContainer<Data>& lc){
@ -55,7 +60,6 @@ struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateTreeFromLinearC
if(position >= lc.Size()) return nullptr; if(position >= lc.Size()) return nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]); struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]);
// if(position == 0) root = tmp;
tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1); tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1);
tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2); tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2);
@ -130,6 +134,7 @@ BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(const BinaryTreeLnk<Data>& t
template <typename Data> template <typename Data>
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree) noexcept{ BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree) noexcept{
Clear();
std::swap(size, tree.size); std::swap(size, tree.size);
std::swap(root, tree.root); std::swap(root, tree.root);
return *this; return *this;

View File

@ -1,30 +1,17 @@
#ifndef BINARYTREELNK_HPP #ifndef BINARYTREELNK_HPP
#define BINARYTREELNK_HPP #define BINARYTREELNK_HPP
/* ************************************************************************** */
#include "../binarytree.hpp" #include "../binarytree.hpp"
/* ************************************************************************** */
namespace lasd { namespace lasd {
/* ************************************************************************** */
template <typename Data> template <typename Data>
class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data> class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
private:
protected: protected:
using BinaryTree<Data>::size;
struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node
private:
protected: protected:
using BinaryTree<Data>::Node::data; using BinaryTree<Data>::Node::data;
@ -46,64 +33,42 @@ protected:
}; };
protected: protected:
using BinaryTree<Data>::size;
struct BinaryTreeLnk<Data>::NodeLnk* root = nullptr; struct BinaryTreeLnk<Data>::NodeLnk* root = nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data); struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data);
public: public:
// Default constructor // Default constructor
BinaryTreeLnk() = default; BinaryTreeLnk() = default;
/* ************************************************************************ */
// Specific constructors
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
BinaryTreeLnk(const BinaryTreeLnk<Data>&); BinaryTreeLnk(const BinaryTreeLnk<Data>&);
// Move constructor
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept; BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BinaryTreeLnk(); virtual ~BinaryTreeLnk();
/* ************************************************************************ */
// Copy assignment
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&); BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
// Move assignment
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept; BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BinaryTreeLnk<Data>&) const noexcept; bool operator==(const BinaryTreeLnk<Data>&) const noexcept;
bool operator!=(const BinaryTreeLnk<Data>&) const noexcept; bool operator!=(const BinaryTreeLnk<Data>&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from BinaryTree) // Specific member functions (inherited from BinaryTree)
struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
/* ************************************************************************ */
// Specific member functions (inherited from Container) // Specific member functions (inherited from Container)
void Clear() override; // Override Container member void Clear() override; // Override Container member
// Specific functions
struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong); struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong);
struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::Node*); struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::Node*);
void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node); void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node);
}; };
/* ************************************************************************** */
} }
#include "binarytreelnk.cpp" #include "binarytreelnk.cpp"

View File

@ -1,7 +1,8 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ----- begin of struct NodeVec ----- */
template <typename Data> template <typename Data>
BinaryTreeVec<Data>::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec<Data>* ref){ BinaryTreeVec<Data>::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec<Data>* ref){
data = dat; data = dat;
@ -64,6 +65,10 @@ struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::NodeVec::RightChild() const{
throw std::out_of_range("Right child does not exist!"); throw std::out_of_range("Right child does not exist!");
} }
/* ----- end of struct NodeVec ----- */
/* ----- begin of class BinaryTreeVec ----- */
template <typename Data> template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(const LinearContainer<Data>& lc){ BinaryTreeVec<Data>::BinaryTreeVec(const LinearContainer<Data>& lc){
tree.Resize(lc.Size()); tree.Resize(lc.Size());
@ -137,6 +142,7 @@ bool BinaryTreeVec<Data>::operator!=(const BinaryTreeVec& bt) const noexcept{
template <typename Data> template <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::Root() const{ struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::Root() const{
if(size==0) throw std::length_error("Empty tree!");
return *(tree.Front()); return *(tree.Front());
} }

View File

@ -2,31 +2,18 @@
#ifndef BINARYTREEVEC_HPP #ifndef BINARYTREEVEC_HPP
#define BINARYTREEVEC_HPP #define BINARYTREEVEC_HPP
/* ************************************************************************** */
#include "../binarytree.hpp" #include "../binarytree.hpp"
#include "../../vector/vector.hpp" #include "../../vector/vector.hpp"
/* ************************************************************************** */
namespace lasd { namespace lasd {
/* ************************************************************************** */
template <typename Data> template <typename Data>
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data> class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
private:
protected: protected:
using BinaryTree<Data>::size;
// using BinaryTree<Data>::Node;
struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node
private:
protected: protected:
using BinaryTree<Data>::Node::data; using BinaryTree<Data>::Node::data;
ulong index; ulong index;
@ -46,59 +33,35 @@ protected:
}; };
protected: protected:
using BinaryTree<Data>::size;
Vector<struct BinaryTreeVec<Data>::NodeVec*> tree; Vector<struct BinaryTreeVec<Data>::NodeVec*> tree;
public: public:
// Default constructor
BinaryTreeVec() = default; BinaryTreeVec() = default;
/* ************************************************************************ */
// Specific constructors
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
BinaryTreeVec(const BinaryTreeVec<Data>&); BinaryTreeVec(const BinaryTreeVec<Data>&);
// Move constructor
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept; BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BinaryTreeVec(); virtual ~BinaryTreeVec();
/* ************************************************************************ */
// Copy assignment
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&); BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
// Move assignment
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept; BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BinaryTreeVec&) const noexcept; bool operator==(const BinaryTreeVec&) const noexcept;
bool operator!=(const BinaryTreeVec&) const noexcept; bool operator!=(const BinaryTreeVec&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from BinaryTree) // Specific member functions (inherited from BinaryTree)
struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
/* ************************************************************************ */
// Specific member functions (inherited from Container) // Specific member functions (inherited from Container)
void Clear() override; // Override Container member void Clear() override; // Override Container member
/* ----- override of map and fold in breadth ----- */ /* ----- override of map and fold in breadth ----- */
using typename BinaryTree<Data>::MapFunctor; using typename BinaryTree<Data>::MapFunctor;
using typename BinaryTree<Data>::FoldFunctor; using typename BinaryTree<Data>::FoldFunctor;
void MapBreadth(const MapFunctor, void*) override; void MapBreadth(const MapFunctor, void*) override;
@ -106,8 +69,6 @@ public:
}; };
/* ************************************************************************** */
} }
#include "binarytreevec.cpp" #include "binarytreevec.cpp"

View File

@ -11,7 +11,7 @@
int main() { int main() {
std::cout << "Lasd Libraries 2020" << std::endl; std::cout << "Lasd Libraries 2020" << std::endl;
//menu(); menu();
lasdtest(); // To call in the menu of your library test! lasdtest(); // To call in the menu of your library test!
return 0; return 0;
} }

View File

@ -17,10 +17,27 @@
#include"../binarytree/vec/binarytreevec.hpp" #include"../binarytree/vec/binarytreevec.hpp"
#include<iostream> #include<iostream>
using namespace std; using namespace std;
using namespace lasd;
/* ************************************************************************** */ /* ************************************************************************** */
void menu(){ void menu(){
std::cout<<"MYTESTS\n"; cout<<"MY TESTS\n";
Vector<string> vec(10);
vec[0] = "A";
vec[1] = "B";
vec[2] = "C";
vec[3] = "D";
vec[4] = "E";
vec[5] = "F";
vec[6] = "G";
vec[7] = "H";
vec[8] = "I";
vec[9] = "L";
BinaryTreeLnk<string> bt1(vec);
BinaryTreeLnk<string> bt2(vec);
// if(bt1 == bt2) cout<<"uguali";
} }