diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 4a8744b..9216be4 100755 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -419,7 +419,7 @@ void BTPostOrderIterator::operator++(){ if(stack.Empty()){ curr = nullptr; }else{ - if( curr == &((stack.Top())->LeftChild()) ){ + if((stack.Top())->HasLeftChild() && curr == &((stack.Top())->LeftChild()) ){ if( (stack.Top())->HasRightChild() ){ curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); }else{ diff --git a/librerie/exercise4/binarytree/binarytree.cpp b/librerie/exercise4/binarytree/binarytree.cpp index 4a8744b..9216be4 100755 --- a/librerie/exercise4/binarytree/binarytree.cpp +++ b/librerie/exercise4/binarytree/binarytree.cpp @@ -419,7 +419,7 @@ void BTPostOrderIterator::operator++(){ if(stack.Empty()){ curr = nullptr; }else{ - if( curr == &((stack.Top())->LeftChild()) ){ + if((stack.Top())->HasLeftChild() && curr == &((stack.Top())->LeftChild()) ){ if( (stack.Top())->HasRightChild() ){ curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); }else{ diff --git a/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp index 85c852e..09d5aa2 100755 --- a/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp @@ -15,10 +15,11 @@ protected: protected: using BinaryTree::Node::data; + + public: struct NodeLnk* left = nullptr; struct NodeLnk* right = nullptr; - public: struct NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible. struct NodeLnk& operator=(NodeLnk&&) noexcept; // Move assignment of abstract types should not be possible. bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions) diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index 77a3ec5..be61c4d 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -66,7 +66,6 @@ void BST::Insert(Data&& data) noexcept{ if(pointer == nullptr){ pointer = new NodeLnk(); std::swap(pointer->value, data); - size++; } } @@ -185,14 +184,14 @@ bool BST::Exists(const Data& data) const noexcept{ } template -Data BST::DataNDelete(NodeLnk* ptr){ +Data BST::DataNDelete(struct BST::NodeLnk* ptr){ Data data = ptr->Element(); delete ptr; return data; } template -typename BST::NodeLnk* BST::Detach(NodeLnk*& ptrref) noexcept{ +typename BST::NodeLnk* BST::Detach(struct BST::NodeLnk*& ptrref) noexcept{ if(ptrref == nullptr) return nullptr; if(ptrref->left == nullptr){ @@ -203,23 +202,23 @@ typename BST::NodeLnk* BST::Detach(NodeLnk*& ptrref) noexcept{ } else{ NodeLnk* maxNode = DetachMax(ptrref->left); - std::swap(ptrref->data , maxNode->data); + std::swap(ptrref->Element() , maxNode->Element()); return maxNode; } } template -typename BST::NodeLnk* BST::DetachMin(NodeLnk*& ptrref) noexcept{ +typename BST::NodeLnk* BST::DetachMin(struct BST::NodeLnk*& ptrref) noexcept{ return SkipOnRight(FindPointerToMin(ptrref)); } template -typename BST::NodeLnk* BST::DetachMax(NodeLnk*& ptrref) noexcept{ +typename BST::NodeLnk* BST::DetachMax(struct BST::NodeLnk*& ptrref) noexcept{ return SkipOnLeft(FindPointerToMax(ptrref)); } template -typename BST::NodeLnk* BST::SkipOnLeft(NodeLnk*& ptrref) noexcept{ +typename BST::NodeLnk* BST::SkipOnLeft(struct BST::NodeLnk*& ptrref) noexcept{ NodeLnk* left = nullptr; if(ptrref != nullptr){ std::swap(left, ptrref->left); @@ -230,7 +229,7 @@ typename BST::NodeLnk* BST::SkipOnLeft(NodeLnk*& ptrref) noexcept{ } template -typename BST::NodeLnk* BST::SkipOnRight(NodeLnk*& ptrref) noexcept{ +typename BST::NodeLnk* BST::SkipOnRight(struct BST::NodeLnk*& ptrref) noexcept{ NodeLnk* right = nullptr; if(ptrref != nullptr){ std::swap(right, ptrref->right); @@ -241,7 +240,7 @@ typename BST::NodeLnk* BST::SkipOnRight(NodeLnk*& ptrref) noexcept{ } template -typename BST::NodeLnk* const& BST::FindPointerToMin(NodeLnk* const& node) const noexcept{ +typename BST::NodeLnk* const& BST::FindPointerToMin(struct BST::NodeLnk* const& node) const noexcept{ NodeLnk* const* ptr = &node; NodeLnk* curr = node; if(curr!=nullptr){ @@ -254,12 +253,12 @@ typename BST::NodeLnk* const& BST::FindPointerToMin(NodeLnk* const& } template -typename BST::NodeLnk*& BST::FindPointerToMin(NodeLnk*& node) noexcept{ +typename BST::NodeLnk*& BST::FindPointerToMin(struct BST::NodeLnk*& node) noexcept{ return const_cast(static_cast *>(this)->FindPointerToMin(node)); } template -typename BST::NodeLnk* const& BST::FindPointerToMax(NodeLnk* const& node) const noexcept{ +typename BST::NodeLnk* const& BST::FindPointerToMax(struct BST::NodeLnk* const& node) const noexcept{ NodeLnk* const* ptr = &node; NodeLnk* curr = node; if(curr!=nullptr){ @@ -272,88 +271,111 @@ typename BST::NodeLnk* const& BST::FindPointerToMax(NodeLnk* const& } template -typename BST::NodeLnk*& BST::FindPointerToMax(NodeLnk*& node) noexcept{ +typename BST::NodeLnk*& BST::FindPointerToMax(struct BST::NodeLnk*& node) noexcept{ return const_cast(static_cast *>(this)->FindPointerToMax(node)); } /* ---- END ----- */ template -typename BST::NodeLnk* const& BST::FindPointerTo(NodeLnk* const& node) const noexcept{ - NodeLnk* const* ptr = &node; - NodeLnk* curr = node; - if(curr!=nullptr){ - while(curr!=nullptr || curr->Element() != node->Element()){ - if(curr->Element() < node->Element()){ - ptr = &curr->right; - curr = curr->right; - }else if(curr->Element() > node->Element()){ - ptr = &curr->left; - curr = curr->left; +typename BST::NodeLnk* const& BST::FindPointerTo(struct BST::NodeLnk* const& ref, Data data) const noexcept{ + NodeLnk* const* pointer = &ref; + NodeLnk* current = ref; + if(current != nullptr){ + while(current != nullptr && current->Element() != data){ + if(current->Element() < data){ + pointer = ¤t->right; + current = current->right; + }else if(current->Element() > data){ + pointer = ¤t->left; + current = current->left; } } + return *pointer; + + }else{ + return *pointer; } - if(curr->Element() == node->Element()) return *ptr; - else return nullptr; } -template -typename BST::NodeLnk*& BST::FindPointerTo(NodeLnk*& node) noexcept{ - return const_cast(static_cast *>(this)->FindPointerTo(node)); -} template -typename BST::NodeLnk* const& BST::FindPointerToPredecessor(NodeLnk* const& node) const noexcept{ - if(node == nullptr) return nullptr; - NodeLnk* const* ptr = &node; - NodeLnk* curr = node; - NodeLnk* lastRight; - while(curr!=nullptr || node->Element() != curr->Element()){ - if(node->Element() == curr->Element()){ - ptr = FindPointerToMax(curr->left); - }else if(node->Element() < curr->Element()){ - ptr = &curr->left; - curr = curr->left; - }else{ - ptr = &curr->right; - curr = curr->right; - lastRight = ptr; +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{ + NodeLnk* const* pointer = &ref; + NodeLnk* current = ref; + NodeLnk* const* lastRight = pointer; + + +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; + }else{ + lastRight = pointer; + pointer = ¤t->right; + current = current->right; + } } + return lastRight; +}else{ + 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()){ + lastLeft = pointer; + pointer = ¤t->left; + current = current->left; + }else{ + pointer = ¤t->right; + current = current->right; + } + } + return lastLeft; + }else{ + return lastLeft; } - if(node->Element() == curr->Element()) return *ptr; - else return lastRight; } template -typename BST::NodeLnk*& BST::FindPointerToPredecessor(NodeLnk*& node) noexcept{ - return const_cast(static_cast *>(this)->FindPointerToPredecessor(node)); +typename BST::NodeLnk** BST::FindPointerToSuccessor(struct BST::NodeLnk*& node, Data data) noexcept{ + return const_cast(static_cast *>(this)->FindPointerToSuccessor(node, data)); } -template -typename BST::NodeLnk* const& BST::FindPointerToSuccessor(NodeLnk* const& node) const noexcept{ - if(node == nullptr) return nullptr; - NodeLnk* const* ptr = &node; - NodeLnk* curr = node; - NodeLnk* lastLeft; - while(curr!=nullptr || node->Element() != curr->Element()){ - if(node->Element() == curr->Element()){ - ptr = FindPointerToMin(curr->left); - }else if(node->Element() < curr->Element()){ - ptr = &curr->left; - curr = curr->left; - lastLeft = ptr; - }else{ - ptr = &curr->right; - curr = curr->right; - } - } - if(node->Element() == curr->Element()) return *ptr; - else return lastLeft; -} -template -typename BST::NodeLnk*& BST::FindPointerToSuccessor(NodeLnk*& node) noexcept{ - return const_cast(static_cast *>(this)->FindPointerToSuccessor(node)); -} + +/* ************************************************************************** */ } diff --git a/librerie/exercise4/bst/bst.hpp b/librerie/exercise4/bst/bst.hpp index 3b6ac0b..9a2cca7 100755 --- a/librerie/exercise4/bst/bst.hpp +++ b/librerie/exercise4/bst/bst.hpp @@ -47,10 +47,10 @@ public: /* ************************************************************************ */ // Copy assignment - BST& operator=(const BST&); + BST& operator=(const BST&); // Move assignment - BST& operator=(BST&&) noexcept; + BST& operator=(BST&&) noexcept; /* ************************************************************************ */ @@ -107,14 +107,14 @@ protected: NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept; NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept; - NodeLnk* const& FindPointerTo(NodeLnk* const&) const noexcept; - NodeLnk*& FindPointerTo(NodeLnk*&) noexcept; + NodeLnk* const& FindPointerTo(NodeLnk* const&, Data) const noexcept; + NodeLnk*& FindPointerTo(NodeLnk*&, Data) noexcept; - NodeLnk* const& FindPointerToPredecessor(NodeLnk* const&) const noexcept; - NodeLnk*& FindPointerToPredecessor(NodeLnk*&) noexcept; + NodeLnk* const* FindPointerToPredecessor(NodeLnk* const&, Data) const noexcept; + NodeLnk** FindPointerToPredecessor(NodeLnk*&, Data) noexcept; + NodeLnk* const* FindPointerToSuccessor(NodeLnk* const&, Data) const noexcept; + NodeLnk** FindPointerToSuccessor(NodeLnk*&, Data) noexcept; - NodeLnk* const& FindPointerToSuccessor(NodeLnk* const&) const noexcept; - NodeLnk*& FindPointerToSuccessor(NodeLnk*&) noexcept; }; diff --git a/librerie/exercise4/build.sh b/librerie/exercise4/build.sh index cf5567b..bedbbbc 100755 --- a/librerie/exercise4/build.sh +++ b/librerie/exercise4/build.sh @@ -1,7 +1,7 @@ #! /bin/bash -g++ -O3 -o main \ +g++-10 -O3 -o main \ zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \ zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \ zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \