Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quadtree deletion

Started by Promit Jun 12, 2004 at 9:35 AM 1 replies 250+ views
Original Post
Promit
Promit
The other day I wrote a quadtree implementation, and instead of writing a DeleteTree() function that recurses, I just added a recursive destructor. I did it in about a minute and didn't really think about it...so I guess I'm just a little paranoid about whether it's actually 'right'.
struct QuadNode
{
	QuadNode* Children[4];
	int NumPatches;
	int* Patches;

	Vector Min, Max, Pos;

	QuadNode() : NumPatches( 0 ), Patches( NULL )
	{
		Children[0] = NULL;
		Children[1] = NULL;
		Children[2] = NULL;
		Children[3] = NULL;
	}

	~QuadNode()
	{
		if( Patches != NULL )
		{
			delete[] Patches;
			Patches = NULL;
		}
		NumPatches = 0;

		for( int i = 0; i < 4; ++i ) 
			if( Children[i] ) 
			{
				delete Children[i];
				Children[i] = NULL;
			}
	}
};

Then when the parent class goes to delete the tree, it simply says delete m_RootNode; and lets the destructor do the rest. I think it's right, but like I said, I'm paranoid, and also a little tired. Does it do what it's supposed to? [edited by - Promit on June 12, 2004 10:49:30 AM]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Aprosenf
Aprosenf
Yes, that''s exactly right. That will completely remove a node and all of its children.
raydog
raydog
Why don''t you put some text file logging in your destructor to find out?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.