From 65942205cd28c70e6bf5b7a0e438d99db363bc29 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Thu, 29 Apr 2021 08:36:55 +0200 Subject: [PATCH] Library 3 --- librerie/exercise3/binarytree/binarytree.cpp | 93 +++++++++++++++---- .../binarytree/lnk/binarytreelnk.hpp | 31 ++++--- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 78df502..7032298 100644 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -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 { @@ -364,23 +370,18 @@ bool BTPostOrderIterator::Terminated() noexcept{ template void BTPostOrderIterator::operator++(){ if(Terminated()) throw std::out_of_range("Iterator is terminated!"); - - if(curr == &stack.Top().LeftChild()){ - if(stack.Top().HasRightChild()){ - curr = DeepestLeftLeaf(&stack.Top().RightChild()); - }else{ - try{ + try{ + if(curr == &stack.Top().LeftChild()){ + if(stack.Top().HasRightChild()){ + curr = DeepestLeftLeaf(&stack.Top().RightChild()); + }else{ curr = &stack.TopNPop(); - }catch(std::length_error exc){ - curr = nullptr; } - } - }else{ - try{ + }else{ curr = &stack.TopNPop(); - }catch(std::length_error exc){ - curr = nullptr; } + }catch(std::length_error exc){ + curr = nullptr; } } @@ -466,18 +467,78 @@ void BTInOrdertOrderIterator::operator++(){ } } +template +BTBreadthIterator::BTBreadthIterator(const BinaryTree& tree){ + curr = &(tree.Root()); +} +template +BTBreadthIterator::BTBreadthIterator(const BTBreadthIterator& itr){ + curr = itr.curr; + queue = itr.queue; +} +template +BTBreadthIterator::BTBreadthIterator(BTBreadthIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(queue, itr.queue); +} +template +BTBreadthIterator::~BTBreadthIterator(){ + curr = nullptr; + queue.Clear(); +} +template +BTBreadthIterator& BTBreadthIterator::operator=(const BTBreadthIterator& itr){ + curr = itr.curr; + queue = itr.queue; + return *this; +} +template +BTBreadthIterator& BTBreadthIterator::operator=(BTBreadthIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(queue, itr.queue); + return *this; +} +template +bool BTBreadthIterator::operator==(const BTBreadthIterator& itr) const noexcept{ + return ( curr==itr.curr && queue==itr.queue ); +} +template +bool BTBreadthIterator::operator!=(const BTBreadthIterator& itr) const noexcept{ + return !(*this == itr); +} +template +struct BinaryTree::Node BTBreadthIterator::operator*() const{ + return *curr; +} +template +bool BTBreadthIterator::Terminated() noexcept{ + return curr == nullptr; +} - - +template +void BTBreadthIterator::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; + } +} /* ************************************************************************** */ diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp index 4d18351..40bba30 100644 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp @@ -13,43 +13,48 @@ namespace lasd { /* ************************************************************************** */ template -class BinaryTreeLnk { // Must extend BinaryTree +class BinaryTreeLnk : virtual protected BinaryTree{ // Must extend BinaryTree private: - // ... - protected: - // using BinaryTree::???; + using BinaryTree::size; - // ... - - struct NodeLnk { // Must extend Node + struct NodeLnk : virtual protected BinaryTree::Node { // Must extend Node private: - // ... - protected: - // ... + using BinaryTree::Node::data; + struct NodeLnk* left; + struct* NodeLnk* right; 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: // Default constructor - // BinaryTreeLnk() specifiers; + BinaryTreeLnk(); /* ************************************************************************ */ // Specific constructors - // BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer + BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer /* ************************************************************************ */