diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index f1c2c51..c92868b 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -93,7 +93,6 @@ void BST::RemoveMin(){ delete DetachMin(root); } - template const Data& BST::Max() const{ if(root == nullptr) throw std::length_error("Empty tree!"); @@ -277,8 +276,6 @@ typename BST::NodeLnk*& BST::FindPointerToMax(struct BST::Node return const_cast(static_cast *>(this)->FindPointerToMax(node)); } -/* ---- END ----- */ - template typename BST::NodeLnk* const& BST::FindPointerTo(struct BST::NodeLnk* const& ref, Data data) const noexcept{ /* @@ -304,72 +301,78 @@ typename BST::NodeLnk* const& BST::FindPointerTo(struct BST::N return *pointer; } - -template -typename BST::NodeLnk*& BST::FindPointerTo(struct BST::NodeLnk*& node, Data data) noexcept{ - return const_cast(static_cast *>(this)->FindPointerTo(node, data)); -} - template typename BST::NodeLnk* const* BST::FindPointerToPredecessor(struct BST::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; + while(current != nullptr){ + if(data == current->Element()){ + if(current->HasLeftChild()){ + return &(FindPointerToMax(current->left)); }else{ - lastRight = pointer; - pointer = ¤t->right; - current = current->right; + return lastRight; } + }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 BST::NodeLnk** BST::FindPointerToPredecessor(struct BST::NodeLnk*& node, Data data) noexcept{ - return const_cast(static_cast *>(this)->FindPointerToPredecessor(node, data)); -} - - template typename BST::NodeLnk* const* BST::FindPointerToSuccessor(struct BST::NodeLnk* const& ref, Data data) const noexcept{ NodeLnk* const* pointer = &ref; 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)); } - return lastLeft; - }else if(data < current->Element()){ + else{ + return lastLeft; + } + }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 BST::NodeLnk*& BST::FindPointerTo(struct BST::NodeLnk*& node, Data data) noexcept{ + return const_cast(static_cast *>(this)->FindPointerTo(node, data)); +} + +template +typename BST::NodeLnk** BST::FindPointerToPredecessor(struct BST::NodeLnk*& node, Data data) noexcept{ + return const_cast(static_cast *>(this)->FindPointerToPredecessor(node, data)); } template diff --git a/librerie/exercise4/bst/bst.hpp b/librerie/exercise4/bst/bst.hpp index b1df7fd..33d7212 100755 --- a/librerie/exercise4/bst/bst.hpp +++ b/librerie/exercise4/bst/bst.hpp @@ -53,7 +53,7 @@ public: bool Exists(const Data&) const noexcept override; // Override TestableContainer member -public: +protected: // Auxiliary member functions diff --git a/teoria/riferimenti.cpp b/teoria/riferimenti.cpp index b44e0e7..fc79e82 100644 --- a/teoria/riferimenti.cpp +++ b/teoria/riferimenti.cpp @@ -7,7 +7,7 @@ void move(); int f(); int main(){ - //lvalue(); + lvalue(); //rvalue(); move(); return 0; @@ -23,6 +23,10 @@ void lvalue(){ riferimento++; cout<