-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboggleutil.cpp
More file actions
55 lines (46 loc) · 1.51 KB
/
boggleutil.cpp
File metadata and controls
55 lines (46 loc) · 1.51 KB
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
#include "boggleutil.h"
#include <iostream>
using namespace std;
void TST::insert(string data, TSTNode *¤t)
{
if (!current) // Create a new node if current is null
current = new TSTNode(data[0]);
if (data[0] > current->data) // Go left
insert(data, current->right);
else if (data[0] < current->data) // Go right
insert(data, current->left);
// First letter of data is equal to current->data, truncate that first letter and go mid.
else if (data.length() > 1)
insert(data.substr(1), current->mid);
else
current->end = true;
}
char TST::find(string data, TSTNode *current)
{
if (!current) // Base case
return 0;
// cout << "Find on current letter: " << current->data << endl;
if (data.length() == 1 && data[0] == current->data)
{
if (!current->end) // The word was a substring but not a full word
return 1;
else // The word was a full word found in the lexicon
return 2;
}
if (data[0] > current->data) // Go right
return find(data, current->right);
else if (data[0] < current->data) // Go left
return find(data, current->left);
else // Letters match, truncate the first letter of data and go mid.
return find(data.substr(1), current->mid);
}
void TST::clear(TSTNode *current)
{
if (!current) // Base case
return;
// Recursively traverse the trie
clear(current->left);
clear(current->mid);
clear(current->right);
delete current;
}