bool Page::Balance(int* val, Page* rw) { if (this->keys.size() == 2*_t+1) { *val = this->keys.at(_t); this->keys.erase(keys.begin()+_t); rw->RemoveChildren(0); // after all size is 2*t for (uint i = 0; i < _t ; i++) { rw->AddChildren(this->child.at(_t+1)); this->child.erase(child.begin()+_t + 1); rw->addToKeys(this->keys.at(_t)); this->keys.erase(keys.begin() + _t); } rw->AddChildren(this->child.at(_t+1)); this->child.erase(child.begin()+_t + 1); return true; } else { for (uint i = 0; i < this->child.size(); i++) if (child.at(i) != NULL) { int v=-1; Page* r = new Page(_t); r->RemoveChildren(0); bool isNotBalanced = this->child.at(i)->Balance(&v, r); if (r->GetSize()>0) { this->child.insert(child.begin() + i+1, r); this->keys.insert(keys.begin() + i, v); } if (isNotBalanced) return true; } } return false; }