-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLB.java
More file actions
254 lines (246 loc) · 9.2 KB
/
DLB.java
File metadata and controls
254 lines (246 loc) · 9.2 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
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
/*DLB data structure, this works correctly.
*/
/*
* @author drbickford1
*/
public class DLB implements DictionaryInterface
{
//public methods
protected Node root;
protected Node currNode;
protected Node nodeBefore;
protected int numberOfEntries;
//DLB constructor creating a new node with null values
public DLB()
{
//creating a new root
root = new Node(' ', null, null);
}
/*
*add method. takes a string value and adds it to the DLB
*/
public boolean add(String s)
{
//variables used by this method
char[] input = s.toCharArray();
boolean added = false;
currNode = root;
Node newNode;
//if/else statement to check if the node under the root is null. if it
//is null it will create a new node and set currNode to that node
if(root.getDownNode() == null)
{
//for loop to create new nodes for the first word added
for(int i=0; i<input.length; i++)
{
//creates a new node with the input and sets it to the new node
newNode = new Node(input[0], null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
}
//creates a new node with a null value and sets it to the bottom
//of the last node
newNode = new Node('\0', null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
added = true;
}
//if it is not the first words added it will drop to here
else
{
//setting currNode to the node under the root
currNode = currNode.getDownNode();
//for loop to iterate through the length of the word
for(int i=0; i<input.length; i++)
{
//if/elseif to go through and check certain values to dertermine
//if it needs to be added or not
if(currNode.getData() == input[0] && currNode.getDownNode() != null)
{
//setting the currNode to the node underneith
currNode = currNode.getDownNode();
}
else if(currNode.getData() != input[0] && currNode.getRightNode() != null)
{
//while loop to go through and get the node to the right
while(currNode.getData() != input[0] && currNode.getRightNode() != null)
{
currNode = currNode.getRightNode();
}
if(currNode.getData() != input[0] && currNode.getRightNode() == null)
{
//creating a new node with the input and setting
//currNode to that node
newNode = new Node(input[0], null, null);
numberOfEntries++;
currNode.setRightNode(newNode);
currNode = currNode.getRightNode();
//for loop to iterate through and add a new word
for(int j=i+1; j<input.length-i-1; j++)
{
newNode = new Node(input[j], null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
numberOfEntries++;
}
//creating a null node and setting added to true
newNode = new Node('\0', null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
added = true;
break;
}
else if(currNode.getData() == input[0])
{
//getting the down node if the input is equal
currNode = currNode.getDownNode();
}
}
else if((currNode.getData() != input[0] && currNode.getRightNode() == null))
{
//creating a new node with the data at input 0
newNode = new Node(input[0], null, null);
currNode.setRightNode(newNode);
currNode = currNode.getRightNode();
numberOfEntries++;
//for loop to create a new node for the word
for(int j=i+1; j<input.length-i-1; j++)
{
newNode = new Node(input[j], null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
numberOfEntries++;
}
//creating a new null node for the bottom of the word
newNode = new Node('\0', null, null);
currNode.setDownNode(newNode);
currNode = currNode.getDownNode();
added = true;
break;
}
}
}
//returning added
return added;
}
/* search method. Takes a stringbuilder and searches the tree for that word
*/
public int search(StringBuilder s)
{
//variables used by this method
String searchString = s.toString();
String newString = searchString.toLowerCase();
int type = 0;
char[] searchArray = newString.toCharArray();
currNode = root;
currNode = currNode.getDownNode();
//for loop to iterate through the word and check if it's in the DLB
for(int i=0; i<searchArray.length; i++)
{
//if/elseif to check for certain values
if(currNode.getData() == searchArray[i] && currNode.getDownNode().getData() != '\0' && i < searchArray.length)
{
//getting the down node
currNode = currNode.getDownNode();
type = 1;
}
else if(currNode.getData() == searchArray[i] && currNode.getDownNode().getData() == '\0' && i < searchArray.length)
{
//setting type to 2 and breaking the loop
type = 2;
break;
}
else if(currNode.getData() != searchArray[i] && currNode.getData() != '\0')
{
//while loop to iterate over the searchArray and get the next
//node if it isn't the right character
while(currNode.getData() != searchArray[i])
{
if(currNode.getRightNode() == null && currNode.getData() != searchArray[i])
{
//if it in't found it breaks the loop
type = 0;
break;
}
else
{
//getting the right node
currNode = currNode.getRightNode();
type = 1;
}
}
if(currNode.getDownNode() != null)
{
//sets to the down node
currNode = currNode.getDownNode();
}
else
{
//setting type to 1 and breaking the loop
type = 1;
break;
}
}
else if(currNode.getData() == searchArray[i] && currNode.getDownNode().getData() != '\0' && i < searchArray.length-1)
{
//setting type to 3 and breaking the loop
type = 3;
break;
}
}
//returnign type
return type;
}
/*
*Node class. creates new nodes with given data and set values.
*/
protected class Node
{
//global values
protected char data; // entry in bag
protected Node down; // link to next node
protected Node right;
//Node constructor.
protected Node(char dataPortion)
{
this(dataPortion, null, null);
} // end constructor
//Node constructor setting the data and nodes to valid areas
protected Node(char dataPortion, Node downNode, Node rightNode)
{
data = dataPortion;
down = downNode;
right = rightNode;
} // end constructor
//returns the data that the node holds
protected char getData()
{
return data;
} // end getData
//setting the data the node holds
protected void setData(char newData)
{
data = newData;
} // end setData
//returning the right node
protected Node getRightNode()
{
return right;
} // end getNextNode
//returning the down node
protected Node getDownNode()
{
return down;
}
//setting the right node
protected void setRightNode(Node nextRightNode)
{
right = nextRightNode;
} // end setNextNode
//setting the down node
protected void setDownNode(Node nextDownNode)
{
down = nextDownNode;
}
} // end Node
}