Library 3

binarytreevec
This commit is contained in:
Alessandro Ferro 2021-05-01 23:58:12 +02:00
parent 27726938dd
commit 10c5148efc
37 changed files with 200 additions and 43 deletions

0
librerie/exercise3/Exercise3.pdf Normal file → Executable file
View File

0
librerie/exercise3/binarytree/binarytree.cpp Normal file → Executable file
View File

0
librerie/exercise3/binarytree/binarytree.hpp Normal file → Executable file
View File

15
librerie/exercise3/binarytree/lnk/binarytreelnk.cpp Normal file → Executable file
View File

@ -34,20 +34,15 @@ bool BinaryTreeLnk<Data>::NodeLnk::HasRightChild() const noexcept{
}
template <typename Data>
NodeLnk& BinaryTreeLnk<Data>::NodeLnk::LeftChild() const{
Node& BinaryTreeLnk<Data>::NodeLnk::LeftChild() const{
return *left;
}
template <typename Data>
NodeLnk& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
Node& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
return *right;
}
template <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(){
size = 0;
}
// creates a tree from a linear container in breadth
template <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(const LinearContainer<Data>& lc){
@ -85,15 +80,17 @@ BinaryTreeLnk<Data>::BinaryTreeLnk(const BinaryTreeLnk<Data>& tree){
}
BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CopyTree(struct BinaryTreeLnk<Data>::NodeLnk* nodeToCopy){
if(nodeToCopy==nullptr) return nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(nodeToCopy.Element());
if(nodeToCopy.HasLeftChild())
tmp->left = copyTrees(&(nodeToCopy.LeftChild()));
tmp->left = copyTree(&(nodeToCopy.LeftChild()));
else
tmp->left = nullptr;
if(nodeToCopy.HasRightChild())
tmp->right = copyTrees(&(nodeToCopy.RightChild()));
tmp->right = copyTree(&(nodeToCopy.RightChild()));
else
tmp->right = nullptr;

14
librerie/exercise3/binarytree/lnk/binarytreelnk.hpp Normal file → Executable file
View File

@ -31,13 +31,11 @@ protected:
using BinaryTree<Data>::Node::data;
struct NodeLnk* left;
struct* NodeLnk* right;
struct NodeLnk* right;
public:
NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible.
// Move assignment
Node& operator=(Node&&) noexcept override; // Move assignment of abstract types should not be possible.
Node& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible.
Node& operator=(NodeLnk&&) noexcept override; // Move assignment of abstract types should not be possible.
bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)
bool HasLeftChild() const noexcept override; // (concrete function should not throw exceptions)
bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions)
@ -52,7 +50,7 @@ protected:
public:
// Default constructor
BinaryTreeLnk();
BinaryTreeLnk() = default;
/* ************************************************************************ */
@ -75,10 +73,10 @@ public:
/* ************************************************************************ */
// Copy assignment
BinaryTreeLnk<Data>& operator=(const BinaryTreeLnk<Data>&);
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
// Move assignment
BinaryTreeLnk<Data> operator=(BinaryTreeLnk<Data>&&) noexcept;
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept;
/* ************************************************************************ */

157
librerie/exercise3/binarytree/vec/binarytreevec.cpp Normal file → Executable file
View File

