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>
bool Tree<T>::try_to_del(Node_ptr<T>* node, T val){
if(val == (*node)->value) {
Node<T>* temp = (*node).get();
std::vector<Node<T>*> path;
Node_ptr<T>* temp = node;
std::vector<Node_ptr<T>*> 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;
}
@ -134,7 +136,8 @@ template <typename T>
void Node<T>::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 <typename T>
@ -163,7 +166,7 @@ void Tree<T>::print() const {
int main(){
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){
if(t.insert(i)) std::cout << "added " << i << "\n";
else std::cout << "failed at adding " << i << "\n";