lasd/librerie/exercise4/bst/bst.hpp

128 lines
4.1 KiB
C++
Raw Normal View History

2021-05-11 06:51:06 +02:00
#ifndef BST_HPP
#define BST_HPP
/* ************************************************************************** */
#include "../binarytree/lnk/binarytreelnk.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename Data>
2021-05-11 16:28:01 +02:00
class BST : virtual public BinaryTreeLnk<Data> { // Must extend BinaryTreeLnk<Data>
2021-05-11 06:51:06 +02:00
protected:
2021-05-11 16:28:01 +02:00
using BinaryTreeLnk<Data>::size;
using BinaryTreeLnk<Data>::root;
using typename BinaryTreeLnk<Data>::NodeLnk;
2021-05-11 06:51:06 +02:00
public:
2021-05-11 16:28:01 +02:00
BST() = default;
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Specific constructors
2021-05-12 08:43:39 +02:00
BST(const LinearContainer<Data>&); // A bst obtained from a LinearContainer
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Copy constructor
2021-05-11 16:28:01 +02:00
BST(const BST<Data>&);
2021-05-11 06:51:06 +02:00
// Move constructor
2021-05-11 16:28:01 +02:00
BST(BST<Data>&&) noexcept;
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Destructor
2021-05-12 08:43:39 +02:00
virtual ~BST();
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Copy assignment
2021-05-11 16:28:01 +02:00
BST& operator=(const BST<Data>&);
2021-05-11 06:51:06 +02:00
// Move assignment
2021-05-11 16:28:01 +02:00
BST& operator=(BST<Data>&&) noexcept;
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Comparison operators
2021-05-12 08:43:39 +02:00
bool operator==(const BST<Data>&) const noexcept;
bool operator!=(const BST<Data>&) const noexcept;
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Specific member functions
2021-05-12 08:43:39 +02:00
void Insert(const Data&) noexcept; // Copy of the value
void Insert(Data&&) noexcept; // Move of the value
void Remove(const Data&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
const Data& Min() const; // (concrete function must throw std::length_error when empty)
Data MinNRemove(); // (concrete function must throw std::length_error when empty)
void RemoveMin(); // (concrete function must throw std::length_error when empty)
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
const Data& Max() const; // (concrete function must throw std::length_error when empty)
Data MaxNRemove(); // (concrete function must throw std::length_error when empty)
void RemoveMax(); // (concrete function must throw std::length_error when empty)
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
const Data& Predecessor(const Data&) const; // (concrete function must throw std::length_error when empty)
Data PredecessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
void RemovePredecessor(const Data&); // (concrete function must throw std::length_error when empty)
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
const Data& Successor(const Data&) const; // (concrete function must throw std::length_error when empty)
Data SuccessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
void RemoveSuccessor(const Data&); // (concrete function must throw std::length_error when empty)
2021-05-11 06:51:06 +02:00
/* ************************************************************************ */
// Specific member functions (inherited from TestableContainer)
2021-05-12 08:43:39 +02:00
bool Exists(const Data&) const noexcept override; // Override TestableContainer member
2021-05-11 06:51:06 +02:00
protected:
// Auxiliary member functions
2021-05-12 08:43:39 +02:00
Data DataNDelete(NodeLnk*);
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* Detach(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* DetachMin(NodeLnk*&) noexcept;
NodeLnk* DetachMax(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* SkipOnLeft(NodeLnk*&) noexcept;
NodeLnk* SkipOnRight(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* const& FindPointerToMin(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToMin(NodeLnk*&) noexcept;
NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* const& FindPointerTo(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerTo(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
2021-05-12 08:43:39 +02:00
NodeLnk* const& FindPointerToPredecessor(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToPredecessor(NodeLnk*&) noexcept;
NodeLnk* const& FindPointerToSuccessor(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToSuccessor(NodeLnk*&) noexcept;
2021-05-11 06:51:06 +02:00
};
/* ************************************************************************** */
}
#include "bst.cpp"
#endif