Count is not getting updated

class Node {

public:
char data;
unordered_map<char, Node*> children;
bool terminal;
int count;

Node(char d) {
	data = d;
	terminal = false;
	this->count = 0;
}

};

class Trie {

Node*root;
int cnt;

public:
Trie() {
root = new Node(’\0’);
cnt = 0;
}

void insert(char *w) {

	Node*temp = root;
	for (int i = 0; w[i] != '\0'; i++) {
		char ch = w[i];
		if (temp->children.count(ch)) {
			temp->children[ch]->count += 1;
			temp = temp->children[ch];
		}
		else {
			Node*n = new Node(ch);
			temp->children[ch] = n;
			temp = n;
			temp->count = 1;
		}
	}
	temp->terminal = true;
}

};

the count of nodes isn’t getting incremented

Your given code seems fine to me!
Please share full code on ide.codingblocks.com
just save your code there and share link.


works fine

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.