Trie Search Problem

I'm trying to implement a trie that can print out the frequency of words with a given prefix (see autocompelte function). The function seems to show ghost words for some reason, but I don't know why.

Below is the fixed code:


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
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_;
};


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
int main(void)
{
	//Create Root Node
	Trie *Head = new Trie;
	Head->SetFrequency(0);
	Head->SetIsLeaf(false);

	string w1 = "test";
	string w2 = "testing";
	AddWord(w1,Head);
	AddWord(w2,Head);

	string w3 = "testtest";
	AddWord(w3,Head);
	w3 = "testtesting";
	AddWord(w3,Head);
	w3 = "testingtest";
	AddWord(w3,Head);
	 w3 = "testingtesting";
	AddWord(w3,Head);


	string word = "testing";
	if(FindWord(w2,Head))
		cout << "The word \"" << w2 << "\" has been found!" << endl;
	else
		cout << "The word \"" << w2 << "\" was not found!" << endl;

	cout << "Prefix: " << w1 << endl;
	cout << "-------" << endl;
	string w_backup = w1;
	AutoComplete(w1, Head, GetPrefixCount(w1,Head), w_backup, 0);
	cout << endl;

	cout << "Prefix: " << w2 << endl;
	cout << "-------" << endl;
	w_backup = w2;
	AutoComplete(w2, Head, GetPrefixCount(w2,Head), w_backup, 0);

	return 0;
}
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->SetFrequency(new_word->GetPrefixes()+1);
	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;
		}

		new_word->child[letter]->SetPrefixes(new_word->child[letter]->GetPrefixes()+1);

		new_word = new_word->child[letter];
	}
	new_word->SetIsLeaf(true);
	new_word->SetFrequency(new_word->GetFrequency()+1);
	//Debugging Purposes
	/*
	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->GetPrefixes();

}

void AutoComplete(string &prefix, Trie *root, int total, string origional_prefix, int counter)
{

	if (counter == 0 || FindWord(origional_prefix,root))
	{
		//cout << "current word is: " << prefix << endl;
		int frequency = FrequencyCount(origional_prefix, root);
		cout << prefix << " - " << frequency << " occurrences" << endl;
		total = total - root->GetFrequency();
		counter++;
	}
	else
	{
		//cout << "word not found" << endl;
		if (root->GetFrequency() > 0)
		{
			total = total - root->GetFrequency();
			if (prefix.size() != 2*origional_prefix.size() && total != 0)
				cout << prefix.substr(origional_prefix.size(),prefix.size()) << " - " << root->GetFrequency() << " occurrences" << endl;

		}

		if (root->GetFrequency() < 1)
		{
			//cout << "frequency is less than 1!" << endl;
			total = total - root->GetFrequency();

		}
		else
			total = total - root->GetFrequency();
	}
	cout << "current total is: " << total << endl;


	if(total != 0 && total > 0)
	{
		for (int i = 0; i < 26; i++)
		{
			Trie *pointerChild = root->child[i];
			if(pointerChild)
			{
				char nextChar = 'a'+i;
				prefix.push_back(nextChar);
				AutoComplete(prefix,pointerChild,total, origional_prefix, counter);
				prefix.pop_back();
			}
		}
	}
	else
	{
		return;
	}
}


Here is the console output (added cout statements to show bug):
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
The word "testing" has been found!
Prefix: test
--
test - 1 occurrences
current total is: 5
current total is: 5
current total is: 5
current total is: 5
testtest - 1 occurrences
current total is: 4
current total is: 4
current total is: 4
testtesting - 1 occurrences
current total is: 3
current total is: 3
current total is: 3
current total is: 3
testingtest - 1 occurrences
current total is: 1
current total is: 1
current total is: 1
current total is: -1
current total is: 4
current total is: 4
current total is: 4
testtest - 1 occurrences
current total is: 2
current total is: 2
current total is: 2
testtesting - 1 occurrences
current total is: 0

Prefix: testing
--
testing - 1 occurrences
current total is: 2
current total is: 2
current total is: 2
current total is: 2
testingtest - 1 occurrences
current total is: 1
current total is: 1
current total is: 1
testingtesting - 1 occurrences
current total is: 0
current total is: 1
current total is: 1
current total is: 1
current total is: -1

Process returned 0 (0x0)   execution time : 2.920 s
Press any key to continue.


I don't know for sure where the bug is coming from, but I know it has something to do with my autocomplete function or maybe it is because I am missing a copy constructor. If you can help me out, I would appreciate it.

Last edited on
Its difficult to go over 240+ lines of code to answer your query re a single point, you need to do some work first to present your problem in a easy to get manner. Remove everything superfluous and just post the bare essence of the problem and that way you may get some useful replies
Topic archived. No new replies allowed.