diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 5778d20..78df502 100644 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -172,7 +172,7 @@ void FoldInOrder(const FoldFunctor function, const void* par, void* acc, const N } template -void MapBreadth(const MapFunctor function, void* par, Node* node){ +void BinaryTree::MapBreadth(const MapFunctor function, void* par, Node* node){ QueueLst toVisit; if(node != nullptr){ toVisit.Enqueue(node); @@ -190,6 +190,293 @@ void MapBreadth(const MapFunctor function, void* par, Node* node){ } } +template +void BinaryTree::FoldBreadth(const MapFunctor function, const void* par, void* acc, const Node* node){ + QueueLst toVisit; + if(node != nullptr){ + toVisit.Enqueue(node); + while(!toVisit.Empty()){ + func(toVisit.Head()->Element(), par, acc); + + if(toVisit.Head()->HasLeftChild()){ + toVisit.Enqueue(&(toVisit.Head()->LeftChild())); + } + if(toVisit.Head()->HasRightChild()){ + toVisit.Enqueue(&(toVisit.Head()->RightChild())); + } + toVisit.Dequeue(); + } + } +} + +template +BTPreOrderIterator::BTPreOrderIterator(const BinaryTree& tree){ + curr = &tree.Root(); +} + +template +BTPreOrderIterator::BTPreOrderIterator(const BTPreOrderIterator& itr){ + //curr = &(*itr); + curr = itr.curr; + stack = itr.stack; +} + +template +BTPreOrderIterator::BTPreOrderIterator(BTPreOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); +} + +template +BTPreOrderIterator::~BTPreOrderIterator(){ + stack.Clear(); + curr = nullptr; +} + +template +BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator& itr){ + //curr = &(*itr); + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTPreOrderIterator& BTPreOrderIterator::operator=(BTPreOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); + return *this; +} + +template +bool BTPreOrderIterator::operator==(const BTPreOrderIterator& itr) const noexcept{ + return ( curr==itr.curr && stack==itr.stack ); +} + +template +bool BTPreOrderIterator::operator!=(const BTPreOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +struct BinaryTree::Node BTPreOrderIterator::operator*() const{ + return *curr; +} + +template +bool BTPreOrderIterator::Terminated() noexcept{ + return curr==nullptr; +} + +template +void BTPreOrderIterator::operator++(){ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if( (curr->HasLeftChild() ){ + curr = &(curr->LeftChild()); + + if( (curr->HasRightChild() ) + stack.Push(&curr->RightChild()); + + }else if( (curr->HasRightChild() ){ + curr = &curr->RightChild(); + } + else{ + try{ + curr = &stack.TopNPop(); + }catch(std:: length_error exc){ + curr = nullptr; + } + } +} + +template +struct BinaryTree::Node* BTPostOrderIterator::DeepestLeftLeaf(struct BinaryTree::Node* node){ + if(node->HasLeftChild()){ + stack.Push(node); + MostLeftLeaf(node->LeftChild()); + } + else if(node->HasRightChild()){ + stack.Push(node); + MostLeftLeaf(node->RightChild()); + } + else + return node; +} + +template +BTPostOrderIterator::BTPostOrderIterator(const BinaryTree& tree){ + curr = DeepestLeftLeaf(&tree.Root()) +} + +template +BTPostOrderIterator::BTPostOrderIterator(const BTPostOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; +} + +template +BTPostOrderIterator::BTPostOrderIterator(BTPostOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); +} + +template +BTPostOrderIterator::~BTPostOrderIterator(){ + curr = nullptr; + stack.Clear(); +} + +template +BTPostOrderIterator& BTPostOrderIterator::operator=(const BTPostOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTPostOrderIterator& BTPostOrderIterator::operator=(BTPostOrderIterator&& itr){ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); + return *this; +} + +template +bool BTPostOrderIterator::operator==(const BTPostOrderIterator& itr) const noexcept{ + return (curr == itr.curr && stack == itr.stack ); +} + +template +bool BTPostOrderIterator::operator!=(const BTPostOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +struct BinaryTree::Node BTPostOrderIterator::operator*() const{ + return *curr; +} + +template +bool BTPostOrderIterator::Terminated() noexcept{ + return (curr == nullptr) +} + +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{ + curr = &stack.TopNPop(); + }catch(std::length_error exc){ + curr = nullptr; + } + } + }else{ + try{ + curr = &stack.TopNPop(); + }catch(std::length_error exc){ + curr = nullptr; + } + } +} + +template +struct BinaryTree::Node* BTInOrdertOrderIterator::MostLeftNode(struct BinaryTree::Node* root){ + if(root->HasLeftChild()){ + stack.Push(root); + MostLeftNode(root->LeftChild()); + }else{ + return root; + } +} + +template +BTInOrdertOrderIterator::BTInOrderIterator(const BinaryTree& tree){ + curr = MostLeftNode(tree.Root()); +} + +template +BTInOrdertOrderIterator::BTInOrderIterator(const BTInOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; +} + +template +BTInOrdertOrderIterator::BTInOrderIterator(BTInOrderIterator&& toMove) noexcept{ + std::move(curr, toMove.curr); + std::move(stack, toMove.stack); +} + +template + BTInOrdertOrderIterator::~BTInOrderIterator(){ + stack.Clear(); + curr = nullptr; + } + +template +BTInOrdertOrderIterator::BTInOrderIterator& operator=(const BTInOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTInOrdertOrderIterator::BTInOrderIterator& operator=(BTInOrderIterator&& itr){ + std::move(curr, toMove.curr); + std::move(stack, toMove.stack); + return *this; +} + +template +bool BTInOrdertOrderIterator::operator==(const BTInOrderIterator& itr) const noexcept{ + return (curr == itr.curr && stack == itr.stack ); +} + +template +bool BTInOrdertOrderIterator::operator!=(const BTInOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +struct BinaryTree::Node BTInOrdertOrderIterator::operator*() const{ + return *curr; +} + +template +bool BTInOrdertOrderIterator::Terminated() noexcept{ + return (curr == nullptr); +} + +template +void BTInOrdertOrderIterator::operator++(){ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if(curr->HasRightChild()){ + curr = MostLeftNode(curr->RightChild()); + }else{ + try{ + curr = &(stack.TopNPop()); + }catch(std::length_error exc){ + curr = nullptr; + } + } +} + + + + + + + + + + + + /* ************************************************************************** */ diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp index b6e72fb..f6a82e6 100644 --- a/librerie/exercise3/binarytree/binarytree.hpp +++ b/librerie/exercise3/binarytree/binarytree.hpp @@ -133,7 +133,7 @@ private: protected: struct BinaryTree::Node* curr; - StackLst stack; + StackLst::Node*> stack(); // default constructor for stacklst public: @@ -192,8 +192,8 @@ private: protected: struct BinaryTree::Node* curr; - StackLst stack; - + StackLst::Node*> stack; + struct BinaryTree::Node* DeepestLeftLeaf(struct BinaryTree::Node*); public: // Specific constructors @@ -251,8 +251,8 @@ private: protected: struct BinaryTree::Node* curr; - StackLst stack; - + StackLst::Node*> stack; + struct BinaryTree::Node* MostLeftNode(struct BinaryTree::Node*); public: // Specific constructors @@ -311,7 +311,7 @@ private: protected: struct BinaryTree::Node* curr; - QueueVec queue; + QueueVec::Node*> queue; public: