Library 3

bugfix
This commit is contained in:
Alessandro Ferro 2021-05-05 10:03:25 +02:00
parent 858e7debee
commit 4add56d732
2 changed files with 12 additions and 7 deletions

View File

@ -292,19 +292,21 @@ void BTPreOrderIterator<Data>::operator++(){
if(Terminated()) throw std::out_of_range("Iterator is terminated!"); if(Terminated()) throw std::out_of_range("Iterator is terminated!");
if(curr->HasLeftChild()){ if(curr->HasLeftChild()){
curr = &(curr->LeftChild());
if( curr->HasRightChild() ) if( curr->HasRightChild() ){
stack.Push(&curr->RightChild()); stack.Push(&(curr->RightChild()));
}
curr = &(curr->LeftChild());
}else if(curr->HasRightChild()){ }else if(curr->HasRightChild()){
curr = &curr->RightChild(); curr = &curr->RightChild();
} }
else{ else{ // is leaf
try{ if(stack.Empty()){
curr = stack.TopNPop();
}catch(std:: length_error exc){
curr = nullptr; curr = nullptr;
}else{
curr = stack.TopNPop();
} }
} }
} }

View File

@ -147,12 +147,15 @@ bool BinaryTreeLnk<Data>::operator!=(const BinaryTreeLnk<Data>& tree) const noex
template <typename Data> template <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeLnk<Data>::Root() const{ struct BinaryTree<Data>::Node& BinaryTreeLnk<Data>::Root() const{
if(size==0) throw std::length_error("Empty tree!");
return *root; return *root;
} }
template <typename Data> template <typename Data>
void BinaryTreeLnk<Data>::Clear(){ void BinaryTreeLnk<Data>::Clear(){
DeleteTree(root); DeleteTree(root);
root = nullptr;
size = 0;
} }
} }