I am creating the trie suffix (unfortunately, there is no time to correctly inject the suffix tree) for a 10-character set. The lines I want to parse will be quite long (up to 1 M characters). The tree is designed without any problems, however, I encounter some when I try to free memory after it ends.
In particular, if I configured my constructor and destructor as such (where CNode.child is a pointer to an array of 10 pointers to other CNodes, and count is a simple unsigned int):
CNode::CNode(){ count = 0; child = new CNode* [10]; memset(child, 0, sizeof(CNode*) * 10); } CNode::~CNode(){ for (int i=0; i<10; i++) delete child[i]; }
When I try to remove the root of the node, I get a stack overflow. Maybe I'm wrong, but I'm sure this is due to too many calls to destructors (each destructor calls up to 10 other destructors). I know that this is suboptimal and spatial, and temporary, but it should be a quick and dirty solution to the problem with a repeating substring.
tl; dr : how could one free the memory occupied by a very deep tree?
Thank you for your time.
source share