From 4add56d7326d782e7de28809b2b5fcca0848bc19 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 5 May 2021 10:03:25 +0200 Subject: [PATCH] Library 3 bugfix --- librerie/exercise3/binarytree/binarytree.cpp | 16 +++++++++------- .../exercise3/binarytree/lnk/binarytreelnk.cpp | 3 +++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 5060d17..a5fdf86 100755 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -292,19 +292,21 @@ 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()); + if( curr->HasRightChild() ){ + stack.Push(&(curr->RightChild())); + } + + curr = &(curr->LeftChild()); }else if(curr->HasRightChild()){ curr = &curr->RightChild(); } - else{ - try{ - curr = stack.TopNPop(); - }catch(std:: length_error exc){ + else{ // is leaf + if(stack.Empty()){ curr = nullptr; + }else{ + curr = stack.TopNPop(); } } } diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp index a84a4e0..5b269f9 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp @@ -147,12 +147,15 @@ bool BinaryTreeLnk::operator!=(const BinaryTreeLnk& tree) const noex template struct BinaryTree::Node& BinaryTreeLnk::Root() const{ + if(size==0) throw std::length_error("Empty tree!"); return *root; } template void BinaryTreeLnk::Clear(){ DeleteTree(root); + root = nullptr; + size = 0; } }