From 2b7c59412cfbe3a7760b6c27bfbea07608f50f7a Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 12 May 2021 12:02:04 +0200 Subject: [PATCH] Library 4 --- librerie/exercise4/bst/bst.cpp | 189 +++++++++++++++++++++++++++++++-- 1 file changed, 183 insertions(+), 6 deletions(-) diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index 34d3633..77a3ec5 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -48,14 +48,14 @@ bool BST::operator==(const BST& bst) const noexcept{ template bool BST::operator!=(const BST& bst) const noexcept{ - return !(*this = bst) + return !(*this == bst); } template void BST::Insert(const Data& data) noexcept{ NodeLnk*& pointer = FindPointerTo(root, data); if(pointer == nullptr){ - pointer = CreateNode(data); + pointer = BinaryTreeLnk::CreateNode(data); size++; } } @@ -94,7 +94,7 @@ template void BST::RemoveMin(){ if(root == nullptr) throw std::length_error("Empty tree!"); - return DetachMin(root); + delete DetachMin(root); } @@ -116,12 +116,12 @@ template void BST::RemoveMax(){ if(root == nullptr) throw std::length_error("Empty tree!"); - return DetachMax(root); + delete DetachMax(root); } template const Data& BST::Predecessor(const Data& data) const{ - NodeLnk* const* ptr = FindPointerToPredecessor(root, data) + NodeLnk* const* ptr = FindPointerToPredecessor(root, data); if(ptr!=nullptr){ return (*(*ptr)).Element(); }else{ @@ -151,7 +151,7 @@ void BST::RemovePredecessor(const Data& data){ template const Data& BST::Successor(const Data& data) const{ - NodeLnk* const* ptr = FindPointerToSuccessor(root, data) + NodeLnk* const* ptr = FindPointerToSuccessor(root, data); if(ptr!=nullptr){ return (*(*ptr)).Element(); }else{ @@ -179,4 +179,181 @@ void BST::RemoveSuccessor(const Data& data){ } } +template +bool BST::Exists(const Data& data) const noexcept{ + return (FindPointerTo(root, data) != nullptr); +} + +template +Data BST::DataNDelete(NodeLnk* ptr){ + Data data = ptr->Element(); + delete ptr; + return data; +} + +template +typename BST::NodeLnk* BST::Detach(NodeLnk*& ptrref) noexcept{ + if(ptrref == nullptr) return nullptr; + + if(ptrref->left == nullptr){ + return SkipOnRight(ptrref); + } + else if(ptrref->right == nullptr){ + return SkipOnLeft(ptrref); + } + else{ + NodeLnk* maxNode = DetachMax(ptrref->left); + std::swap(ptrref->data , maxNode->data); + return maxNode; + } +} + +template +typename BST::NodeLnk* BST::DetachMin(NodeLnk*& ptrref) noexcept{ + return SkipOnRight(FindPointerToMin(ptrref)); +} + +template +typename BST::NodeLnk* BST::DetachMax(NodeLnk*& ptrref) noexcept{ + return SkipOnLeft(FindPointerToMax(ptrref)); +} + +template +typename BST::NodeLnk* BST::SkipOnLeft(NodeLnk*& ptrref) noexcept{ + NodeLnk* left = nullptr; + if(ptrref != nullptr){ + std::swap(left, ptrref->left); + std::swap(left, ptrref); + --size; + } + return left; +} + +template +typename BST::NodeLnk* BST::SkipOnRight(NodeLnk*& ptrref) noexcept{ + NodeLnk* right = nullptr; + if(ptrref != nullptr){ + std::swap(right, ptrref->right); + std::swap(right, ptrref); + --size; + } + return right; +} + +template +typename BST::NodeLnk* const& BST::FindPointerToMin(NodeLnk* const& node) const noexcept{ + NodeLnk* const* ptr = &node; + NodeLnk* curr = node; + if(curr!=nullptr){ + while(curr->left != nullptr){ + ptr = &curr->left; + curr = curr->left; + } + } + return *ptr; +} + +template +typename BST::NodeLnk*& BST::FindPointerToMin(NodeLnk*& node) noexcept{ + return const_cast(static_cast *>(this)->FindPointerToMin(node)); +} + +template +typename BST::NodeLnk* const& BST::FindPointerToMax(NodeLnk* const& node) const noexcept{ + NodeLnk* const* ptr = &node; + NodeLnk* curr = node; + if(curr!=nullptr){ + while(curr->right != nullptr){ + ptr = &curr->right; + curr = curr->right; + } + } + return *ptr; +} + +template +typename BST::NodeLnk*& BST::FindPointerToMax(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; + } + } + } + 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; + } + } + 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)); +} + +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)); +} + }