mirror of https://github.com/xfarrow/lasd.git
parent
147730b10b
commit
ae5b251099
|
@ -93,7 +93,6 @@ void BST<Data>::RemoveMin(){
|
|||
delete DetachMin(root);
|
||||
}
|
||||
|
||||
|
||||
template <typename Data>
|
||||
const Data& BST<Data>::Max() const{
|
||||
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));
|
||||
}
|
||||
|
||||
/* ---- END ----- */
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
|
||||
/*
|
||||
|
@ -304,45 +301,43 @@ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::N
|
|||
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>
|
||||
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* current = ref;
|
||||
NodeLnk* const* lastRight = pointer;
|
||||
NodeLnk* const* lastRight = nullptr;
|
||||
|
||||
if(ref != nullptr){
|
||||
while(current != nullptr){
|
||||
if(data == current->Element()){
|
||||
if(current->HasLeftChild()){
|
||||
return pointer = &(FindPointerToMax(current->left));
|
||||
}
|
||||
return lastRight;
|
||||
}else if(data < current->Element()){
|
||||
pointer = ¤t->left;
|
||||
current = current->left;
|
||||
return &(FindPointerToMax(current->left));
|
||||
}else{
|
||||
return lastRight;
|
||||
}
|
||||
}else if(current->Element() < data){
|
||||
lastRight = pointer;
|
||||
pointer = ¤t->right;
|
||||
pointer = &(current->right);
|
||||
current = current->right;
|
||||
}else if(current->Element() > data){
|
||||
pointer = &(current->left);
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
return lastRight;
|
||||
}else{
|
||||
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>
|
||||
typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
|
||||
|
@ -350,26 +345,34 @@ typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToSuccessor(struct BST
|
|||
NodeLnk* current = ref;
|
||||
NodeLnk* const* lastLeft = nullptr;
|
||||
|
||||
if(ref != nullptr){
|
||||
while( current != nullptr){
|
||||
if(data == current->Element()){
|
||||
if(current->HasRightChild()){
|
||||
return pointer = &(FindPointerToMin(current->right));
|
||||
}
|
||||
else{
|
||||
return lastLeft;
|
||||
}else if(data < current->Element()){
|
||||
}
|
||||
}else if(current->Element() > data){
|
||||
lastLeft = pointer;
|
||||
pointer = ¤t->left;
|
||||
current = current->left;
|
||||
}else{
|
||||
}else if(current->Element() < data){
|
||||
pointer = ¤t->right;
|
||||
current = current->right;
|
||||
}
|
||||
}
|
||||
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>
|
||||
|
|
|
@ -53,7 +53,7 @@ public:
|
|||
|
||||
bool Exists(const Data&) const noexcept override; // Override TestableContainer member
|
||||
|
||||
public:
|
||||
protected:
|
||||
|
||||
// Auxiliary member functions
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ void move();
|
|||
int f();
|
||||
|
||||
int main(){
|
||||
//lvalue();
|
||||
lvalue();
|
||||
//rvalue();
|
||||
move();
|
||||
return 0;
|
||||
|
@ -23,6 +23,10 @@ void lvalue(){
|
|||
riferimento++;
|
||||
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
|
||||
int var2 = 7;
|
||||
int* riferimento2 = &var2;
|
||||
|
|
Loading…
Reference in New Issue