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

172 lines
4.4 KiB
C++
Raw Normal View History

2021-04-24 16:58:05 +02:00
namespace lasd {
2021-05-05 22:37:45 +02:00
/* ----- begin of struct NodeLnk ----- */
2021-04-30 18:12:50 +02:00
template <typename Data>
2021-05-04 16:05:48 +02:00
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const BinaryTreeLnk<Data>::NodeLnk& node){
2021-04-30 18:12:50 +02:00
data = node.data;
left = node.left;
right = node.right;
return *this;
}
2021-04-24 16:58:05 +02:00
2021-04-30 18:12:50 +02:00
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;
}
2021-04-24 16:58:05 +02:00
2021-04-30 18:12:50 +02:00
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>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::LeftChild() const{
2021-04-30 18:12:50 +02:00
return *left;
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
2021-04-30 18:12:50 +02:00
return *right;
}
2021-05-05 22:37:45 +02:00
/* ----- end of struct NodeLnk ----- */
/* ----- begin of class BinaryTreeLnk ----- */
2021-04-30 18:12:50 +02:00
// 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();
}
2021-05-04 16:05:48 +02:00
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>& lc, ulong position){
2021-04-30 18:12:50 +02:00
if(position >= lc.Size()) return nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]);
tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1);
tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2);
return tmp;
}
template <typename Data>
2021-05-04 16:05:48 +02:00
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateNode(const Data& data){
2021-04-30 18:12:50 +02:00
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());
}
2021-05-04 16:05:48 +02:00
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CopyTree(struct BinaryTreeLnk<Data>::Node* nodeToCopy){
2021-05-01 23:58:12 +02:00
if(nodeToCopy==nullptr) return nullptr;
2021-05-04 16:05:48 +02:00
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(nodeToCopy->Element());
2021-04-30 18:12:50 +02:00
2021-05-04 16:05:48 +02:00
if(nodeToCopy->HasLeftChild())
tmp->left = CopyTree(&(nodeToCopy->LeftChild()));
2021-04-30 18:12:50 +02:00
else
tmp->left = nullptr;
2021-05-04 16:05:48 +02:00
if(nodeToCopy->HasRightChild())
tmp->right = CopyTree(&(nodeToCopy->RightChild()));
2021-04-30 18:12:50 +02:00
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{
2021-05-05 22:37:45 +02:00
Clear();
2021-04-30 18:12:50 +02:00
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{
if(size == tree.size){
2021-05-11 07:00:36 +02:00
if(size == 0) return true;
else return (Root() == tree.Root());
}else{
return false;
}
2021-04-30 18:12:50 +02:00
}
template <typename Data>
bool BinaryTreeLnk<Data>::operator!=(const BinaryTreeLnk<Data>& tree) const noexcept{
return !(*this == tree);
}
template <typename Data>
2021-05-06 19:46:16 +02:00
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::Root() const{
2021-05-05 10:03:25 +02:00
if(size==0) throw std::length_error("Empty tree!");
2021-04-30 18:12:50 +02:00
return *root;
}
template <typename Data>
2021-05-04 16:05:48 +02:00
void BinaryTreeLnk<Data>::Clear(){
2021-04-30 18:12:50 +02:00
DeleteTree(root);
2021-05-05 10:03:25 +02:00
root = nullptr;
size = 0;
2021-04-30 18:12:50 +02:00
}
2021-04-24 16:58:05 +02:00
}