How does trie tree work in case of more than one test case?

Revision en1, by SAeed, 2016-03-02 22:07:55

Hello everyone !! I have this implementation for trie tree :

const int MAX_CHAR = 10;
struct trie {
	trie* child[MAX_CHAR];
	bool isLeaf;

	trie() {
            memset(child, 0, sizeof(child));
            isLeaf = 0;
	}

	void insert(char *str) {
		if(*str == '\0')
			isLeaf = 1;
		else {
			int cur = *str - '0';
			if(child[cur] == 0)
				child[cur] = new trie();
			child[cur]->insert(str+1);
		}
	}
};

I was wondering, what will happen in case of more than one test case? I know that C++ doesn't support garbage collector.

So, do I need to add the following destructor:

~trie(){
            for(int i = 0; i < 10; i++) delete child[i];
        }

Or can C++ delete this tree? I know bool isLeaf will be deleted at root node, and so is trie* child[MAX_CHAR];, but I'm not sure about the remaining part of my trie (the part where child array is pointing to).

Or may be there is another way of deleting this trie?

The reason I'm asking is because I'm afraid of MLE with a big number of test cases. Can any one help?

Tags trie tree

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English SAeed 2016-03-02 22:09:29 2 Tiny change: ' MLE with a big numbe' -> ' MLE with big numbe'
en2 English SAeed 2016-03-02 22:08:34 2 Tiny change: 'ryone !!\nI have t' -> 'ryone !!\n\nI have t'
en1 English SAeed 2016-03-02 22:07:55 1154 Initial revision (published)