Library 4

cleaner code
This commit is contained in:
Alessandro Ferro 2021-05-13 21:52:08 +02:00
parent 90f20eb405
commit 147730b10b
3 changed files with 45 additions and 87 deletions

View File

@ -78,21 +78,18 @@ void BST<Data>::Remove(const Data& data) noexcept{
template <typename Data>
const Data& BST<Data>::Min() const{
if(root == nullptr) throw std::length_error("Empty tree!");
return FindPointerToMin(root)->Element();
}
template <typename Data>
Data BST<Data>::MinNRemove(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DataNDelete(DetachMin(root));
}
template <typename Data>
void BST<Data>::RemoveMin(){
if(root == nullptr) throw std::length_error("Empty tree!");
delete DetachMin(root);
}
@ -100,21 +97,18 @@ void BST<Data>::RemoveMin(){
template <typename Data>
const Data& BST<Data>::Max() const{
if(root == nullptr) throw std::length_error("Empty tree!");
return FindPointerToMax(root)->Element();
}
template <typename Data>
Data BST<Data>::MaxNRemove(){
if(root == nullptr) throw std::length_error("Empty tree!");
return DataNDelete(DetachMax(root));
}
template <typename Data>
void BST<Data>::RemoveMax(){
if(root == nullptr) throw std::length_error("Empty tree!");
delete DetachMax(root);
}
@ -241,6 +235,14 @@ typename BST<Data>::NodeLnk* BST<Data>::SkipOnRight(struct BST<Data>::NodeLnk*&
template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMin(struct BST<Data>::NodeLnk* const& node) const noexcept{
/*
In order to return a [reference to a] const pointer, we need to
declare a variable (ptr) which points to a const pointer which points to
a NodeLnk.
This const pointer that points to a NodeLnk is the parameter of the function.
Hence, *ptr will be a const pointer.
*/
NodeLnk* const* ptr = &node;
NodeLnk* curr = node;
if(curr!=nullptr){
@ -279,23 +281,27 @@ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMax(struct BST<Data>::Node
template <typename Data>
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
NodeLnk* const* pointer = &ref;
/*
In order to return a [reference to a] const pointer, we need to
declare a variable (pointer) which points to a const pointer which points to
a NodeLnk.
This const pointer that points to a NodeLnk is the parameter of the function.
Hence, *pointer will be a const pointer.
*/
NodeLnk* const* pointer = &ref; //a pointer to a const pointer to a NodeLnk
NodeLnk* current = ref;
if(current != nullptr){
while(current != nullptr && current->Element() != data){
if(current->Element() < data){
pointer = &current->right;
pointer = &(current->right);
current = current->right;
}else if(current->Element() > data){
pointer = &current->left;
pointer = &(current->left);
current = current->left;
}
}
return *pointer;
}else{
return *pointer;
}
}
@ -304,15 +310,13 @@ typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk
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{
NodeLnk* const* pointer = &ref;
NodeLnk* current = ref;
NodeLnk* const* lastRight = pointer;
if(ref != nullptr){
if(ref != nullptr){
while( current != nullptr){
if(data == current->Element()){
if(current->HasLeftChild()){
@ -329,9 +333,9 @@ if(ref != nullptr){
}
}
return lastRight;
}else{
}else{
return lastRight;
}
}
}
template <typename Data>
@ -346,7 +350,6 @@ 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()){
@ -374,8 +377,4 @@ typename BST<Data>::NodeLnk** BST<Data>::FindPointerToSuccessor(struct BST<Data>
return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToSuccessor(node, data));
}
/* ************************************************************************** */
}

View File

@ -2,16 +2,10 @@
#ifndef BST_HPP
#define BST_HPP
/* ************************************************************************** */
#include "../binarytree/lnk/binarytreelnk.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BST : virtual public BinaryTreeLnk<Data> { // Must extend BinaryTreeLnk<Data>
@ -25,43 +19,18 @@ protected:
public:
BST() = default;
/* ************************************************************************ */
// Specific constructors
BST(const LinearContainer<Data>&); // A bst obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
BST(const LinearContainer<Data>&);
BST(const BST<Data>&);
// Move constructor
BST(BST<Data>&&) noexcept;
/* ************************************************************************ */
// Destructor
virtual ~BST();
/* ************************************************************************ */
// Copy assignment
BST<Data>& operator=(const BST<Data>&);
// Move assignment
BST<Data>& operator=(BST<Data>&&) noexcept;
/* ************************************************************************ */
// Comparison operators
bool operator==(const BST<Data>&) const noexcept;
bool operator!=(const BST<Data>&) const noexcept;
/* ************************************************************************ */
// Specific member functions
void Insert(const Data&) noexcept; // Copy of the value
void Insert(Data&&) noexcept; // Move of the value
void Remove(const Data&) noexcept;
@ -82,13 +51,9 @@ public:
Data SuccessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
void RemoveSuccessor(const Data&); // (concrete function must throw std::length_error when empty)
/* ************************************************************************ */
// Specific member functions (inherited from TestableContainer)
bool Exists(const Data&) const noexcept override; // Override TestableContainer member
protected:
public:
// Auxiliary member functions
@ -114,14 +79,8 @@ protected:
NodeLnk** FindPointerToPredecessor(NodeLnk*&, Data) noexcept;
NodeLnk* const* FindPointerToSuccessor(NodeLnk* const&, Data) const noexcept;
NodeLnk** FindPointerToSuccessor(NodeLnk*&, Data) noexcept;
};
/* ************************************************************************** */
}
#include "bst.cpp"
#endif

View File

@ -2,11 +2,11 @@
#include "zlasdtest/test.hpp"
#include "zmytest/test.hpp"
#include "bst/bst.hpp"
/* ************************************************************************** */
#include <iostream>
using namespace lasd;
/* ************************************************************************** */
int main() {