-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.java
More file actions
58 lines (49 loc) · 1.45 KB
/
Copy pathIndex.java
File metadata and controls
58 lines (49 loc) · 1.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
package proj5;
/**
* The index class represents an ADT that holds words associated with page numbers, similar to the
* index in the back of a book
*/
public class Index {
private BinarySearchTree<IndexEntry> bst;
/**
* Default constructor
*/
public Index() {
bst = new BinarySearchTree<IndexEntry>();
}
/**
* Add a word into the tree.
* If it already exists, update its page number. If too many pages, delete it.
*
* @return if it gets deleted from the index, return false, else return true
*/
public boolean handleWord(String word, int pageNumber) {
IndexEntry entry = new IndexEntry(word);
if(bst.search(entry)) {
//word exists -> update its page number
IndexEntry node = bst.getNode(entry);
boolean added = node.addPage(pageNumber);
if(!added) {
System.out.println("Deleting '" + node.toString() + "' from index");
bst.delete(entry);
return false;
}
} else {
entry.addPage(pageNumber);
bst.insert(entry);
}
return true;
}
/**
* Default toString
* @return the stringified version of the index
*/
public String toString() {
String[] list = bst.toArray();
String ret = "";
for(Object word : list) {
ret += word.toString() + "\n";
}
return ret;
}
}