This commit is contained in:
TUNQRT 2025-03-02 11:09:56 +01:00
parent c65f71e997
commit b82c258399
1 changed files with 15 additions and 12 deletions

View File

@ -87,24 +87,26 @@ bool Tree<T>::del(T val){
template <typename T> template <typename T>
bool Tree<T>::try_to_del(Node_ptr<T>* node, T val){ bool Tree<T>::try_to_del(Node_ptr<T>* node, T val){
if(val == (*node)->value) { if(val == (*node)->value) {
Node<T>* temp = (*node).get(); Node_ptr<T>* temp = node;
std::vector<Node<T>*> path; std::vector<Node_ptr<T>*> path;
if((*node)->small) { if((*node)->small) {
temp = (*node)->small.get();
path.push_back(temp); path.push_back(temp);
std::cout << "pushing " << (*temp)->value << "\n";
temp = &(*node)->small;
} }
else { else {
node->reset((*node)->big.release()); node->reset((*node)->big.release());
return true; return true;
} }
while(temp->big){ while((*temp)->big){
temp = temp->big.get();
path.push_back(temp); path.push_back(temp);
std::cout << "pushing " << (*temp)->value << "\n";
temp = &(*temp)->big;
} }
//mam temp na 'to swap' nodu... //mam temp na 'to swap' nodu...
std::swap((*node)->value, temp->value); std::swap((*node)->value, (*temp)->value);
//temp->steal_values(std::move(temp->small)); temp->reset((*temp)->small.release());
for(int i = path.size() -1; i >= 0; i--) path[i]->update_depth(); for(int i = path.size() -1; i >= 0; i--) (*path[i])->update_depth();
path.clear(); path.clear();
return true; return true;
} }
@ -134,7 +136,8 @@ template <typename T>
void Node<T>::update_depth(){ void Node<T>::update_depth(){
if(small && big) max_depth = std::max(small->max_depth, big->max_depth) + 1; if(small && big) max_depth = std::max(small->max_depth, big->max_depth) + 1;
else if (small) max_depth = small->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 <typename T> template <typename T>
@ -163,7 +166,7 @@ void Tree<T>::print() const {
int main(){ int main(){
Tree<int> t; Tree<int> t;
std::vector<int> v = {3, 6, 8}; std::vector<int> v = {9, 2, 5, 7, 4, 8, -1, 15, 12, 17, 10, 13};
for(auto i : v){ for(auto i : v){
if(t.insert(i)) std::cout << "added " << i << "\n"; if(t.insert(i)) std::cout << "added " << i << "\n";
else std::cout << "failed at adding " << i << "\n"; else std::cout << "failed at adding " << i << "\n";