Hullo hullo, I was hoping somebody could check my logic (or point out why my reasoning isn't correct) with the following code.
Most people here would've seen linked lists/doubly-linked lists and the such, well I've gone and made a 2-Dimensional Grid of nodes (each with an Up, Down, Next, and Previous pointer) such that at the edges, it loops back to the beginning of the row or column (depending on the direction being travelled). Pictures are wonderful here. (Attached)
Each square represents a node, and the one with the "O" is the OriginNode (there is a seperate pointer for this, kind of like a "head" or "first" pointer).
Now, I call this thing a Quadtree because I set out to create a Quadtree, but I realized I didn't need a lot of the capabilities of a Quadtree, and this Grid would work better, but I was too lazy to change the name of my functions, so don't be surprised when this looks nothing like a Quadtree (For those of you that remember, I set out to make this a LONG time ago)
Code:
//So, when it's time to destroy my Quadtree, I also want to destroy the Grid
cQUADTREE::~cQUADTREE(void)
{
DestructTree(OriginLeaf, OriginLeaf->Next);
//delete(OriginLeaf); //This is the pickle
}
//The rest is taken of recursively with
void cQUADTREE::DestructTree(cLEAF* LeftEnd, cLEAF* Temp)
{
if(Temp->Next == LeftEnd)
{
if(LeftEnd->Down != OriginLeaf)
{
DestructTree(LeftEnd->Down, LeftEnd->Down->Next);
}
delete(LeftEnd);
LeftEnd = NULL;
}
else
{
DestructTree(LeftEnd, Temp->Next);
}
delete(Temp);
Temp = NULL;
}
Now, I can guarantee the Grid is setup properly since I've tested it by traversing the nuts out of it in all directions. I drew out a 2x2 and 3x3 grid on paper, and followed it through, and just as I thought, all those nodes get deleted.
So, what's the problem?
Well, according to my drawings and whatnot, the OriginNode should be deleted by the recursive function, since "LeftEnd" is initially set to OriginNode, and at the end of each row, LeftEnd is the first thing to be deleted.
However, if I uncomment that bolded line in the destructor and run the program, then when I exit, it exits happily. This should not be happening. The patented Microsoft "You did something unexpected, tell us about it" message box should pop up. But it doesn't.
So, I guess my most immediate question is, if I have already deleted OriginNode, then I should not be allowed to delete it again, right?
If somebody's up for checking the logic behind this all, feel free, but I would really be surprised if there was something wrong there after all these drawings. But, then again, there must be a mistake somewhere.
Thanks for giving this a read, I know it was a heftier one. As always, I'm open to any ideas or suggestions.