So this is my class tNode
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class tNode {
public:
tNode( int nodeNumber ) { dNodeNumber = nodeNumber; };
~tNode();
void clearChildren() { possibleChildren.clear(); };
void eraseChild( int index ) { possibleChildren.erase( possibleChildren.begin() + index ); };
void addChild( int newChild ) { possibleChildren.push_back( newChild ); };
void changeChildren( vector<int> possibilities ) { possibleChildren.clear(); possibleChildren = possibilities; };
int dNodeNumber;
vector<int> possibleChildren;
};
|
then along the code i declare a
vector<tNode*> branch;
I created a function to populate the vector
bool createBranch( vector<tNode*>& branch ) {//...}
I insert new elements in branch using the possibleChildren vector. In each iteration i have a series of conditions that populate possibleChildren of the last node in branch and then i create a new node with dNodeNumber equal to the last element of possibleChildren and do possibleChildren.pop_back().
this is what i do to insert a new element in branch:
1 2 3
|
branch.push_back( new tNode(branch[branch.size()-1]->possibleChildren[branch[branch.size()-1]->possibleChildren.size()-1]) );
// basically going to the last element of possibleChildren in the last node of branch
branch[branch.size()-2]->possibleChildren.pop_back();
|
i have a stopping condition to know that branch is complete (and the last element of branch has possibleChildren with size 0 ).
what i intend to do in the next iteration is:
iterate branch from end to begining and remove elements that have possibleChildren with size=0 until the first one that has possibleChildren with size!=0 (thus removing all actions that have no possible action after). then repopulate the branch.
this process would go until only the root node with possibleChildren's size=0 exists in the branch.
so:
populating the branch the first time works (i printed the content of branch and everything was as expected everytime);
this implies repopulating is also working because i use the same function and i tried to manually introduce some random nodes in a branch just to test this and it worked as expected;
what i think is not working is the deletion of elements in branch.
at first i was simply doing
1 2 3 4 5 6 7
|
// Remove elements to construct new branch
for( size_t i=branch.size()-1; i>0; i-- ) {
if( branch[i]->possibleChildren.size() == 0 )
branch.pop_back();
else
break;
}
|
and i think this only removes the element from vector but keeps the allocated space by new inside the createBranch function.
then i started using
1 2 3 4 5 6 7 8 9
|
// Remove elements to construct new branch
for( size_t i=branch.size()-1; i>0; i-- ) {
if( branch[i]->possibleChildren.size() == 0 ) {
delete &branch[branch.size()-1];
branch.pop_back();
}
else
break;
}
|
which supposedly would also delete the allocated space but i get random errors in run time. (sometimes it gives an error and crashes the program, others it works)
Are you suggesting i change this to:
1 2 3 4 5 6 7 8 9
|
// Remove elements to construct new branch
for( size_t i=branch.size()-1; i>0; i-- ) {
if( branch[i]->possibleChildren.size() == 0 ) {
branch[i] = NULL;
branch.pop_back();
}
else
break;
}
|
??