-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeIndex.java
More file actions
77 lines (65 loc) · 2.45 KB
/
Copy pathTreeIndex.java
File metadata and controls
77 lines (65 loc) · 2.45 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
import java.util.*;
import java.io.*;
/**
* Tree Index Implementation
*
* @author Tafita Rakotozandry & Taylor Strong & Khalid Al-Motaery
* @version 11/28/2020
*/
public class TreeIndex extends IndexAbstract implements Index {
private static TreeMap <String, TreeSet <Integer>> treeMap;
/**
* Constructor for the TreeIndex class
*/
public TreeIndex() {
treeMap = new TreeMap <String, TreeSet <Integer>> ();
}
/**
* Creates and writes the output file
*
*/
public void createOutFile() {
try {
//create an output file
File outputFile = new File("TreeIndexOut.txt");
outputFile.createNewFile();
PrintWriter writeFile = new PrintWriter(outputFile);
//create an iterator
Iterator <Map.Entry<String,TreeSet<Integer>>> iterator = treeMap.entrySet().iterator();
Map.Entry <String, TreeSet<Integer>> entry = null;
//go through the treeMap
while (iterator.hasNext()) {
entry = iterator.next();
writeFile.println(entry.getKey() + " " + entry.getValue());
}
//close the write File
writeFile.close();
} catch (Exception e) {
System.out.println("An error has occurred in createOutFile: " + e);
}
}
/**
* Adds each word and line to the TreeMap
*
* @param w the word to enter into the TreeMap
* @param k the line of the word
* @return true or false depending on if the addition was successful or not
*/
public boolean add(String w, Integer k) {
if (!isEnglish(w)) {//return false if the word is not in english
return false;
}
//check if the word is in the treeMap
if (!treeMap.containsKey(w)) { //if the word is not the tree map
treeMap.put(w, new TreeSet <Integer> ()); //Associates the specified word with a new treeset
treeMap.get(w).add(k); //Add the line corresponding with the word
return true;
} else if (treeMap.containsKey(w)) { //if the word is in the tree map already
if (!treeMap.get(w).contains(k)) { //if the current line is not repeated
treeMap.get(w).add(k); //Add the line corresponding with the word
return true;
}
}
return false; // return false if the testing was not successful
}
}