stdvector - vector bad allocation error c++ -
i have been working on avl tree vector based quite time. i'm suppose take inputs file, on 4118 th input gives me bad_alloc error. did research , gathered inputs have reserve space also. when allocate space, still gives same error.
parts of code:
i call function:
void insert(t d, unsigned int c = 1);
find(t d) finds position of newnode in vector<node<t>*> myvector
; return position if doesn't find newnode. insert take care of returned integer (shown below)
insert is:
template<typename t> void binarytree<t>::insert(t d, unsigned int c) //inserts type t count c vector { node<t>* newnode = new node<t>(d,c); if(myvector.empty()) { myvector.push_back(newnode); } else { int r = find(d); total++; //if newnode has same data data in position r if(r < myvector.size() && myvector[r] && *newnode == *myvector[r]) { myvector[r]->data.loc.push_back(newnode->data.loc[0]); myvector[r]->count += newnode->count; delete newnode; return; } //insert vector else { checkspace(r); myvector[r] = newnode; //reparent(r); } } }
with checkspace being:
template<typename t> void binarytree<t>::checkspace(int i) //resizes vector if needed { if(i >= myvector.size()) { myvector.resize(rightoi(i),null); } }
and void reparent(r) being main function rotate , balancing. commented out reparent(r), , might have isolated problem in insert function. new this, , appreciate help. thank in advance.
rightoi function:
template<typename t> //return right position of int binarytree<t>::rightoi(int i) { return i*2 + 2; }
i may wrong, , offtopic, seems me, vector isn't idea dynamic trees, i'd create tree old-fashioned way, this:
struct node { t value; node* right; node* left; } int main() { node<int>* root = new node<int>(); root->value = 10; root->right = null; root->left = null; node<int>* somenode = new node<int>(); somenode->value = 5; somenode->right = null; somenode->left = null; root->left = somenode; }
so may wrapped functions addelement, rebalance, traverse, delete rules. ask if need more detailed description.
Comments
Post a Comment