bug fix
This commit is contained in:
parent
c65f71e997
commit
b82c258399
27
tree.cpp
27
tree.cpp
|
@ -87,29 +87,31 @@ 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;
|
||||
}
|
||||
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<T>::try_to_del(Node_ptr<T>* 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 <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";
|
||||
|
|
Loading…
Reference in New Issue