Library 3

This commit is contained in:
Alessandro Ferro 2021-04-29 08:36:55 +02:00
parent b6f6baeede
commit 65942205cd
2 changed files with 95 additions and 29 deletions

View File

@ -1,5 +1,11 @@
// #include "..." #include "../queue/queue.hpp"
#include "../queue/vec/queuevec.hpp"
#include "../queue/lst/queuelst.hpp"
#include "../stack/stack.hpp"
#include "../stack/lst/stacklst.hpp"
#include "../stack/vec/stackvec.hpp"
namespace lasd { namespace lasd {
@ -364,24 +370,19 @@ bool BTPostOrderIterator<Data>::Terminated() noexcept{
template <typename Data> template <typename Data>
void BTPostOrderIterator<Data>::operator++(){ void BTPostOrderIterator<Data>::operator++(){
if(Terminated()) throw std::out_of_range("Iterator is terminated!"); if(Terminated()) throw std::out_of_range("Iterator is terminated!");
try{
if(curr == &stack.Top().LeftChild()){ if(curr == &stack.Top().LeftChild()){
if(stack.Top().HasRightChild()){ if(stack.Top().HasRightChild()){
curr = DeepestLeftLeaf(&stack.Top().RightChild()); curr = DeepestLeftLeaf(&stack.Top().RightChild());
}else{ }else{
try{
curr = &stack.TopNPop(); curr = &stack.TopNPop();
}catch(std::length_error exc){
curr = nullptr;
}
} }
}else{ }else{
try{
curr = &stack.TopNPop(); curr = &stack.TopNPop();
}
}catch(std::length_error exc){ }catch(std::length_error exc){
curr = nullptr; curr = nullptr;
} }
}
} }
template <typename Data> template <typename Data>
@ -466,18 +467,78 @@ void BTInOrdertOrderIterator<Data>::operator++(){
} }
} }
template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){
curr = &(tree.Root());
}
template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BTBreadthIterator& itr){
curr = itr.curr;
queue = itr.queue;
}
template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(BTBreadthIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(queue, itr.queue);
}
template <typename Data>
BTBreadthIterator<Data>::~BTBreadthIterator(){
curr = nullptr;
queue.Clear();
}
template <typename Data>
BTBreadthIterator& BTBreadthIterator<Data>::operator=(const BTBreadthIterator& itr){
curr = itr.curr;
queue = itr.queue;
return *this;
}
template <typename Data>
BTBreadthIterator& BTBreadthIterator<Data>::operator=(BTBreadthIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(queue, itr.queue);
return *this;
}
template <typename Data>
bool BTBreadthIterator<Data>::operator==(const BTBreadthIterator& itr) const noexcept{
return ( curr==itr.curr && queue==itr.queue );
}
template <typename Data>
bool BTBreadthIterator<Data>::operator!=(const BTBreadthIterator& itr) const noexcept{
return !(*this == itr);
}
template <typename Data>
struct BinaryTree<Data>::Node BTBreadthIterator<Data>::operator*() const{
return *curr;
}
template <typename Data>
bool BTBreadthIterator<Data>::Terminated() noexcept{
return curr == nullptr;
}
template <typename Data>
void BTBreadthIterator<Data>::operator++(){
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
if(curr->HasLeftChild()){
queue.Enqueue(&(curr->LeftChild()));
}
if(curr->HasRightChild()){
queue.Enqueue(&(curr->RightChild()));
}
if(!queue.Empty()){
curr = queue.HeadNDequeue();
}else{
curr = nullptr;
}
}
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -13,43 +13,48 @@ namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data> template <typename Data>
class BinaryTreeLnk { // Must extend BinaryTree<Data> class BinaryTreeLnk : virtual protected BinaryTree<Data>{ // Must extend BinaryTree<Data>
private: private:
// ...
protected: protected:
// using BinaryTree<Data>::???; using BinaryTree<Data>::size;
// ... struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node
struct NodeLnk { // Must extend Node
private: private:
// ...
protected: protected:
// ... using BinaryTree<Data>::Node::data;
struct NodeLnk* left;
struct* NodeLnk* right;
public: public:
// ... NodeLnk& operator=(const Node&); // Copy assignment of abstract types should not be possible.
// Move assignment
NodeLnk& operator=(Node&&) noexcept override; // Move assignment of abstract types should not be possible.
bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions)
bool HasLeftChild() const noexcept override; // (concrete function should not throw exceptions)
bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions)
Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent)
Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent)
}; };
public: public:
// Default constructor // Default constructor
// BinaryTreeLnk() specifiers; BinaryTreeLnk();
/* ************************************************************************ */ /* ************************************************************************ */
// Specific constructors // Specific constructors
// BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer
/* ************************************************************************ */ /* ************************************************************************ */