@ -3,7 +3,162 @@ namespace lasd {
/* ************************************************************************** */
// ...
template <typename Data>
struct BinaryTree<Data>:Node& BinaryTreeVec<Data>::NodeVec::operator=(const BinaryTreeVec<Data>::NodeVec& node){
ReferenceToTree = node.ReferenceToTree;
data = node.data;
index = node.index;
return *this;
}
template <typename Data>
struct BinaryTree<Data>:Node& BinaryTreeVec<Data>::NodeVec::operator=(BinaryTreeVec<Data>::NodeVec&& node) noexcept{
std::swap(data, node.data);
std::swap(index, node.index);
std::swap(ReferenceToTree, node.ReferenceToTree);
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::IsLeaf() const noexcept{
return (!HasLeftChild() && !HasRightChild());
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::HasLeftChild() const noexcept{
if(index*2+1 < ReferenceToTree->size){
return true;
}else{
return false;
}
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::HasRightChild() const noexcept{
if(index*2+2 < ReferenceToTree->size){
return true;
}else{
return false;
}
}
template <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::NodeVec::LeftChild() const{
if(index*2+1 < ReferenceToTree->size)
return *((ReferenceToTree->tree)[index*2+1]);
else
throw std::out_of_range("Left child does not exist!");
}
template <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::NodeVec::RightChild() const{
if(index*2+2 < ReferenceToTree->size)
return *((ReferenceToTree->tree)[index*2+2]);
else
throw std::out_of_range("Right child does not exist!");
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(const LinearContainer<Data>& lc){
tree.Resize(lc.Size());
size = lc.Size();
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = lc[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(const BinaryTreeVec<Data>& bt){
size = bt.size;
tree.Resize(size);
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = bt[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(BinaryTreeVec<Data>&& bt) noexcept{
std::swap(size,bt.size);
std::swap(tree,bt.tree);
}
template <typename Data>
BinaryTreeVec<Data>::~BinaryTreeVec(){
Clear();
}
template <typename Data>
bool BinaryTreeVec<Data>::operator=(const BinaryTreeVec<Data>& bt){
size = bt.size;
tree.Resize(size);
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = bt[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator=(BinaryTreeVec<Data>&& bt) noexcept{
std::swap(size, bt.size);
std::swap(tree, bt.tree);
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator==(const BinaryTreeVec& bt) const noexcept{
if(size==bt.size){
for(ulong i=0 ; i<size ; ++i){
if((tree[i])->data != ((bt.tree)[i])->data ) return false;
}
return true;
}
return false;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator!=(const BinaryTreeVec& bt) const noexcept{
return !(*this == bt);
}
template <typename Data>
struct BinaryTreeVec<Data>::Node& BinaryTreeVec<Data>::Root(){
return *(tree.Front());
}
template <typename Data>
void BinaryTreeVec<Data>::Clear(){
for(ulong i=0 ; i<size ; ++i){
delete tree[i];
tree[i] = nullptr
}
size = 0;
}
template <typename Data>
void BinaryTreeVec<Data>::MapBreadth(const MapFunctor func, void* par) override{
for(ulong i=0 ; i<size ; ++i){
func( *(tree[i]), par);
}
}
template <typename Data>
void BinaryTreeVec<Data>::FoldBreadth(const MapFunctor func, const void* par, void* acc) override{
for(ulong i=0 ; i<size ; ++i){
func( *(tree[i]), par, acc);
}
}
/* ************************************************************************** */

57
librerie/exercise3/binarytree/vec/binarytreevec.hpp Normal file → Executable file
View File

@ -14,82 +14,89 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BinaryTreeVec { // Must extend BinaryTree<Data>
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
private:
// ...
protected:
// using BinaryTree<Data>::???;
using BinaryTree<Data>::size;
// using BinaryTree<Data>::Node;
Vector<struct NodeVec*> tree;
// ...
struct NodeVec { // Must extend Node
struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node
private:
// ...
protected:
// ...
using BinaryTree<Data>::Node::data;
ulong index;
BinaryTreeVec<Data>* ReferenceToTree = nullptr;
public:
// ...
struct BinaryTree<Data>::Node& operator=(const NodeVec&) override; // Copy assignment of abstract types should not be possible.
struct BinaryTree<Data>::Node& operator=(NodeVec&&) noexcept override; // Move assignment of abstract types should not be possible.
bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)
bool HasLeftChild() const noexcept override; // (concrete function should not throw exceptions)
bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions)
struct BinaryTree<Data>::Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent)
struct BinaryTree<Data>::Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent)
};
public:
// Default constructor
// BinaryTreeVec() specifiers;
BinaryTreeVec() = default;
/* ************************************************************************ */
// Specific constructors
// BinaryTreeVec(argument) specifiers; // A binary tree obtained from a LinearContainer
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
// BinaryTreeVec(argument) specifiers;
BinaryTreeVec(const BinaryTreeVec<Data>&);
// Move constructor
// BinaryTreeVec(argument) specifiers;
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
// Destructor
// ~BinaryTreeVec() specifiers;
virtual ~BinaryTreeVec();
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
// Move assignment
// type operator=(argument) specifiers;
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
// Comparison operators
// type operator==(argument) specifiers;
// type operator!=(argument) specifiers;
bool operator==(const BinaryTreeVec&) const noexcept;
bool operator!=(const BinaryTreeVec&) const noexcept;
/* ************************************************************************ */
// Specific member functions (inherited from BinaryTree)
// type Root() specifiers; // Override BinaryTree member (throw std::length_error when empty)
struct BinaryTreeVec<Data>::NodeVec& Root() override; // Override BinaryTree member (throw std::length_error when empty)
/* ************************************************************************ */
// Specific member functions (inherited from Container)
// type Clear() specifiers; // Override Container member
void Clear() override; // Override Container member
/* ----- override of map and fold in breadth ----- */
using typename BinaryTree<Data>::MapFunctor;
using typename BinaryTree<Data>::FoldFunctor;
void MapBreadth(const MapFunctor, void*) override;
void FoldBreadth(const FoldFunctor, const void*, void*) const override;
};

0
librerie/exercise3/build.sh Normal file → Executable file
View File

0
librerie/exercise3/container/container.cpp Normal file → Executable file
View File

0
librerie/exercise3/container/container.hpp Normal file → Executable file
View File

0
librerie/exercise3/iterator/iterator.hpp Normal file → Executable file
View File

0
librerie/exercise3/list/list.cpp Normal file → Executable file
View File

0
librerie/exercise3/list/list.hpp Normal file → Executable file
View File

0
librerie/exercise3/main.cpp Normal file → Executable file
View File

0
librerie/exercise3/vector/vector.cpp Normal file → Executable file
View File

0
librerie/exercise3/vector/vector.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/binarytree/binarytree.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/container/container.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/container/container.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise1/fulltest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise1/simpletest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise1/test.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise2/fulltest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise2/simpletest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise2/test.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise3/fulltest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise3/simpletest.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/exercise3/test.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/iterator/iterator.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/list/list.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/queue/queue.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/stack/stack.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/test.cpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/test.hpp Normal file → Executable file
View File

0
librerie/exercise3/zlasdtest/vector/vector.hpp Normal file → Executable file
View File

0
librerie/exercise3/zmytest/test.cpp Normal file → Executable file
View File

0
librerie/exercise3/zmytest/test.hpp Normal file → Executable file
View File