-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject2336F2 (1).java
More file actions
326 lines (260 loc) · 10.9 KB
/
Copy pathProject2336F2 (1).java
File metadata and controls
326 lines (260 loc) · 10.9 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
Project Requirement: Refer to the project document provided with the assignment
Project Description:
Information retrieval systems allow users to enter keywords and retrieve articles that have those keywords associated with them.
For example, once a student named Yi Li wrote a paper called, “Object Class Recognition using Images of Abstract Regions,"
and included the following keywords: `object recognition', `abstract regions', `mixture models', and `EM algorithm'.
If someone does a search for all articles about the EM algorithm, this paper (and many others) will be retrieved.
Implement a binary search tree and use it to store and retrieve articles. The tree will be sorted by keyword, and each node will
contain an unordered linked list of Record objects which contain information about each article that corresponds to that
keyword
datafile contains the following per Article record
Title Id
Title
Author
Number of keys identifier
List of keys in each corresponding article
Keys are inserted into the Binary Search tree using the insert method in the BST class
Each key will reference an unordered linked list of article objects (articleid, titleid, and author)
Algorithm:
- Create a BufferedReader Object to read the text from an Input stream (datafile.txt) by buffering characters that seamlessly
reads lines (characters, arrays or lines).
Note: Each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
It wraps BufferedReader in Java around a java FileReaders (whose read() operations may be costly)
- Loop:
- read titleid, title, author
- create an article object
- read the number of keys identifier
- Loop read number of keys
- insert each key into a BST data structure (BST class will insert the key if not exist)
- add the article object to each of the respective keyword node in the BST data structure
- ouput the resultant BST along with the list of articles per keyword
*/
// Sashank Merugumuvvala
// IDE: OnlineGDB
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Project2336F2 {
public static void main(String args[]) {
BinarySearchTree bst = new BinarySearchTree();
readFileRecords(bst, "datafile.txt");
System.out.println("\t\tWelcome to Information Retrieval System");
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("1. InOrder Traversal with Details <Output keywords along with their associated referenced articles.>");
System.out.println("2. InOrder Traversal (Keywords Only) <Output only the keywords, excluding the referenced articles.>");
System.out.println("3. PreOrder Traversal (Keywords Only) <Output only the keywords in pre-order traversal, without the referenced articles.> ");
System.out.println("4. Search for a specific Keyword <If found, display the keyword with referenced articles; otherwise, output the keyword not found message.>");
System.out.println("5. Exit <Terminates the program.>");
System.out.print("\nEnter a choice? ");
choice = input.nextInt();
switch(choice){
case 1:
bst.inOrderWithDetails();
break;
case 2:
bst.inOrderWithKeywords();
System.out.println();
break;
case 3:
bst.preOrder();
System.out.println();
break;
case 4:
System.out.print("Enter the keyword: ");
String keyword = input.next();
TreeNode<String> result = bst.search(keyword);
if(result != null && keyword.equals(result.element))
{
System.out.println(keyword);
for(Article article : result.head){
System.out.print(article);
}
System.out.println();
}
else{
System.out.println("Database does not exist in the Information Retrieval System!");
System.out.println();
}
break;
}
} while (choice != 5);
}
// method to read files
public static void readFileRecords(BinarySearchTree bst, String filename) {
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new FileReader(filename));
}
catch (IOException e) {
e.printStackTrace();
}
while(true) {
if (fileReader == null) {
System.out.println("Error: file must be opened first!");
break;
}
else {
try {
String strId = fileReader.readLine();
if (strId == null) break;
int id = Integer.parseInt(strId);
String title = fileReader.readLine();
String author = fileReader.readLine();
int numKeys = Integer.parseInt(fileReader.readLine());
String keyword;
Article art;
for (int i=0; i<numKeys; i++) {
keyword = fileReader.readLine();
art = new Article(id, title, author);
bst.insert(keyword, art);
}
fileReader.readLine();
}
catch (NumberFormatException e) {
e.printStackTrace();
break;
}
catch (Exception e) {
e.printStackTrace();
break;
}
}
}
// free up resources
if (fileReader != null) {
try {
fileReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
// BST class
class BinarySearchTree <E extends Comparable<E>> {
protected TreeNode<E> root;
protected int size;
public TreeNode<E> search(E element) {
TreeNode<E> parent = null;
TreeNode<E> current = root;
while (current != null) {
if (element.compareTo(current.element) < 0) {
parent = current;
current = current.leftC;
}
else if (element.compareTo(current.element) > 0) {
parent = current;
current = current.rightC;
}
else if (element.compareTo(current.element) == 0) {
return current;
}
}
return parent;
}
public void insert(E element, Article art)
{
if (root == null) {
root = new TreeNode<>(element);
root.head.addFirst(art);
}
else {
TreeNode<E> parent = search(element);
if (parent != null) {
if (element.compareTo(parent.element) < 0) {
parent.leftC = new TreeNode<>(element);
parent.leftC.head.addFirst(art);
}
else if (element.compareTo(parent.element) > 0) {
parent.rightC = new TreeNode<>(element);
parent.rightC.head.addFirst(art);
}
else {
parent.head.addFirst(art);
}
}
}
size++;
}
// Keyword method
public void inOrderWithKeywords()
{
System.out.println("\n====================================================");
System.out.println("Running InOrder Traversal of the Binary Search tree:");
inOrderWithKeywords(root, "", true);
}
// Key word method
protected void inOrderWithKeywords(TreeNode<E> node, String prefix, boolean isLeft)
{
if (node == null) return;
inOrderWithKeywords(node.leftC, prefix + (isLeft ? " " : " "), true);
System.out.printf("%s%s %s\n", prefix, (isLeft ? "L" : "R"), node.element);
inOrderWithKeywords(node.rightC, prefix + (isLeft ? " " : " "), false);
}
// Method for in order traversal
public void inOrderWithDetails()
{
System.out.println("\n====================================================");
System.out.println("Running InOrder Traversal of the Binary Search tree:");
inOrderWithDetails(root, "", true);
}
protected void inOrderWithDetails(TreeNode<E> node, String prefix, boolean isLeft)
{
if (node == null) return;
inOrderWithDetails(node.leftC, prefix + (isLeft ? " " : " "), true);
System.out.printf("%s%s %s\n", prefix, (isLeft ? "L" : "R"), node.element);
if (node.head.isEmpty()) {
System.out.println(prefix + " No articles found for this keyword.");
} else {
for (Article article : node.head) {
System.out.println(prefix + "" + article);
}
}
inOrderWithDetails(node.rightC, prefix + (isLeft ? " " : " "), false);
}
// preorder traversal method
public void preOrder()
{
System.out.println("\n====================================================");
System.out.println("Running PreOrder Traversal of the Binary Search tree:");
preOrder(root, "", true);
}
protected void preOrder(TreeNode<E> node, String prefix, boolean isLeft)
{
if (node == null) return;
System.out.printf("%s%s %s\n", prefix, (isLeft ? "L" : "R"), node.element);
preOrder(node.leftC, prefix + (isLeft ? " " : " "), true);
preOrder(node.rightC, prefix + (isLeft ? " " : " "), false);
}
}
class TreeNode<E> {
protected E element;
protected TreeNode<E> leftC;
protected TreeNode<E> rightC;
protected LinkedList<Article> head;
public TreeNode(E e) {
element = e;
head = new LinkedList<Article>();
}
}
class Article {
private int id;
private String title;
private String author;
public Article() { }
public Article(int i, String t, String a) {
id =i;
title = t;
author = a;
}
@Override
public String toString() {
return String.format("\t %d | %s | %s-->\n", id, title, author);
}
}