diff --git a/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp index a14fe11..5b49da9 100755 --- a/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp @@ -143,7 +143,8 @@ BinaryTreeLnk& BinaryTreeLnk::operator=(BinaryTreeLnk&& tree) template bool BinaryTreeLnk::operator==(const BinaryTreeLnk& tree) const noexcept{ if(size == tree.size){ - return (Root() == tree.Root()); + if(size == 0) return true; + else return (Root() == tree.Root()); }else{ return false; } diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index b7159c4..34d3633 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -1,6 +1,13 @@ namespace lasd { +template +BST::BST(const LinearContainer& lc){ + for(ulong i=0 ; i BST::BST(const BST& bst) : BinaryTreeLnk(bst){} @@ -26,4 +33,150 @@ BST& BST::operator=(BST&& bst) noexcept{ return *this; } +template +bool BST::operator==(const BST& bst) const noexcept{ + if(size != bst.Size()) return false; + + BTInOrderIterator itr1(*this); + BTInOrderIterator itr2(bst); + + for(; !itr1.Terminated() ; ++itr1, ++itr2){ + if(*itr1 != *itr2) return false; + } + return true; +} + +template +bool BST::operator!=(const BST& bst) const noexcept{ + return !(*this = bst) +} + +template +void BST::Insert(const Data& data) noexcept{ + NodeLnk*& pointer = FindPointerTo(root, data); + if(pointer == nullptr){ + pointer = CreateNode(data); + size++; + } +} + +template +void BST::Insert(Data&& data) noexcept{ + NodeLnk*& pointer = FindPointerTo(root, data); + if(pointer == nullptr){ + pointer = new NodeLnk(); + std::swap(pointer->value, data); + + size++; + } +} + +template +void BST::Remove(const Data& data) noexcept{ + delete Detach(FindPointerTo(root,data)); +} + +template +const Data& BST::Min() const{ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return FindPointerToMin(root)->Element(); +} + +template +Data BST::MinNRemove(){ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return DataNDelete(DetachMin(root)); +} + +template +void BST::RemoveMin(){ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return DetachMin(root); +} + + +template +const Data& BST::Max() const{ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return FindPointerToMax(root)->Element(); +} + +template +Data BST::MaxNRemove(){ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return DataNDelete(DetachMax(root)); +} + +template +void BST::RemoveMax(){ + if(root == nullptr) throw std::length_error("Empty tree!"); + + return DetachMax(root); +} + +template +const Data& BST::Predecessor(const Data& data) const{ + NodeLnk* const* ptr = FindPointerToPredecessor(root, data) + if(ptr!=nullptr){ + return (*(*ptr)).Element(); + }else{ + throw std::length_error("Predecessor not found!"); + } +} + +template +Data BST::PredecessorNRemove(const Data& data){ + NodeLnk** ptr = FindPointerToPredecessor(root,data); + if(ptr!=nullptr){ + return DataNDelete(Detach(*ptr)); + }else{ + throw std::length_error("Predecessor not found!"); + } +} + +template +void BST::RemovePredecessor(const Data& data){ + NodeLnk** ptr = FindPointerToPredecessor(root,data); + if(ptr!=nullptr){ + delete Detach(*ptr); + }else{ + throw std::length_error("Predecessor not found!"); + } +} + +template +const Data& BST::Successor(const Data& data) const{ + NodeLnk* const* ptr = FindPointerToSuccessor(root, data) + if(ptr!=nullptr){ + return (*(*ptr)).Element(); + }else{ + throw std::length_error("Successor not found!"); + } +} + +template +Data BST::SuccessorNRemove(const Data& data){ + NodeLnk** ptr = FindPointerToSuccessor(root,data); + if(ptr!=nullptr){ + return DataNDelete(Detach(*ptr)); + }else{ + throw std::length_error("Successor not found!"); + } +} + +template +void BST::RemoveSuccessor(const Data& data){ + NodeLnk** ptr = FindPointerToSuccessor(root,data); + if(ptr!=nullptr){ + delete Detach(*ptr); + }else{ + throw std::length_error("Successor not found!"); + } +} + } diff --git a/librerie/exercise4/bst/bst.hpp b/librerie/exercise4/bst/bst.hpp index d068a91..3b6ac0b 100755 --- a/librerie/exercise4/bst/bst.hpp +++ b/librerie/exercise4/bst/bst.hpp @@ -29,7 +29,7 @@ public: /* ************************************************************************ */ // Specific constructors - // BST(argument) specifiers; // A bst obtained from a LinearContainer + BST(const LinearContainer&); // A bst obtained from a LinearContainer /* ************************************************************************ */ @@ -42,7 +42,7 @@ public: /* ************************************************************************ */ // Destructor - ~BST(); + virtual ~BST(); /* ************************************************************************ */ @@ -55,60 +55,66 @@ public: /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const BST&) const noexcept; + bool operator!=(const BST&) const noexcept; /* ************************************************************************ */ // Specific member functions - // type Insert(argument) specifiers; // Copy of the value - // type Insert(argument) specifiers; // Move of the value - // type Remove(argument) specifiers; + void Insert(const Data&) noexcept; // Copy of the value + void Insert(Data&&) noexcept; // Move of the value + void Remove(const Data&) noexcept; - // type Min(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type MinNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type RemoveMin(argument) specifiers; // (concrete function must throw std::length_error when empty) + 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) - // type Max(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type MaxNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type RemoveMax(argument) specifiers; // (concrete function must throw std::length_error when empty) + 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) - // type Predecessor(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type PredecessorNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type RemovePredecessor(argument) specifiers; // (concrete function must throw std::length_error when empty) + 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) - // type Successor(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type SuccessorNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) - // type RemoveSuccessor(argument) specifiers; // (concrete function must throw std::length_error when empty) + 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) /* ************************************************************************ */ // Specific member functions (inherited from TestableContainer) - // type Exists(argument) specifiers; // Override TestableContainer member + bool Exists(const Data&) const noexcept override; // Override TestableContainer member protected: // Auxiliary member functions - // type DataNDelete(argument) specifiers; + Data DataNDelete(NodeLnk*); - // type Detach(argument) specifiers; + NodeLnk* Detach(NodeLnk*&) noexcept; - // type DetachMin(argument) specifiers; - // type DetachMax(argument) specifiers; + NodeLnk* DetachMin(NodeLnk*&) noexcept; + NodeLnk* DetachMax(NodeLnk*&) noexcept; - // type SkipOnLeft(argument) specifiers; - // type SkipOnRight(argument) specifiers; + NodeLnk* SkipOnLeft(NodeLnk*&) noexcept; + NodeLnk* SkipOnRight(NodeLnk*&) noexcept; - // type FindPointerToMin(argument) specifiers; - // type FindPointerToMax(argument) specifiers; + NodeLnk* const& FindPointerToMin(NodeLnk* const&) const noexcept; + NodeLnk*& FindPointerToMin(NodeLnk*&) noexcept; + NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept; + NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept; - // type FindPointerTo(argument) specifiers; + NodeLnk* const& FindPointerTo(NodeLnk* const&) const noexcept; + NodeLnk*& FindPointerTo(NodeLnk*&) noexcept; - // type FindPointerToPredecessor(argument) specifiers; - // type FindPointerToSuccessor(argument) specifiers; + NodeLnk* const& FindPointerToPredecessor(NodeLnk* const&) const noexcept; + NodeLnk*& FindPointerToPredecessor(NodeLnk*&) noexcept; + + NodeLnk* const& FindPointerToSuccessor(NodeLnk* const&) const noexcept; + NodeLnk*& FindPointerToSuccessor(NodeLnk*&) noexcept; };