-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
137 lines (118 loc) · 3.55 KB
/
trie.cpp
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
#include <vector>
#include <iostream>
#include <string>
#include <ctype.h>
#include "trie.h"
using namespace std;
/*******************************************************************************
* Impelmentation for a Trie in C++
*
* Created by: SourceTricks
* Modified by: Ross Kwong
* Website: http://www.sourcetricks.com/2011/06/c-tries.html
*
*******************************************************************************/
Node::Node() { mContent = ' ';
mWordMarker = false;
mTravelledFlag = false; }
Node::~Node() {}
char Node::Content() { return mContent; }
void Node::SetContent(char c) { mContent = c; }
bool Node::HasWordMarker() { return mWordMarker; }
void Node::SetWordMarker() { mWordMarker = true; }
void Node::AppendChild(Node* child) { mChildren.push_back(child); }
std::vector<Node*> Node::children() { return mChildren; };
Trie::Trie() { root = new Node(); }
Trie::~Trie() {}
/*
===============================================================================
FindChild()
Finds the Node that contains the specific character and returns it
===============================================================================
*/
Node* Node::FindChild(char c) {
for ( int i = 0; i < mChildren.size(); i++ ) {
Node* tmp = mChildren.at(i);
if ( tmp->Content() == c ) {
return tmp;
}
}
return NULL;
}
/*
===============================================================================
ToUpperCase()
Converts a string to all upper case
===============================================================================
*/
std::string Trie::ToUpperCase(std::string str) {
if (str.length() != 0) {
for (std::string::size_type i = 0; i<str.length(); i++ ) {
str[i] = std::toupper(str[i]);
}
return str;
}
return NULL;
}
/*
===============================================================================
AddWord()
Adds the word into the trie by breaking up the characters into nodes
===============================================================================
*/
void Trie::AddWord(std::string str) {
Node* current = root;
int strLength = str.length();
str = ToUpperCase(str);
if ( strLength == 0 ) {
//current->SetWordMarker(); // an empty word
return;
}
if ( SearchWord(str)==1 ) {
return;
}
for ( int i = 0; i < strLength; i++ ) {
Node* child = current->FindChild(str[i]);
if ( child != NULL ) {
current = child;
} else {
Node* tmp = new Node();
tmp->SetContent(str[i]);
current->AppendChild(tmp);
current = tmp;
}
if ( i == strLength - 1) {
current->SetWordMarker();
}
}
}
/*
===============================================================================
SearchWord()
Searches for the word in the trie and returns a int for each state
0 = String sent has no spelling (i.e. axz vs axe )
1 = String sent is a word
2 = String sent is a word forming, but not a word yet (i.e. Thursda_)
===============================================================================
*/
int Trie::SearchWord(std::string str) {
if (str.length() > 0) {
Node* current = root;
str = ToUpperCase(str);
while ( current != NULL ) {
for ( int i = 0; i < str.length(); i++ ) {
Node* tmp = current->FindChild(str[i]);
if ( tmp == NULL ) {
return 0;
}
current = tmp;
}
if ( current->HasWordMarker() ) {
return 1;
} else {
return 2;
}
}
}
return 0;
}