AVL Tree Output is missing nodes

Hey fellas,

My AVL tree program won't print properly, and I can't figure out why. The program's purpose is to read in words from a file, then insert them into an AVL tree based on their position in the alphabet hierarchy (A-Z). Via debugging, I know the words are being read properly, but I have no idea if the Insert function is actually putting the words into an AVL tree. I would try using non-recursive functions, but part of the program criteria is that it must use recursive functions. I've searched the internet high and low, and over the past couple days have had minimal luck tackling this issue on my own. Your suggestions are greatly appreciated.

Here is the Insert function (where I think the problem is):

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
AVLNode* Insert (string tword, AVLNode*& t)
{
	AVLNode* newNode;
	if (t == NULL)
	{
		newNode = new AVLNode;
		if (newNode == NULL)
		{
			return t;
		}

		newNode -> word = tword;
		newNode -> height = (Height (t)) + 1;
		newNode -> left = NULL;
		newNode -> right = NULL;
		newNode -> counter = 1;
		return newNode;
	}
	else if (tword < t -> word)
	{
		t -> left = Insert (tword, t -> left);
		if (Height (t -> left) - Height (t -> right) == 2)
			{
				if (tword < (t -> left -> word))
				{
					LL (t);
				}
				else
				{
					LR (t);
				}
			}
	}
	else if (tword > t -> word)
	{
		t -> right = Insert (tword, t -> right);
		if (Height (t -> right) - Height (t -> left) == 2)
		{
			if (tword > (t -> right -> word))
			{
				RR (t);
			}
			else
			{
				RL (t);
			}
		}
	}
	else
	{
		(t -> counter) ++;
	}
	t -> height = max (Height (t -> left), Height (t -> right)) + 1;
	return t;

		
}


and here is the entire program:

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Program Purpose:
The purpose of this program is to insert the words inside a file into an AVL tree.  
Once the AVL tree is created, it is printed to visually appear like 
an AVL tree.

*/


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <fstream>

using namespace std;


struct AVLNode	
{
	string word;
	int counter;
	int height;
	AVLNode* left;
	AVLNode* right;
};


int Height (AVLNode* t); //determine the height of the AVL tree
void RR (AVLNode* t);
void LR (AVLNode* t);
void RL (AVLNode* t);
void LL (AVLNode* t);
void CheckBalance (AVLNode* t);
void RebalanceLeft (AVLNode* t);
void RebalanceRight (AVLNode* t);
AVLNode* Insert (string word, AVLNode*& t);
void Print (AVLNode* t, int treeLevel);
AVLNode* TreeRoot (string root);

bool IsAWord (string possible_word);
//function to determine if a string read from file meets the criteria to be called a word
//precondition: a word was read from file, 
//postcondition: the boolean result of whether the string is a word or not 


string LowercaseConvert (string word);
//function to make words with uppercase letters all lowercase
//precondition: a word was read from file, IsAWord was called
//postcondition: a lowercase version of the parameter word is returned 




int main ()
{
	ifstream inputFile;
	string fileName;
	AVLNode* t;

	cout << "please enter a filename (eg: wordCount.gauss)" << endl;
	cin >> fileName;

	inputFile.open (fileName.c_str ());

	string rootWord;
	inputFile >> rootWord;
	string lowerCaseRoot = LowercaseConvert (rootWord);

	t =	TreeRoot (lowerCaseRoot);
	AVLNode* treeRootCopy = t;

	while (!inputFile.eof())
	{
		string possible_word;
		inputFile >> possible_word;

		if (IsAWord (possible_word) == true) //if the word from file matches the definition of a word, make the word
											 //make the word lowercase and insert it into the binary search tree
		{
			string lowerCaseWord = LowercaseConvert (possible_word);
			Insert (lowerCaseWord, t);
		}

	}

	cout << "Resulting Binary Search Tree: " << endl << endl;
	Print (t, 1);

	inputFile.close ();
	return 0;
}

AVLNode* TreeRoot (string root)
{
	AVLNode* t = new AVLNode;
	t -> word = root;
	t -> height = 0;
	t -> left = NULL;
	t -> right = NULL;
	t -> counter = 0;
	return t;
}


