-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
318 lines (274 loc) · 6.71 KB
/
data.js
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
// Got this here - https://raw.githubusercontent.com/janogonzalez/priorityqueuejs/master/index.js
/**
* Expose `PriorityQueue`.
*/
module.exports.PriorityQueue = PriorityQueue;
/**
* Initializes a new empty `PriorityQueue` with the given `comparator(a, b)`
* function, uses `.DEFAULT_COMPARATOR()` when no function is provided.
*
* The comparator function must return a positive number when `a > b`, 0 when
* `a == b` and a negative number when `a < b`.
*
* @param {Function}
* @return {PriorityQueue}
* @api public
*/
function PriorityQueue(comparator) {
this._comparator = comparator || PriorityQueue.DEFAULT_COMPARATOR;
this._elements = [];
}
/**
* Compares `a` and `b`, when `a > b` it returns a positive number, when
* it returns 0 and when `a < b` it returns a negative number.
*
* @param {String|Number} a
* @param {String|Number} b
* @return {Number}
* @api public
*/
PriorityQueue.DEFAULT_COMPARATOR = function(a, b) {
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
} else {
a = a.toString();
b = b.toString();
if (a == b) return 0;
return (a > b) ? 1 : -1;
}
};
/**
* Returns whether the priority queue is empty or not.
*
* @return {Boolean}
* @api public
*/
PriorityQueue.prototype.isEmpty = function() {
return this.size() === 0;
};
/**
* Peeks at the top element of the priority queue.
*
* @return {Object}
* @throws {Error} when the queue is empty.
* @api public
*/
PriorityQueue.prototype.peek = function() {
if (this.isEmpty()) throw new Error('PriorityQueue is empty');
return this._elements[0];
};
/**
* Dequeues the top element of the priority queue.
*
* @return {Object}
* @throws {Error} when the queue is empty.
* @api public
*/
PriorityQueue.prototype.deq = function() {
var first = this.peek();
var last = this._elements.pop();
var size = this.size();
if (size === 0) return first;
this._elements[0] = last;
var current = 0;
while (current < size) {
var largest = current;
var left = (2 * current) + 1;
var right = (2 * current) + 2;
if (left < size && this._compare(left, largest) >= 0) {
largest = left;
}
if (right < size && this._compare(right, largest) >= 0) {
largest = right;
}
if (largest === current) break;
this._swap(largest, current);
current = largest;
}
return first;
};
/**
* Enqueues the `element` at the priority queue and returns its new size.
*
* @param {Object} element
* @return {Number}
* @api public
*/
PriorityQueue.prototype.enq = function(element) {
var size = this._elements.push(element);
var current = size - 1;
while (current > 0) {
var parent = Math.floor((current - 1) / 2);
if (this._compare(current, parent) <= 0) break;
this._swap(parent, current);
current = parent;
}
return size;
};
/**
* Returns the size of the priority queue.
*
* @return {Number}
* @api public
*/
PriorityQueue.prototype.size = function() {
return this._elements.length;
};
/**
* Iterates over queue elements
*
* @param {Function} fn
*/
PriorityQueue.prototype.forEach = function(fn) {
return this._elements.forEach(fn);
};
/**
* Compares the values at position `a` and `b` in the priority queue using its
* comparator function.
*
* @param {Number} a
* @param {Number} b
* @return {Number}
* @api private
*/
PriorityQueue.prototype._compare = function(a, b) {
return this._comparator(this._elements[a], this._elements[b]);
};
/**
* Swaps the values at position `a` and `b` in the priority queue.
*
* @param {Number} a
* @param {Number} b
* @api private
*/
PriorityQueue.prototype._swap = function(a, b) {
var aux = this._elements[a];
this._elements[a] = this._elements[b];
this._elements[b] = aux;
};
module.exports.List = List;
function List() {
List.makeNode = function() {
return {data: null, next: null};
};
this.start = null;
this.end = null;
this.add = function(data) {
if (this.start === null) {
this.start = List.makeNode();
this.end = this.start;
} else {
this.end.next = List.makeNode();
this.end = this.end.next;
} ;
this.end.data = data;
};
this.delete = function(data) {
var current = this.start;
var previous = this.start;
while (current !== null) {
if (data === current.data) {
if (current === this.start) {
this.start = current.next;
return;
}
if (current === this.end)
this.end = previous;
previous.next = current.next; return;
}
previous = current;
current = current.next;
}
};
this.insertAsFirst = function(d) {
var temp = List.makeNode();
temp.next = this.start;
this.start = temp;
temp.data = d;
};
this.insertAfter = function(t, d) {
var current = this.start;
while (current !== null) {
if (current.data === t) {
var temp = List.makeNode();
temp.data = d;
temp.next = current.next;
if (current === this.end) this.end = temp;
current.next = temp;
return;
}
current = current.next;
}
};
this.item = function(i) {
var current = this.start;
while (current !== null) {
i--;
if (i === 0) return current;
current = current.next;
}
return null;
};
this.each = function(f) {
var current = this.start;
while (current !== null) {
f(current);
current = current.next;
}
};
}
module.exports.Tree = BinaryTree;
function BinaryTree(){
this.Nodes = new Array();
this.level = 0;
this.node = 0;
this.setNode = function(value,level,node){
if (level === undefined) {
this.Nodes[this.btSMF(this.level, this.node)] = value;
}else {
this.Nodes[this.btSMF(level,node)] = value;
}
}
this.getNode = function(level, node){
if (level === undefined) {
return this.Nodes[this.btSMF(this.level,this.node)];
}else {
return this.Nodes[this.btSMF(level,node)];
}
}
this.root = function(value){
this.level = 0;
this.node = 0;
if (value !== undefined) {
this.Nodes[this.btSMF(this.level,this.node)] = value;
}
return this.Nodes[this.btSMF(this.level,this.node)];
}
this.leftChild = function(value){
this.level++;
this.node = this.node * 2;
if (value !== undefined) {
this.Nodes[this.btSMF(this.level,this.node)] = value;
}
return this.Nodes[this.btSMF(this.level,this.node)];
}
this.rightChild = function(value){
this.level++;
this.node = this.node * 2 + 1;
if (value !== undefined) {
this.Nodes[this.btSMF(this.level, this.node)] = value;
}
return this.Nodes[this.btSMF(this.level, this.node)];
}
this.parent = function(value){
this.level--;
this.node = this.node >> 1;
if (value !== undefined) {
this.Nodes[this.btSMF(this.level, this.node)] = value;
}
return this.Nodes[this.btSMF(this.level,this.node)];
}
this.btSMF = function(level, node){
return node + (1 << level) - 1;
}
}