Library 3 & Library 4

- resolved bug in library 3 (and in lib4)
- 0 errors in simpletests for library 4
This commit is contained in:
Alessandro Ferro 2021-05-13 18:38:07 +02:00
parent 2b7c59412c
commit 90f20eb405
6 changed files with 108 additions and 85 deletions

View File

@ -419,7 +419,7 @@ void BTPostOrderIterator<Data>::operator++(){
if(stack.Empty()){ if(stack.Empty()){
curr = nullptr; curr = nullptr;
}else{ }else{
if( curr == &((stack.Top())->LeftChild()) ){ if((stack.Top())->HasLeftChild() && curr == &((stack.Top())->LeftChild()) ){
if( (stack.Top())->HasRightChild() ){ if( (stack.Top())->HasRightChild() ){
curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); curr = DeepestLeftLeaf(&((stack.Top())->RightChild()));
}else{ }else{

View File

@ -419,7 +419,7 @@ void BTPostOrderIterator<Data>::operator++(){
if(stack.Empty()){ if(stack.Empty()){
curr = nullptr; curr = nullptr;
}else{ }else{
if( curr == &((stack.Top())->LeftChild()) ){ if((stack.Top())->HasLeftChild() && curr == &((stack.Top())->LeftChild()) ){
if( (stack.Top())->HasRightChild() ){ if( (stack.Top())->HasRightChild() ){
curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); curr = DeepestLeftLeaf(&((stack.Top())->RightChild()));
}else{ }else{

View File

@ -15,10 +15,11 @@ protected:
protected: protected:
using BinaryTree<Data>::Node::data; using BinaryTree<Data>::Node::data;
public:
struct NodeLnk* left = nullptr; struct NodeLnk* left = nullptr;
struct NodeLnk* right = nullptr; struct NodeLnk* right = nullptr;
public:
struct NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible. 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. 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) bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)

View File

@ -66,7 +66,6 @@ void BST<Data>::Insert(Data&& data) noexcept{
if(pointer == nullptr){ if(pointer == nullptr){
pointer = new NodeLnk(); pointer = new NodeLnk();
std::swap(pointer->value, data); std::swap(pointer->value, data);
size++; size++;
} }
} }
@ -185,14 +184,14 @@ bool BST<Data>::Exists(const Data& data) const noexcept{
} }
template <typename Data> template <typename Data>
Data BST<Data>::DataNDelete(NodeLnk* ptr){ Data BST<Data>::DataNDelete(struct BST<Data>::NodeLnk* ptr){
Data data = ptr->Element(); Data data = ptr->Element();
delete ptr; delete ptr;
return data; return data;
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* BST<Data>::Detach(NodeLnk*& ptrref) noexcept{ typename BST<Data>::NodeLnk* BST<Data>::Detach(struct BST<Data>::NodeLnk*& ptrref) noexcept{
if(ptrref == nullptr) return nullptr; if(ptrref == nullptr) return nullptr;
if(ptrref->left == nullptr){ if(ptrref->left == nullptr){
@ -203,23 +202,23 @@ typename BST<Data>::NodeLnk* BST<Data>::Detach(NodeLnk*& ptrref) noexcept{
} }
else{ else{
NodeLnk* maxNode = DetachMax(ptrref->left); NodeLnk* maxNode = DetachMax(ptrref->left);
std::swap(ptrref->data , maxNode->data); std::swap(ptrref->Element() , maxNode->Element());
return maxNode; return maxNode;
} }
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* BST<Data>::DetachMin(NodeLnk*& ptrref) noexcept{ typename BST<Data>::NodeLnk* BST<Data>::DetachMin(struct BST<Data>::NodeLnk*& ptrref) noexcept{
return SkipOnRight(FindPointerToMin(ptrref)); return SkipOnRight(FindPointerToMin(ptrref));
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* BST<Data>::DetachMax(NodeLnk*& ptrref) noexcept{ typename BST<Data>::NodeLnk* BST<Data>::DetachMax(struct BST<Data>::NodeLnk*& ptrref) noexcept{
return SkipOnLeft(FindPointerToMax(ptrref)); return SkipOnLeft(FindPointerToMax(ptrref));
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* BST<Data>::SkipOnLeft(NodeLnk*& ptrref) noexcept{ typename BST<Data>::NodeLnk* BST<Data>::SkipOnLeft(struct BST<Data>::NodeLnk*& ptrref) noexcept{
NodeLnk* left = nullptr; NodeLnk* left = nullptr;
if(ptrref != nullptr){ if(ptrref != nullptr){
std::swap(left, ptrref->left); std::swap(left, ptrref->left);
@ -230,7 +229,7 @@ typename BST<Data>::NodeLnk* BST<Data>::SkipOnLeft(NodeLnk*& ptrref) noexcept{
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* BST<Data>::SkipOnRight(NodeLnk*& ptrref) noexcept{ typename BST<Data>::NodeLnk* BST<Data>::SkipOnRight(struct BST<Data>::NodeLnk*& ptrref) noexcept{
NodeLnk* right = nullptr; NodeLnk* right = nullptr;
if(ptrref != nullptr){ if(ptrref != nullptr){
std::swap(right, ptrref->right); std::swap(right, ptrref->right);
@ -241,7 +240,7 @@ typename BST<Data>::NodeLnk* BST<Data>::SkipOnRight(NodeLnk*& ptrref) noexcept{
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMin(NodeLnk* const& node) const noexcept{ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMin(struct BST<Data>::NodeLnk* const& node) const noexcept{
NodeLnk* const* ptr = &node; NodeLnk* const* ptr = &node;
NodeLnk* curr = node; NodeLnk* curr = node;
if(curr!=nullptr){ if(curr!=nullptr){
@ -254,12 +253,12 @@ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMin(NodeLnk* const&
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMin(NodeLnk*& node) noexcept{ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMin(struct BST<Data>::NodeLnk*& node) noexcept{
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMin(node)); return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMin(node));
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMax(NodeLnk* const& node) const noexcept{ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMax(struct BST<Data>::NodeLnk* const& node) const noexcept{
NodeLnk* const* ptr = &node; NodeLnk* const* ptr = &node;
NodeLnk* curr = node; NodeLnk* curr = node;
if(curr!=nullptr){ if(curr!=nullptr){
@ -272,88 +271,111 @@ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMax(NodeLnk* const&
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMax(NodeLnk*& node) noexcept{ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMax(struct BST<Data>::NodeLnk*& node) noexcept{
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 ----- */ /* ---- END ----- */
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(NodeLnk* const& node) const noexcept{ typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
NodeLnk* const* ptr = &node; NodeLnk* const* pointer = &ref;
NodeLnk* curr = node; NodeLnk* current = ref;
if(curr!=nullptr){ if(current != nullptr){
while(curr!=nullptr || curr->Element() != node->Element()){ while(current != nullptr && current->Element() != data){
if(curr->Element() < node->Element()){ if(current->Element() < data){
ptr = &curr->right; pointer = &current->right;
curr = curr->right; current = current->right;
}else if(curr->Element() > node->Element()){ }else if(current->Element() > data){
ptr = &curr->left; pointer = &current->left;
curr = curr->left; current = current->left;
} }
} }
return *pointer;
}else{
return *pointer;
} }
if(curr->Element() == node->Element()) return *ptr;
else return nullptr;
} }
template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(NodeLnk*& node) noexcept{
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerTo(node));
}
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToPredecessor(NodeLnk* const& node) const noexcept{ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
if(node == nullptr) return nullptr; return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerTo(node, data));
NodeLnk* const* ptr = &node; }
NodeLnk* curr = node;
NodeLnk* lastRight;
while(curr!=nullptr || node->Element() != curr->Element()){ template <typename Data>
if(node->Element() == curr->Element()){ typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
ptr = FindPointerToMax(curr->left); NodeLnk* const* pointer = &ref;
}else if(node->Element() < curr->Element()){ NodeLnk* current = ref;
ptr = &curr->left; NodeLnk* const* lastRight = pointer;
curr = curr->left;
}else{
ptr = &curr->right; if(ref != nullptr){
curr = curr->right; while( current != nullptr){
lastRight = ptr; if(data == current->Element()){
if(current->HasLeftChild()){
return pointer = &(FindPointerToMax(current->left));
}
return lastRight;
}else if(data < current->Element()){
pointer = &current->left;
current = current->left;
}else{
lastRight = pointer;
pointer = &current->right;
current = current->right;
}
} }
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{
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 = &current->left;
current = current->left;
}else{
pointer = &current->right;
current = current->right;
}
}
return lastLeft;
}else{
return lastLeft;
} }
if(node->Element() == curr->Element()) return *ptr;
else return lastRight;
} }
template <typename Data> template <typename Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToPredecessor(NodeLnk*& node) noexcept{ typename BST<Data>::NodeLnk** BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToPredecessor(node)); return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToSuccessor(node, data));
} }
template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::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 Data>
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToSuccessor(NodeLnk*& node) noexcept{ /* ************************************************************************** */
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToSuccessor(node));
}
} }

View File

@ -47,10 +47,10 @@ public:
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // Copy assignment
BST& operator=(const BST<Data>&); BST<Data>& operator=(const BST<Data>&);
// Move assignment // Move assignment
BST& operator=(BST<Data>&&) noexcept; BST<Data>& operator=(BST<Data>&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
@ -107,14 +107,14 @@ protected:
NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept; NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept;
NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept; NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept;
NodeLnk* const& FindPointerTo(NodeLnk* const&) const noexcept; NodeLnk* const& FindPointerTo(NodeLnk* const&, Data) const noexcept;
NodeLnk*& FindPointerTo(NodeLnk*&) noexcept; NodeLnk*& FindPointerTo(NodeLnk*&, Data) noexcept;
NodeLnk* const& FindPointerToPredecessor(NodeLnk* const&) const noexcept; NodeLnk* const* FindPointerToPredecessor(NodeLnk* const&, Data) const noexcept;
NodeLnk*& FindPointerToPredecessor(NodeLnk*&) 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;
}; };

View File

@ -1,7 +1,7 @@
#! /bin/bash #! /bin/bash
g++ -O3 -o main \ g++-10 -O3 -o main \
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \ zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \ zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \ zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \