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>
|
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;
|
||||||
}
|
}
|
||||||
else if(val < (*node)->value){
|
else if(val < (*node)->value){
|
||||||
if(!(*node)->small) return false;
|
if( ! (*node)->small) return false;
|
||||||
else {
|
else {
|
||||||
if(try_to_del(&(*node)->small, val)){
|
if(try_to_del(&(*node)->small, val)){
|
||||||
(*node)->update_depth();
|
(*node)->update_depth();
|
||||||
|
@ -119,7 +121,7 @@ bool Tree<T>::try_to_del(Node_ptr<T>* node, T val){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(!(*node)->big) return false;
|
if( ! (*node)->big) return false;
|
||||||
else {
|
else {
|
||||||
if(try_to_del(&(*node)->big, val)){
|
if(try_to_del(&(*node)->big, val)){
|
||||||
(*node)->update_depth();
|
(*node)->update_depth();
|
||||||
|
@ -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";
|
||||||
|
|
Loading…
Reference in New Issue