This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
138 lines (126 loc) · 4.71 KB
/
BST.java
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
import java.text.DecimalFormat;
import java.util.Iterator;
public class BST implements Iterable<Term> {
//initialize private count and root variables
private int count;
private BSTNode root; // middle branch node
public BST() {
this.count = 0;
root = null;
}
public int size() {
return count;
}
public void add(String documentName, String word) {
//adds a new term or inc frequency if it already exists in BST
Term term = new Term(documentName, word);
BSTNode node = new BSTNode(term);
BSTNode current = root;
boolean added = false;
//System.out.println("\nAdding term: " + word);
if (current == null) {
//System.out.println("Replaced root");
root = node;
count++;
added= true;
}
while (!added) {
if (current.getTerm().getName().compareTo(node.getTerm().getName()) == 0) {
//Equal
//System.out.println("Equal terms, adding occurrence");
current.getTerm().addNewOccurrence(documentName);
added = true;
} else if (current.getTerm().getName().compareTo(node.getTerm().getName()) > 0) {
//go left
if (current.getLeft() != null) {
current = current.getLeft();
//System.out.println("Going left");
}
else {
//System.out.println("Adding left node");
current.setLeft(node);
count++;
added = true;
}
} else if (current.getTerm().getName().compareTo(node.getTerm().getName()) < 0) {
//go right
if (current.getRight() != null) {
current = current.getRight();
//System.out.println("Going right");
}
else {
//System.out.println("Adding right node");
current.setRight(node);
count++;
added = true;
}
}
}
}
public Term get(String word, Boolean printDepth) {
// returns the name of the term
// checks to see if printDepth is true and if so then checks to see how deep it exists in the tree
String originalWord = word;
word = word.trim().toLowerCase();
String printOut = "NULL";
DecimalFormat fmt = new DecimalFormat("0.00");
BSTNode current = root;
Term term = new Term(word.toLowerCase().trim());
int depth = 1;
boolean found = false;
while (!found) {
if (current.getTerm().getName().compareTo(word) == 0) {
//Equal
found = true;
String docList = "";
for(Occurrence doc: current.getTerm().getDocsList()) {
Query query = new Query(this);
double TFIDF = query.getTFIDF(current.getTerm().getName(), doc.getDocName());
docList = doc.getDocName() + ": " + fmt.format(TFIDF) +", " + docList;
}
printOut = originalWord + " in pages: " + docList.trim().substring(0, docList.length()-2);
} else if (current.getTerm().getName().compareTo(word) > 0) {
//go left
if (current.getLeft() != null) {
current = current.getLeft();
depth++;
} else {
depth++;
if(printDepth)
System.out.println(" At depth " + depth);
System.out.println(word + " not found");
return null;
}
} else if (current.getTerm().getName().compareTo(word) < 0) {
//go right
if (current.getRight() != null) {
current = current.getRight();
depth++;
} else {
depth++;
if(printDepth)
System.out.println(" At depth " + depth);
System.out.println(originalWord + " not found");
return null;
}
}
}
if (printDepth) {
System.out.println(" At depth " + depth);
}
System.out.println(printOut);
return term;
}
public BSTNode getRoot() {
return root;
}
@Override
public Iterator<Term> iterator() {
return new BSTIterator<Term>(this);
}
public void print(){
for(Term term: this){
System.out.println("Iterator Found: " + term.getName());
}
}
}