Library 4

update
This commit is contained in:
Alessandro Ferro 2021-05-16 22:31:05 +02:00
parent 147730b10b
commit ae5b251099
3 changed files with 48 additions and 41 deletions

View File

@ -93,7 +93,6 @@ void BST<Data>::RemoveMin(){
delete DetachMin(root); delete DetachMin(root);
} }
template <typename Data> template <typename Data>
const Data& BST<Data>::Max() const{ const Data& BST<Data>::Max() const{
if(root == nullptr) throw std::length_error("Empty tree!"); if(root == nullptr) throw std::length_error("Empty tree!");
@ -277,8 +276,6 @@ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMax(struct BST<Data>::Node
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMax(node)); return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMax(node));
} }
/* ---- END ----- */
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
/* /*
@ -304,72 +301,78 @@ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::N
return *pointer; return *pointer;
} }
template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerTo(node, data));
}
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{ typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
/*
If the element we are looking the predecessor for is the current element,
then its predecessor resides in the max node of its left subtree (if it has
a left subtree, return the lastRight otherwise).
If the element we are looking the predecessor for is greater than the current element,
then we have to go down right the tree, saving the current "lastRight".
If the element we are looking the predecessor for is less than the current element,
then we have to go down left the tree,
*/
NodeLnk* const* pointer = &ref; NodeLnk* const* pointer = &ref;
NodeLnk* current = ref; NodeLnk* current = ref;
NodeLnk* const* lastRight = pointer; NodeLnk* const* lastRight = nullptr;
if(ref != nullptr){ while(current != nullptr){
while( current != nullptr){ if(data == current->Element()){
if(data == current->Element()){ if(current->HasLeftChild()){
if(current->HasLeftChild()){ return &(FindPointerToMax(current->left));
return pointer = &(FindPointerToMax(current->left));
}
return lastRight;
}else if(data < current->Element()){
pointer = &current->left;
current = current->left;
}else{ }else{
lastRight = pointer; return lastRight;
pointer = &current->right;
current = current->right;
} }
}else if(current->Element() < data){
lastRight = pointer;
pointer = &(current->right);
current = current->right;
}else if(current->Element() > data){
pointer = &(current->left);
current = current->left;
} }
return lastRight;
}else{
return lastRight;
} }
return lastRight;
} }
template <typename Data>
typename BST<Data>::NodeLnk** BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToPredecessor(node, data));
}
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{ typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
NodeLnk* const* pointer = &ref; NodeLnk* const* pointer = &ref;
NodeLnk* current = ref; NodeLnk* current = ref;
NodeLnk* const* lastLeft = nullptr; NodeLnk* const* lastLeft = nullptr;
if(ref != nullptr){
while( current != nullptr){ while( current != nullptr){
if(data == current->Element()){ if(data == current->Element()){
if(current->HasRightChild()){ if(current->HasRightChild()){
return pointer = &(FindPointerToMin(current->right)); return pointer = &(FindPointerToMin(current->right));
} }
return lastLeft; else{
}else if(data < current->Element()){ return lastLeft;
}
}else if(current->Element() > data){
lastLeft = pointer; lastLeft = pointer;
pointer = &current->left; pointer = &current->left;
current = current->left; current = current->left;
}else{ }else if(current->Element() < data){
pointer = &current->right; pointer = &current->right;
current = current->right; current = current->right;
} }
} }
return lastLeft; return lastLeft;
}else{ }
return lastLeft;
} template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerTo(node, data));
}
template <typename Data>
typename BST<Data>::NodeLnk** BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToPredecessor(node, data));
} }
template <typename Data> template <typename Data>

View File

@ -53,7 +53,7 @@ public:
bool Exists(const Data&) const noexcept override; // Override TestableContainer member bool Exists(const Data&) const noexcept override; // Override TestableContainer member
public: protected:
// Auxiliary member functions // Auxiliary member functions

View File

@ -7,7 +7,7 @@ void move();
int f(); int f();
int main(){ int main(){
//lvalue(); lvalue();
//rvalue(); //rvalue();
move(); move();
return 0; return 0;
@ -23,6 +23,10 @@ void lvalue(){
riferimento++; riferimento++;
cout<<var<<endl; // stampa 8 cout<<var<<endl; // stampa 8
/* il riferimento ha lo stesso indirizzo della variabile */
if(&var == &riferimento)
cout<<"Stesso indirizzo";
// il che è esattamente equivalente a fare questo // il che è esattamente equivalente a fare questo
int var2 = 7; int var2 = 7;
int* riferimento2 = &var2; int* riferimento2 = &var2;