Trie Implementation Woes

I'm trying to implement a trie that can print out the frequency of words with a given prefix. Here is what i have so far:
Trie Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Trie
{
public:
	Trie();
	virtual ~Trie();

	//Child nodes of characters from a-z
	Trie *child[26];
	//vector<Trie> child;

	int prefixes_;

	//accessor & mutator functions
	bool GetIsLeaf() { return is_leaf_; }
	void SetIsLeaf(bool val) { is_leaf_ = val; }
	int GetFrequency() { return frequency_; }
	void SetFrequency(int val) { frequency_ = val; }
	int GetPrefixes() { return prefixes_; }
	void SetPrefixes(int val) { prefixes_ = val; }

private:
	bool is_leaf_;
	int frequency_;
};


Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
bool FindWord(string &word, Trie *root)
{
	Trie *new_word = root;
	for(unsigned int i = 0 ; i < word.length(); i++)
	{
		int letter = (int)word[i] - (int)'a';
		if(new_word->child[letter] == nullptr) //if not found
			return false;
		new_word = new_word->child[letter];
	}
	return new_word->GetIsLeaf();
}

void AddWord(string &word, Trie *root)
{
	Trie *new_word = root;
	new_word->prefixes_++;
	for(unsigned int i = 0 ; i < word.length(); i++)
	{
		int letter = (int)word[i] - (int)'a';	//extract character of word
		if(new_word->child[letter] == nullptr)
		{
			new_word->child[letter] = new Trie;
		}

		/*cout << "not value of x: " << new_word->child[letter]->GetPrefixes() << endl;
		int x = (new_word->child[letter]->GetPrefixes())+1;
		cout << "value of x: " << x << endl;
		new_word->child[letter]->SetPrefixes(x);*/
		new_word->child[letter]->prefixes_++;

		new_word = new_word->child[letter];
	}
	new_word->SetFrequency(new_word->GetFrequency()+1);
	/*
	cout << "Word: " << word << endl;
	cout << "frequency: " << new_word->GetFrequency() << endl;
	cout << "prefixes: " << new_word->GetPrefixes() << endl;
	cout << "is leaf: " << new_word->GetIsLeaf() << endl << endl;
	*/
}

int FrequencyCount(string &prefix, Trie *root)
{
	Trie *word = root;
	for(unsigned int i = 0 ; i < prefix.length(); i++)
	{
		int letter = (int)prefix[i] - (int)'a';
		if(word->child[letter] == nullptr) //if not found
			return 0;
		else
			word = word->child[letter];
	}
	return word->GetFrequency();
}

int GetPrefixCount(string &prefix, Trie *root)
{
	Trie *current = root;
	for(unsigned int i = 0; i < prefix.length() ; i++)
	{
		int letter = (int)prefix[i] - (int)'a';
		if(current->child[letter] == nullptr)
			return 0;
		else
			current = current->child[letter];
	}
	return current->prefixes_;

}

//void Trie::AutoComplete(string &prefix, Trie &root)
void AutoComplete(string &prefix, Trie *root, int total)
{

	Trie *current = root;
	for(unsigned int i = 0; i < prefix.length() ; i++)
	{
		int letter = (int)prefix[i] - (int)'a';
		if(current->child[letter] != nullptr)
			current = current->child[letter];
	}

	int frequency = FrequencyCount(prefix, root);
	if (FindWord(prefix,root))
	{
		cout << prefix << " - " << frequency << " occurrences" << endl;
		total = total - frequency;
	}

	if(total != 0)
	{
		//insert code here
	}
	else
	{
		return;
	}

}


When I debug, I get an error in the AddTrie function. Specifically, this line:

new_word->child[letter]->prefixes_++;

In addition, I have no clue how to start with getting the other words. I think the above error has some impact on my results, but I'm not 100% sure. Any advice and information you can give is greatly appreciated!
When I debug, I get an error in the AddTrie function. Specifically, this line:


Sounds like a compiler error, not debugging. What was the exact text of the error - verbatim?

Speaking of debugging, try to use the debugger in your IDE. Create a watch list of variables, step through code 1 line at a time, deduce where things go wrong.
I was able to find the error. It had to deal with me not initializing the children to nullptr. Below is the fixed version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Trie
{
public:
	Trie(): prefixes_(0), is_leaf_(false), frequency_(0)
	{
		for (int i=0; i<26; i++)
		{
			child[i] = nullptr;
		}
	}
	virtual ~Trie();

	//Child nodes of characters from a-z
	Trie *child[26];
	//vector<Trie> child;

	int prefixes_;

	//accessor & mutator functions
	bool GetIsLeaf() { return is_leaf_; }
	void SetIsLeaf(bool val) { is_leaf_ = val; }
	int GetFrequency() { return frequency_; }
	void SetFrequency(int val) { frequency_ = val; }
	int GetPrefixes() { return prefixes_; }
	void SetPrefixes(int val) { prefixes_ = val; }
	bool is_leaf_;

private:
	//bool is_leaf_;
	int frequency_;
};
Topic archived. No new replies allowed.