Library 4

Added more functions
This commit is contained in:
Alessandro Ferro 2021-05-12 08:43:39 +02:00
parent 599433ced3
commit 5aaddb0051
3 changed files with 192 additions and 32 deletions

View File

@ -143,7 +143,8 @@ BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree)
template <typename Data> template <typename Data>
bool BinaryTreeLnk<Data>::operator==(const BinaryTreeLnk<Data>& tree) const noexcept{ bool BinaryTreeLnk<Data>::operator==(const BinaryTreeLnk<Data>& tree) const noexcept{
if(size == tree.size){ if(size == tree.size){
return (Root() == tree.Root()); if(size == 0) return true;
else return (Root() == tree.Root());
}else{ }else{
return false; return false;
} }

View File

@ -1,6 +1,13 @@
namespace lasd { namespace lasd {
template <typename Data>
BST<Data>::BST(const LinearContainer<Data>& lc){
for(ulong i=0 ; i<lc.Size() ; ++i){
Insert(lc[i]);
}
}
template <typename Data> template <typename Data>
BST<Data>::BST(const BST<Data>& bst) BST<Data>::BST(const BST<Data>& bst)
: BinaryTreeLnk<Data>(bst){} : BinaryTreeLnk<Data>(bst){}
@ -26,4 +33,150 @@ BST<Data>& BST<Data>::operator=(BST<Data>&& bst) noexcept{
return *this; return *this;
} }
template <typename Data>
bool BST<Data>::operator==(const BST<Data>& bst) const noexcept{
if(size != bst.Size()) return false;
BTInOrderIterator<Data> itr1(*this);
BTInOrderIterator<Data> itr2(bst);
for(; !itr1.Terminated() ; ++itr1, ++itr2){
if(*itr1 != *itr2) return false;
}
return true;
}
template <typename Data>
bool BST<Data>::operator!=(const BST<Data>& bst) const noexcept{
return !(*this = bst)
}
template <typename Data>
void BST<Data>::Insert(const Data& data) noexcept{
NodeLnk*& pointer = FindPointerTo(root, data);
if(pointer == nullptr){
pointer = CreateNode(data);
size++;
}
}
template <typename Data>
void BST<Data>::Insert(Data&& data) noexcept{
NodeLnk*& pointer = FindPointerTo(root, data);
if(pointer == nullptr){
pointer = new NodeLnk();
std::swap(pointer->value, data);
size++;
}
}
template <typename Data>
void BST<Data>::Remove(const Data& data) noexcept{
delete Detach(FindPointerTo(root,data));
}
template <typename Data>
const Data& BST<Data>::Min() const{
if(root == nullptr) throw std::length_error("Empty tree!");
return FindPointerToMin(root)->Element();
}
template <typename Data>
Data BST<Data>::MinNRemove(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DataNDelete(DetachMin(root));
}
template <typename Data>
void BST<Data>::RemoveMin(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DetachMin(root);
}
template <typename Data>
const Data& BST<Data>::Max() const{
if(root == nullptr) throw std::length_error("Empty tree!");
return FindPointerToMax(root)->Element();
}
template <typename Data>
Data BST<Data>::MaxNRemove(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DataNDelete(DetachMax(root));
}
template <typename Data>
void BST<Data>::RemoveMax(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DetachMax(root);
}
template <typename Data>
const Data& BST<Data>::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 <typename Data>
Data BST<Data>::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 <typename Data>
void BST<Data>::RemovePredecessor(const Data& data){
NodeLnk** ptr = FindPointerToPredecessor(root,data);
if(ptr!=nullptr){
delete Detach(*ptr);
}else{
throw std::length_error("Predecessor not found!");
}
}
template <typename Data>
const Data& BST<Data>::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 <typename Data>
Data BST<Data>::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 <typename Data>
void BST<Data>::RemoveSuccessor(const Data& data){
NodeLnk** ptr = FindPointerToSuccessor(root,data);
if(ptr!=nullptr){
delete Detach(*ptr);
}else{
throw std::length_error("Successor not found!");
}
}
} }

View File

@ -29,7 +29,7 @@ public:
/* ************************************************************************ */ /* ************************************************************************ */
// Specific constructors // Specific constructors
// BST(argument) specifiers; // A bst obtained from a LinearContainer BST(const LinearContainer<Data>&); // A bst obtained from a LinearContainer
/* ************************************************************************ */ /* ************************************************************************ */
@ -42,7 +42,7 @@ public:
/* ************************************************************************ */ /* ************************************************************************ */
// Destructor // Destructor
~BST(); virtual ~BST();
/* ************************************************************************ */ /* ************************************************************************ */
@ -55,60 +55,66 @@ public:
/* ************************************************************************ */ /* ************************************************************************ */
// Comparison operators // Comparison operators
// type operator==(argument) specifiers; bool operator==(const BST<Data>&) const noexcept;
// type operator!=(argument) specifiers; bool operator!=(const BST<Data>&) const noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions // Specific member functions
// type Insert(argument) specifiers; // Copy of the value void Insert(const Data&) noexcept; // Copy of the value
// type Insert(argument) specifiers; // Move of the value void Insert(Data&&) noexcept; // Move of the value
// type Remove(argument) specifiers; void Remove(const Data&) noexcept;
// type Min(argument) specifiers; // (concrete function must throw std::length_error when empty) const Data& Min() const; // (concrete function must throw std::length_error when empty)
// type MinNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) Data MinNRemove(); // (concrete function must throw std::length_error when empty)
// type RemoveMin(argument) specifiers; // (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) const Data& Max() const; // (concrete function must throw std::length_error when empty)
// type MaxNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) Data MaxNRemove(); // (concrete function must throw std::length_error when empty)
// type RemoveMax(argument) specifiers; // (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) const Data& Predecessor(const Data&) const; // (concrete function must throw std::length_error when empty)
// type PredecessorNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) Data PredecessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
// type RemovePredecessor(argument) specifiers; // (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) const Data& Successor(const Data&) const; // (concrete function must throw std::length_error when empty)
// type SuccessorNRemove(argument) specifiers; // (concrete function must throw std::length_error when empty) Data SuccessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
// type RemoveSuccessor(argument) specifiers; // (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) // Specific member functions (inherited from TestableContainer)
// type Exists(argument) specifiers; // Override TestableContainer member bool Exists(const Data&) const noexcept override; // Override TestableContainer member
protected: protected:
// Auxiliary member functions // Auxiliary member functions
// type DataNDelete(argument) specifiers; Data DataNDelete(NodeLnk*);
// type Detach(argument) specifiers; NodeLnk* Detach(NodeLnk*&) noexcept;
// type DetachMin(argument) specifiers; NodeLnk* DetachMin(NodeLnk*&) noexcept;
// type DetachMax(argument) specifiers; NodeLnk* DetachMax(NodeLnk*&) noexcept;
// type SkipOnLeft(argument) specifiers; NodeLnk* SkipOnLeft(NodeLnk*&) noexcept;
// type SkipOnRight(argument) specifiers; NodeLnk* SkipOnRight(NodeLnk*&) noexcept;
// type FindPointerToMin(argument) specifiers; NodeLnk* const& FindPointerToMin(NodeLnk* const&) const noexcept;
// type FindPointerToMax(argument) specifiers; 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; NodeLnk* const& FindPointerToPredecessor(NodeLnk* const&) const noexcept;
// type FindPointerToSuccessor(argument) specifiers; NodeLnk*& FindPointerToPredecessor(NodeLnk*&) noexcept;
NodeLnk* const& FindPointerToSuccessor(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToSuccessor(NodeLnk*&) noexcept;
}; };