From b82c258399cad1b034d99c4e8444a731e5ff8ae2 Mon Sep 17 00:00:00 2001 From: TUNQRT Date: Sun, 2 Mar 2025 11:09:56 +0100 Subject: [PATCH] bug fix --- tree.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tree.cpp b/tree.cpp index 0c43c17..b5dbe81 100644 --- a/tree.cpp +++ b/tree.cpp @@ -87,29 +87,31 @@ bool Tree::del(T val){ template bool Tree::try_to_del(Node_ptr* node, T val){ if(val == (*node)->value) { - Node* temp = (*node).get(); - std::vector*> path; + Node_ptr* temp = node; + std::vector*> path; if((*node)->small) { - temp = (*node)->small.get(); path.push_back(temp); + std::cout << "pushing " << (*temp)->value << "\n"; + temp = &(*node)->small; } else { node->reset((*node)->big.release()); return true; } - while(temp->big){ - temp = temp->big.get(); + while((*temp)->big){ path.push_back(temp); + std::cout << "pushing " << (*temp)->value << "\n"; + temp = &(*temp)->big; } //mam temp na 'to swap' nodu... - std::swap((*node)->value, temp->value); - //temp->steal_values(std::move(temp->small)); - for(int i = path.size() -1; i >= 0; i--) path[i]->update_depth(); + std::swap((*node)->value, (*temp)->value); + temp->reset((*temp)->small.release()); + for(int i = path.size() -1; i >= 0; i--) (*path[i])->update_depth(); path.clear(); return true; } else if(val < (*node)->value){ - if(!(*node)->small) return false; + if( ! (*node)->small) return false; else { if(try_to_del(&(*node)->small, val)){ (*node)->update_depth(); @@ -119,7 +121,7 @@ bool Tree::try_to_del(Node_ptr* node, T val){ } } else { - if(!(*node)->big) return false; + if( ! (*node)->big) return false; else { if(try_to_del(&(*node)->big, val)){ (*node)->update_depth(); @@ -134,7 +136,8 @@ template void Node::update_depth(){ if(small && big) max_depth = std::max(small->max_depth, big->max_depth) + 1; else if (small) max_depth = small->max_depth + 1; - else max_depth = big->max_depth + 1; + else if (big) max_depth = big->max_depth + 1; + else max_depth = 0; } template @@ -163,7 +166,7 @@ void Tree::print() const { int main(){ Tree t; - std::vector v = {3, 6, 8}; + std::vector v = {9, 2, 5, 7, 4, 8, -1, 15, 12, 17, 10, 13}; for(auto i : v){ if(t.insert(i)) std::cout << "added " << i << "\n"; else std::cout << "failed at adding " << i << "\n";