lasd/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp

77 lines
2.5 KiB
C++
Raw Normal View History

2021-05-11 06:51:06 +02:00
#ifndef BINARYTREELNK_HPP
#define BINARYTREELNK_HPP
#include "../binarytree.hpp"
namespace lasd {
template <typename Data>
2021-05-11 06:52:44 +02:00
class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
2021-05-11 06:51:06 +02:00
protected:
2021-05-11 06:52:44 +02:00
struct NodeLnk : virtual public BinaryTree<Data>::Node { // Must extend Node
2021-05-11 06:51:06 +02:00
protected:
2021-05-11 06:52:44 +02:00
using BinaryTree<Data>::Node::data;
struct NodeLnk* left = nullptr;
struct NodeLnk* right = nullptr;
2021-05-11 06:51:06 +02:00
public:
2021-05-11 06:52:44 +02:00
struct NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible.
struct NodeLnk& operator=(NodeLnk&&) noexcept; // 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)
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
struct NodeLnk& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent)
struct NodeLnk& RightChild() const override; // (concrete function must throw std::out_of_range when not existent)
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
friend class BinaryTreeLnk<Data>;
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
};
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
protected:
using BinaryTree<Data>::size;
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
struct BinaryTreeLnk<Data>::NodeLnk* root = nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data);
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
public:
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
// Default constructor
BinaryTreeLnk() = default;
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
BinaryTreeLnk(const BinaryTreeLnk<Data>&);
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
virtual ~BinaryTreeLnk();
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
bool operator==(const BinaryTreeLnk<Data>&) const noexcept;
bool operator!=(const BinaryTreeLnk<Data>&) const noexcept;
2021-05-11 06:51:06 +02:00
// Specific member functions (inherited from BinaryTree)
2021-05-11 06:52:44 +02:00
NodeLnk& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
2021-05-11 06:51:06 +02:00
// Specific member functions (inherited from Container)
2021-05-11 06:52:44 +02:00
void Clear() override; // Override Container member
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
// Specific functions
2021-05-11 06:51:06 +02:00
2021-05-11 06:52:44 +02:00
struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong);
struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::Node*);
void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node);
};
2021-05-11 06:51:06 +02:00
}
#include "binarytreelnk.cpp"
#endif