-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleHeap.java
More file actions
183 lines (152 loc) · 5.33 KB
/
Copy pathArticleHeap.java
File metadata and controls
183 lines (152 loc) · 5.33 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
/* File: ArticleHeap.java
* Name: Theresa McNeil (tnmcneil@bu.edu)
* Purpose: implements a PriorityQueue for articles ordered by cosine similarity
* Date: 12.4.16
*/
public class ArticleHeap{
private static final int SIZE = 2503;
private int next = 0;
private static Article[]A = new Article[SIZE];
private void resize(){
Article [] B = new Article[A.length*2];
for(int i = 0; i < A.length; i++)
B[i] = A[i];
A = B;
}
private int parent(int i) { return (i - 1) / 2; }
private int lchild(int i) { return 2 * i + 1; }
private int rchild(int i) { return 2 * i + 2; }
private boolean isLeaf(int i) { return (lchild(i) >= next); }
private boolean isRoot(int i) { return i == 0; }
private void swap(int i, int j){
Article temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public boolean isEmpty(){
return(next == 0);
}
public int size(){
return next;
}
public boolean member(String title){
int count = 0;
for(int i = 0; i < next; i++){
if(A[i].getTitle().equals(title))
count++;
}
if(count == 0)
return false;
else
return true;
}
// insert an article into array at next available location, ordered by
// cosine similarity
public void insert(Article a){
if(size() == A.length) resize();
A[next] = a;
int i = next;
int p = parent(i);
while(!isRoot(i) && A[i].getCS() > A[p].getCS()){
swap(i,p);
i = p;
p = parent(i);
}
++next;
}
// remove and return the Article in the heap with the largest cosine
// similarity & replace with the last element in level order
public Article getMax() throws HeapUnderflowException{
if(isEmpty())
throw new HeapUnderflowException("Heap is empty!");
else{
--next;
swap(0, next);
int i = 0;
int mc = maxChild(i);
while(!isLeaf(i) && A[i].getCS() < A[mc].getCS()){
swap(i, mc);
i = mc;
mc = maxChild(i);
}
return A[next];
}
}
// return index of Article with max cosineSimilarity of i
// or -1 if i is a leaf node
public int maxChild(int i){
if(lchild(i) >= next)
return -1;
if(rchild(i) >= next)
return lchild(i);
else if(A[lchild(i)].getCS() > A[rchild(i)].getCS())
return lchild(i);
else
return rchild(i);
}
public static void main (String [] args){
System.out.println("Unit Test for Article Heap");
ArticleHeap Heap = new ArticleHeap();
System.err.println("Test 1, testing the empty heap. Should throw " +
"HeapUnderflowException");
System.err.println("Calling getMax() on the empty ArticleHeap should" +
" return: ");
System.err.println("Heap is empty! \nDone");
try{
Article a = Heap.getMax();
System.out.println(a);
}
catch(HeapUnderflowException e){
System.out.println(e.getMessage());
}
finally{
System.out.println("Done");
}
Article A = new Article("A", "A A A");
Article B = new Article("B", "B B B");
Article AB = new Article("AB", "A B A B");
Article ABC = new Article("ABC", "A B C");
A.putCS(0.985);
B.putCS(0.0);
AB.putCS(0.5);
ABC.putCS(0.624);
System.err.println("\nInserting articles A, B, AB and ABC into " +
"the Heap");
System.err.println("A has CS = 0.985, B has CS = 0.0, AB has " +
"CS = 0.5 and ABC has CS = 0.624 (cosine " +
"similarity values chosen arbitrarily and assigned"+
" using putCS()");
Heap.insert(A);
Heap.insert(B);
Heap.insert(AB);
Heap.insert(ABC);
System.err.println("\nTesting member");
System.err.println("member(\"A\") should return true");
System.out.println(Heap.member("A"));
System.err.println("member(\"C\") should return false");
System.out.println(Heap.member("C"));
System.err.println("\nTesting getMax()");
System.err.println("Should return the Articles in the following " +
"order: \nA, ABC, AB, B, and then throw the " +
"HeapUnderflowException");
try{
System.out.print(Heap.getMax());
System.out.print(Heap.getMax());
System.out.print(Heap.getMax());
System.out.print(Heap.getMax());
System.out.print(Heap.getMax());
}
catch(HeapUnderflowException e){
System.out.println(e.getMessage());
}
finally{
System.out.println("Done");
}
}
}
class HeapUnderflowException extends Exception{
public String text;
public HeapUnderflowException(String text){
super(text);
}
}