AVLNode* Insert (string tword, AVLNode*& t)
{
	AVLNode* newNode;
	if (t == NULL)
	{
		newNode = new AVLNode;
		if (newNode == NULL)
		{
			return t;
		}

		newNode -> word = tword;
		newNode -> height = (Height (t)) + 1;
		newNode -> left = NULL;
		newNode -> right = NULL;
		newNode -> counter = 1;
		return newNode;
	}
	else if (tword < t -> word)
	{
		t -> left = Insert (tword, t -> left);
		if (Height (t -> left) - Height (t -> right) == 2)
			{
				if (tword < (t -> left -> word))
				{
					LL (t);
				}
				else
				{
					LR (t);
				}
			}
	}
	else if (tword > t -> word)
	{
		t -> right = Insert (tword, t -> right);
		if (Height (t -> right) - Height (t -> left) == 2)
		{
			if (tword > (t -> right -> word))
			{
				RR (t);
			}
			else
			{
				RL (t);
			}
		}
	}
	else
	{
		(t -> counter) ++;
	}
	t -> height = max (Height (t -> left), Height (t -> right)) + 1;
	return t;

		
}


int Height (AVLNode* t)
{	
	if (t == NULL)
	{
		return -1;
	}
	else
	{
		return t -> height;
	}
}

void RR (AVLNode* t)
{
	AVLNode* temp1 = t -> right;
	t -> right = temp1 -> left;
	temp1 -> left = t;
	t -> height = max (Height (t -> left), Height (t -> right)) + 1;
	temp1 -> height = max (Height (temp1 -> left), Height (temp1 -> right)) + 1;
	t = temp1;
	return;
}

void LL (AVLNode* t)
{
	AVLNode* temp1 = t -> left;
	t -> left = temp1 -> right;
	temp1 -> right = t;
	t -> height = max (Height (t -> left), Height (t -> right)) + 1;
	temp1 -> height = max (Height (temp1 -> left), Height (temp1 -> right)) + 1;
	t = temp1;
	return;
}

void RL (AVLNode* t)
{
	LL (t -> right);
	RR (t);
	return;
}

void LR (AVLNode* t)
{
	RR (t -> left);
	LL (t);
	return;
}

void CheckBalance (AVLNode* t)
{
	if (Height (t -> left) - Height (t -> right) == 2)
	{
		RebalanceLeft (t);
	}
	else if (Height (t -> right) - Height (t -> left) == 2)
	{
		RebalanceRight (t);
	}
	return;
}

void RebalanceLeft (AVLNode* t)
{
	if (Height (t -> left -> left) - Height (t -> left -> right) == 1)
	{
		LL (t);
	}
	else
	{
		LR (t);
	}
	return;
}

void RebalanceRight (AVLNode* t)
{
	if (Height (t -> right -> right) - Height (t -> right -> left) == 1)
	{
		RR (t);
	}
	else
	{
		RL (t);
	}
	return;
}

void Print (AVLNode* t, int treeLevel)
{
	int i;
	
	if (t == NULL)
	{
		for (i = 0; i < treeLevel; i++)
		{
			cout << "\t";
		}
		cout << "~\n";
		return;
	}
	Print (t -> right, treeLevel + 1);
	
	for (i = 0; i < treeLevel; i++)
	{
		cout << "\t";
	}

	cout << t -> word;

	Print (t -> left, treeLevel + 1);
}



bool IsAWord (string possible_word)
{
	for (unsigned int i = 0; i < possible_word.length (); i++)
	{
		if (!(isalpha (possible_word.at (i)) ) )
		{
			return false;
		}
	}
	return true;
}

string LowercaseConvert (string word)
{
	string lowerCaseWord;
	for (unsigned int i = 0; i < word.length (); i++)
	{
		lowerCaseWord += tolower (word.at (i)); //make the characters of the word lowercase one by one,
											    //then add them together to recreate the word
	}
	return lowerCaseWord;
}





Topic archived. No new replies allowed.