-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeap.java
285 lines (224 loc) · 7.45 KB
/
MinHeap.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
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
import java.util.*;
public class MinHeap {
// public int[] Heap;
public Node[] Heap;
public int size;
public int maxsize;
public int z=0;
private static final int FRONT = 1;
public MinHeap(int maxsize, String term)
{
this.maxsize = maxsize;
this.size = 0;
Heap = new Node[this.maxsize + 1];
Heap[0] = new Node(Integer.MIN_VALUE,term);
}
public MinHeap(int maxsize, double score)
{
this.maxsize = maxsize;
this.size = 0;
Heap = new Node[this.maxsize + 1];
Heap[0] = new Node(Integer.MIN_VALUE,score);
}
private int parent(int pos)
{
return pos / 2;
}
private int leftChild(int pos)
{
if (2 * pos <= size)
return 2 * pos;
else
return -1;
}
private int rightChild(int pos)
{
if (2 * pos + 1 <= size)
return 2 * pos + 1;
else
return -1;
}
public void removeZscore()
{
for(int i=0; i<size; i++)
{
if(Heap[i].score <= 0.0)
{ Heap[i].score=Integer.MAX_VALUE;
z++;
}
}
}
public void minHeap()
{
for (int pos = (size/2); pos >= 1; pos--)
{ // System.out.println(" Pos :"+pos);
minHeapifyTerms(pos);
}
}
// Function to heapify the node at pos
private void minHeapifyTerms(int pos)
{
// If the node is a non-leaf node and greater
// than any of its child
int l=leftChild(pos);
int r=rightChild(pos);
int smallest=pos;
if ( l >0 && l <=size && Heap[l].docId < Heap[pos].docId)
smallest = l ;
else
smallest = pos;
if ( r >0 && r <= size && Heap[r].docId < Heap[smallest].docId)
smallest = r ;
if (smallest != pos)
{
swap(pos, smallest);
minHeapifyTerms(smallest);
}
}
public void minHeapResult()
{
for (int pos = (size/2); pos >= 1; pos--)
{ // System.out.println(" Pos :"+pos);
minHeapifyResults(pos);
}
}
// Function to heapify the node at pos
private void minHeapifyResults(int pos)
{
// If the node is a non-leaf node and greater
// than any of its child
int l=leftChild(pos);
int r=rightChild(pos);
int smallest=pos;
if ( l >0 && l <=size && Heap[l].score < Heap[pos].score)
smallest = l ;
else
smallest = pos;
if ( r >0 && r <= size && Heap[r].score < Heap[smallest].score)
smallest = r ;
if (smallest != pos)
{
swap(pos, smallest);
minHeapifyTerms(smallest);
}
}
// Function that returns true if the passed
// node is a leaf node
private boolean isLeaf(int pos)
{
if (pos > (size / 2) && pos <= size) {
// System.out.println(Heap[pos].term+" Is Leaf");
return true;
}
return false;
}
public void insertTerms(int docId,String term)
{
if (size >= maxsize)
return;
Heap[++size] = new Node(docId,term);
int current = size;
while (Heap[current].docId < Heap[parent(current)].docId)
{
swap(current, parent(current));
current = parent(current);
}
}
public void insertResult(int docId, double score)
{
if (size >= maxsize)
return;
Heap[++size] = new Node(docId,score);
int current = size;
while (Heap[current].score < Heap[parent(current)].score)
{
swap(current, parent(current));
current = parent(current);
}
}
// Function to swap two nodes of the heap
private void swap(int fpos, int spos)
{ //System.out.println("swapping "+fpos+ " "+spos);
Node tmp;
tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}
// Function to print the contents of the heap
public void print()
{ if(Heap.length > 3)
{ int i=0;
for ( i = 1; i < size / 2; i++)
{
System.out.print(" PARENT : " + Heap[i].docId
+ " LEFT CHILD : " + Heap[2 * i].docId
+ " RIGHT CHILD :" + Heap[2 * i + 1].docId);
}
if(2*i + 1 <= size)
System.out.print(" PARENT : " + Heap[i].docId
+ " LEFT CHILD : " + Heap[2 * i].docId
+ " RIGHT CHILD :" + Heap[2 * i + 1].docId);
else
System.out.print(" PARENT : " + Heap[i].docId
+ " LEFT CHILD : " + Heap[2 * i].docId
+ " RIGHT CHILD : 0 " );
}
else if(Heap.length ==3)
{
System.out.print(" PARENT : " + Heap[1] + " LEFT CHILD : " + Heap[2] + " RIGHT CHILD : 0");
}
else
System.out.println(" PARENT : " + Heap[1]);
System.out.println("\n");
}
// Function to build the min heap using
// the minHeapify
// Driver code
public static void main(String[] args)
{
System.out.println("The Min Heap is of Terms ");
MinHeap minHeap = new MinHeap(5,"terms");
minHeap.insertTerms(5,"s5");
minHeap.insertTerms(3,"s3");
minHeap.insertTerms(4,"s4");
minHeap.insertTerms(1,"s1");
minHeap.insertTerms(2,"s2");
minHeap.print();
for(Node node : minHeap.Heap)
System.out.println(node.docId + " "+ node.term);
minHeap.Heap[1].term="99";
minHeap.Heap[1].docId=99;
minHeap.minHeap();
minHeap.print();
System.out.println("\n");
System.out.println("The Min Heap is of Result");
MinHeap minHeap1 = new MinHeap(6,"results");
minHeap1.insertResult(5,5.89);
minHeap1.insertResult(3,3.9);
minHeap1.insertResult(6,6.2);
minHeap1.insertResult(4,4.2);
minHeap1.insertResult(2,2.2);
minHeap1.insertResult(1,1.2);
for(Node node : minHeap1.Heap)
System.out.println(node.docId + " "+ node.score);
minHeap1.Heap[1].score=99.99;
minHeap1.Heap[1].docId=99;
minHeap1.minHeapResult();
System.out.println("\n");
for(Node node : minHeap1.Heap)
System.out.println(node.docId + " "+ node.score);
minHeap1.print();
}
public int getIndexByTerm(String term) {
for(int i=1; i<size; i++)
if(Heap[i].term.equals(term))
return i;
return -1;
}
public void delete(int position) {
Heap[position].docId = Integer.MAX_VALUE;
swap(position,size);
size--;
minHeap();
}
}