mirror of https://github.com/xfarrow/lasd.git
parent
65942205cd
commit
27726938dd
|
@ -2,9 +2,158 @@
|
||||||
namespace lasd {
|
namespace lasd {
|
||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
template <typename Data>
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const NodeLnk& node){
|
||||||
|
data = node.data;
|
||||||
|
left = node.left;
|
||||||
|
right = node.right;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
// ...
|
template <typename Data>
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(NodeLnk&& node) noexcept{
|
||||||
|
std::swap(data, node.data);
|
||||||
|
std::swap(left, node.left);
|
||||||
|
std::swap(right, node.right);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************** */
|
template <typename Data>
|
||||||
|
bool BinaryTreeLnk<Data>::NodeLnk::IsLeaf() const noexcept{
|
||||||
|
return (left==nullptr && right==nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
bool BinaryTreeLnk<Data>::NodeLnk::HasLeftChild() const noexcept{
|
||||||
|
return (left!=nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
bool BinaryTreeLnk<Data>::NodeLnk::HasRightChild() const noexcept{
|
||||||
|
return (right!=nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
NodeLnk& BinaryTreeLnk<Data>::NodeLnk::LeftChild() const{
|
||||||
|
return *left;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
NodeLnk& 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){
|
||||||
|
root = CreateTreeFromLinearContainerInBreadth(lc,0);
|
||||||
|
size = lc.Size();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>& lc, ulong position){
|
||||||
|
if(position >= lc.Size()) return nullptr;
|
||||||
|
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]);
|
||||||
|
// if(position == 0) root = tmp;
|
||||||
|
|
||||||
|
tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1);
|
||||||
|
tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2);
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data){
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* newNode = new struct BinaryTreeLnk<Data>::NodeLnk();
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->left = nullptr;
|
||||||
|
newNode->right = nullptr;
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
BinaryTreeLnk<Data>::BinaryTreeLnk(const BinaryTreeLnk<Data>& tree){
|
||||||
|
if(tree.root == nullptr) return;
|
||||||
|
size = tree.size;
|
||||||
|
root = CopyTree(&tree.Root());
|
||||||
|
}
|
||||||
|
|
||||||
|
BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CopyTree(struct BinaryTreeLnk<Data>::NodeLnk* nodeToCopy){
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(nodeToCopy.Element());
|
||||||
|
|
||||||
|
if(nodeToCopy.HasLeftChild())
|
||||||
|
tmp->left = copyTrees(&(nodeToCopy.LeftChild()));
|
||||||
|
else
|
||||||
|
tmp->left = nullptr;
|
||||||
|
|
||||||
|
if(nodeToCopy.HasRightChild())
|
||||||
|
tmp->right = copyTrees(&(nodeToCopy.RightChild()));
|
||||||
|
else
|
||||||
|
tmp->right = nullptr;
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
BinaryTreeLnk<Data>::BinaryTreeLnk(BinaryTreeLnk<Data>&& tree) noexcept{
|
||||||
|
std::swap(size, tree.size);
|
||||||
|
std::swap(root, tree.root);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
BinaryTreeLnk<Data>::~BinaryTreeLnk(){
|
||||||
|
Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
void BinaryTreeLnk<Data>::DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node){
|
||||||
|
if(node == nullptr) return;
|
||||||
|
DeleteTree(node->left);
|
||||||
|
DeleteTree(node->right);
|
||||||
|
delete node;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(const BinaryTreeLnk<Data>& tree){
|
||||||
|
Clear();
|
||||||
|
size = tree.size;
|
||||||
|
if(tree.root != nullptr)
|
||||||
|
root = CopyTree(&tree.Root());
|
||||||
|
return *this;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree) noexcept{
|
||||||
|
std::swap(size, tree.size);
|
||||||
|
std::swap(root, tree.root);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
bool BinaryTreeLnk<Data>::operator==(const BinaryTreeLnk<Data>& tree) const noexcept{
|
||||||
|
return (Root() == tree.Root());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
bool BinaryTreeLnk<Data>::operator!=(const BinaryTreeLnk<Data>& tree) const noexcept{
|
||||||
|
return !(*this == tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
struct NodeLnk& Root() override{
|
||||||
|
return *root;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Data>
|
||||||
|
void Clear() override{
|
||||||
|
DeleteTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,9 @@ private:
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
using BinaryTree<Data>::size;
|
using BinaryTree<Data>::size;
|
||||||
|
struct NodeLnk* root = nullptr;
|
||||||
|
|
||||||
|
// Node
|
||||||
struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node
|
struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -32,11 +34,10 @@ protected:
|
||||||
struct* NodeLnk* right;
|
struct* NodeLnk* right;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible.
|
||||||
NodeLnk& operator=(const Node&); // Copy assignment of abstract types should not be possible.
|
|
||||||
|
|
||||||
// Move assignment
|
// Move assignment
|
||||||
NodeLnk& operator=(Node&&) noexcept override; // Move assignment of abstract types should not be possible.
|
Node& operator=(Node&&) noexcept override; // Move assignment of abstract types should not be possible.
|
||||||
bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)
|
bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)
|
||||||
bool HasLeftChild() 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)
|
bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions)
|
||||||
|
@ -44,6 +45,8 @@ protected:
|
||||||
Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent)
|
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)
|
Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent)
|
||||||
|
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -54,47 +57,50 @@ public:
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Specific constructors
|
// Specific constructors
|
||||||
BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer
|
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Copy constructor
|
// Copy constructor
|
||||||
// BinaryTreeLnk(argument) specifiers;
|
BinaryTreeLnk(const BinaryTreeLnk<Data>&);
|
||||||
|
|
||||||
// Move constructor
|
// Move constructor
|
||||||
// BinaryTreeLnk(argument) specifiers;
|
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
// ~BinaryTreeLnk() specifiers;
|
virtual ~BinaryTreeLnk();
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Copy assignment
|
// Copy assignment
|
||||||
// type operator=(argument) specifiers;
|
BinaryTreeLnk<Data>& operator=(const BinaryTreeLnk<Data>&);
|
||||||
|
|
||||||
// Move assignment
|
// Move assignment
|
||||||
// type operator=(argument) specifiers;
|
BinaryTreeLnk<Data> operator=(BinaryTreeLnk<Data>&&) noexcept;
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Comparison operators
|
// Comparison operators
|
||||||
// type operator==(argument) specifiers;
|
bool operator==(const BinaryTreeLnk<Data>&) const noexcept;
|
||||||
// type operator!=(argument) specifiers;
|
bool operator!=(const BinaryTreeLnk<Data>&) const noexcept;
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Specific member functions (inherited from BinaryTree)
|
// Specific member functions (inherited from BinaryTree)
|
||||||
|
|
||||||
// type Root() specifiers; // Override BinaryTree member (throw std::length_error when empty)
|
struct NodeLnk& Root() override; // Override BinaryTree member (throw std::length_error when empty)
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
|
|
||||||
// Specific member functions (inherited from Container)
|
// Specific member functions (inherited from Container)
|
||||||
|
|
||||||
// type Clear() specifiers; // Override Container member
|
void Clear() override; // Override Container member
|
||||||
|
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong);
|
||||||
|
struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::NodeLnk*);
|
||||||
|
void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node)
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
Loading…
Reference in New Issue