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

113 lines
3.4 KiB
C++
Raw Normal View History

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