splaytree 3.1.1 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +58 -57
- package/dist/splaytree.js +344 -0
- package/dist/splaytree.js.map +1 -0
- package/dist/splaytree.umd.cjs +3 -0
- package/dist/splaytree.umd.cjs.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/package.json +33 -62
- package/src/index.ts +211 -194
- package/dist/splay.esm.js +0 -673
- package/dist/splay.esm.js.map +0 -1
- package/dist/splay.js +0 -681
- package/dist/splay.js.map +0 -1
- package/typings/index.d.ts +0 -82
- package/typings/node.d.ts +0 -8
- package/typings/types.d.ts +0 -2
package/src/index.ts
CHANGED
|
@@ -1,61 +1,62 @@
|
|
|
1
|
-
import Node from
|
|
2
|
-
import { Key, Value } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export type Comparator<Key> = (a:Key, b:Key) => number;
|
|
6
|
-
export type Visitor<Key, Value> = (node:Node<Key, Value>) => void;
|
|
7
|
-
export type NodePrinter<Key, Value> = (node:Node<Key, Value>) => string;
|
|
1
|
+
import Node from "./node";
|
|
2
|
+
import { Key, Value } from "./types";
|
|
8
3
|
|
|
4
|
+
export type Comparator<Key> = (a: Key, b: Key) => number;
|
|
5
|
+
export type Visitor<Key, Value> = (
|
|
6
|
+
node: Node<Key, Value>
|
|
7
|
+
) => undefined | boolean | void;
|
|
8
|
+
export type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;
|
|
9
9
|
|
|
10
10
|
/* follows "An implementation of top-down splaying"
|
|
11
11
|
* by D. Sleator <sleator@cs.cmu.edu> March 1992
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
function DEFAULT_COMPARE (a:Key, b:Key) : number {
|
|
14
|
+
function DEFAULT_COMPARE(a: Key, b: Key): number {
|
|
16
15
|
return a > b ? 1 : a < b ? -1 : 0;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
type TreeNodeList<Key, Value> = { head:Node<Key, Value>|null };
|
|
21
|
-
|
|
18
|
+
type TreeNodeList<Key, Value> = { head: Node<Key, Value> | null };
|
|
22
19
|
|
|
23
20
|
/**
|
|
24
21
|
* Simple top down splay, not requiring i to be in the tree t.
|
|
25
22
|
*/
|
|
26
|
-
function splay
|
|
23
|
+
function splay(
|
|
24
|
+
i: Key,
|
|
25
|
+
t: Node<Key, Value>,
|
|
26
|
+
comparator: Comparator<Key>
|
|
27
|
+
): Node<Key, Value> {
|
|
27
28
|
const N = new Node(null, null);
|
|
28
29
|
let l = N;
|
|
29
30
|
let r = N;
|
|
30
31
|
|
|
31
32
|
while (true) {
|
|
32
|
-
const cmp = comparator(i, t
|
|
33
|
+
const cmp = comparator(i, t!.key);
|
|
33
34
|
//if (i < t.key) {
|
|
34
35
|
if (cmp < 0) {
|
|
35
|
-
if (t
|
|
36
|
+
if (t!.left === null) break;
|
|
36
37
|
//if (i < t.left.key) {
|
|
37
38
|
if (comparator(i, t.left.key) < 0) {
|
|
38
|
-
const y = t.left;
|
|
39
|
+
const y = t.left; /* rotate right */
|
|
39
40
|
t.left = y.right;
|
|
40
41
|
y.right = t;
|
|
41
42
|
t = y;
|
|
42
43
|
if (t.left === null) break;
|
|
43
44
|
}
|
|
44
|
-
r.left = t;
|
|
45
|
+
r.left = t; /* link right */
|
|
45
46
|
r = t;
|
|
46
47
|
t = t.left;
|
|
47
|
-
|
|
48
|
+
//} else if (i > t.key) {
|
|
48
49
|
} else if (cmp > 0) {
|
|
49
50
|
if (t.right === null) break;
|
|
50
51
|
//if (i > t.right.key) {
|
|
51
52
|
if (comparator(i, t.right.key) > 0) {
|
|
52
|
-
const y = t.right;
|
|
53
|
+
const y = t.right; /* rotate left */
|
|
53
54
|
t.right = y.left;
|
|
54
55
|
y.left = t;
|
|
55
56
|
t = y;
|
|
56
57
|
if (t.right === null) break;
|
|
57
58
|
}
|
|
58
|
-
l.right = t;
|
|
59
|
+
l.right = t; /* link left */
|
|
59
60
|
l = t;
|
|
60
61
|
t = t.right;
|
|
61
62
|
} else break;
|
|
@@ -68,12 +69,12 @@ function splay (i:Key, t:Node<Key, Value>|null, comparator:Comparator<Key>) : No
|
|
|
68
69
|
return t;
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
t:Node<Key, Value>,
|
|
75
|
-
comparator:Comparator<Key
|
|
76
|
-
)
|
|
72
|
+
function insert(
|
|
73
|
+
i: Key,
|
|
74
|
+
data: Value,
|
|
75
|
+
t: Node<Key, Value>,
|
|
76
|
+
comparator: Comparator<Key>
|
|
77
|
+
): Node<Key, Value> {
|
|
77
78
|
const node = new Node(i, data);
|
|
78
79
|
|
|
79
80
|
if (t === null) {
|
|
@@ -95,10 +96,13 @@ function insert (
|
|
|
95
96
|
return node;
|
|
96
97
|
}
|
|
97
98
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
function split(
|
|
100
|
+
key: Key,
|
|
101
|
+
v: Node<Key, Value>,
|
|
102
|
+
comparator: Comparator<Key>
|
|
103
|
+
): {
|
|
104
|
+
left: Node<Key, Value> | null;
|
|
105
|
+
right: Node<Key, Value> | null;
|
|
102
106
|
} {
|
|
103
107
|
let left = null;
|
|
104
108
|
let right = null;
|
|
@@ -107,82 +111,75 @@ function split (key:Key, v:Node<Key, Value>, comparator:Comparator<Key>) : {
|
|
|
107
111
|
|
|
108
112
|
const cmp = comparator(v.key, key);
|
|
109
113
|
if (cmp === 0) {
|
|
110
|
-
left
|
|
114
|
+
left = v.left;
|
|
111
115
|
right = v.right;
|
|
112
116
|
} else if (cmp < 0) {
|
|
113
|
-
right
|
|
117
|
+
right = v.right;
|
|
114
118
|
v.right = null;
|
|
115
|
-
left
|
|
119
|
+
left = v;
|
|
116
120
|
} else {
|
|
117
|
-
left
|
|
121
|
+
left = v.left;
|
|
118
122
|
v.left = null;
|
|
119
|
-
right
|
|
123
|
+
right = v;
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
126
|
return { left, right };
|
|
123
127
|
}
|
|
124
128
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
comparator:Comparator<Key>,
|
|
129
|
+
function merge(
|
|
130
|
+
left: Node<Key, Value> | null,
|
|
131
|
+
right: Node<Key, Value> | null,
|
|
132
|
+
comparator: Comparator<Key>
|
|
130
133
|
) {
|
|
131
134
|
if (right === null) return left;
|
|
132
|
-
if (left
|
|
135
|
+
if (left === null) return right;
|
|
133
136
|
|
|
134
137
|
right = splay(left.key, right, comparator);
|
|
135
138
|
right.left = left;
|
|
136
139
|
return right;
|
|
137
140
|
}
|
|
138
141
|
|
|
139
|
-
|
|
140
|
-
type StringCollector = (s:string) => void;
|
|
141
|
-
|
|
142
|
+
type StringCollector = (s: string) => void;
|
|
142
143
|
|
|
143
144
|
/**
|
|
144
145
|
* Prints level of the tree
|
|
145
146
|
*/
|
|
146
|
-
function printRow
|
|
147
|
-
root:Node<Key, Value>,
|
|
148
|
-
prefix:string,
|
|
149
|
-
isTail:boolean,
|
|
150
|
-
out:StringCollector,
|
|
151
|
-
printNode:NodePrinter<Key, Value
|
|
147
|
+
function printRow(
|
|
148
|
+
root: Node<Key, Value>,
|
|
149
|
+
prefix: string,
|
|
150
|
+
isTail: boolean,
|
|
151
|
+
out: StringCollector,
|
|
152
|
+
printNode: NodePrinter<Key, Value>
|
|
152
153
|
) {
|
|
153
154
|
if (root) {
|
|
154
|
-
out(`${
|
|
155
|
-
const indent = prefix + (isTail ?
|
|
156
|
-
if (root.left)
|
|
157
|
-
if (root.right) printRow(root.right, indent, true,
|
|
155
|
+
out(`${prefix}${isTail ? "└── " : "├── "}${printNode(root)}\n`);
|
|
156
|
+
const indent = prefix + (isTail ? " " : "│ ");
|
|
157
|
+
if (root.left) printRow(root.left, indent, false, out, printNode);
|
|
158
|
+
if (root.right) printRow(root.right, indent, true, out, printNode);
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
export default class Tree<Key = number, Value = any> {
|
|
163
|
+
private _comparator: Comparator<Key>;
|
|
164
|
+
private _root: Node<Key, Value> | null = null;
|
|
165
|
+
private _size: number = 0;
|
|
161
166
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
private _comparator:Comparator<Key>;
|
|
165
|
-
private _root:Node<Key, Value>|null = null;
|
|
166
|
-
private _size:number = 0;
|
|
167
|
-
|
|
168
|
-
constructor (comparator = DEFAULT_COMPARE) {
|
|
167
|
+
constructor(comparator = DEFAULT_COMPARE) {
|
|
169
168
|
this._comparator = comparator;
|
|
170
169
|
}
|
|
171
170
|
|
|
172
|
-
|
|
173
171
|
/**
|
|
174
172
|
* Inserts a key, allows duplicates
|
|
175
173
|
*/
|
|
176
|
-
public insert
|
|
174
|
+
public insert(key: Key, data?: Value): Node<Key, Value> {
|
|
177
175
|
this._size++;
|
|
178
|
-
return this._root = insert(key, data, this._root
|
|
176
|
+
return (this._root = insert(key, data, this._root!, this._comparator));
|
|
179
177
|
}
|
|
180
178
|
|
|
181
|
-
|
|
182
179
|
/**
|
|
183
180
|
* Adds a key, if it is not present in the tree
|
|
184
181
|
*/
|
|
185
|
-
public add
|
|
182
|
+
public add(key: Key, data?: Value): Node<Key, Value> {
|
|
186
183
|
const node = new Node(key, data);
|
|
187
184
|
|
|
188
185
|
if (this._root === null) {
|
|
@@ -212,29 +209,26 @@ export default class Tree<Key=number, Value=any> {
|
|
|
212
209
|
return this._root;
|
|
213
210
|
}
|
|
214
211
|
|
|
215
|
-
|
|
216
212
|
/**
|
|
217
213
|
* @param {Key} key
|
|
218
214
|
* @return {Node|null}
|
|
219
215
|
*/
|
|
220
|
-
public remove
|
|
221
|
-
this._root = this._remove(key, this._root
|
|
216
|
+
public remove(key: Key): void {
|
|
217
|
+
this._root = this._remove(key, this._root!, this._comparator);
|
|
222
218
|
}
|
|
223
219
|
|
|
224
|
-
|
|
225
220
|
/**
|
|
226
221
|
* Deletes i from the tree if it's there
|
|
227
222
|
*/
|
|
228
|
-
private _remove
|
|
229
|
-
i:Key, t:Node<Key, Value>,
|
|
230
|
-
comparator:Comparator<Key>) : Node<Key, Value> {
|
|
223
|
+
private _remove(i: Key, t: Node<Key, Value>, comparator: Comparator<Key>) {
|
|
231
224
|
let x;
|
|
232
225
|
if (t === null) return null;
|
|
233
226
|
t = splay(i, t, comparator);
|
|
234
227
|
const cmp = comparator(i, t.key);
|
|
235
|
-
if (cmp === 0) {
|
|
228
|
+
if (cmp === 0) {
|
|
229
|
+
/* found it */
|
|
236
230
|
if (t.left === null) {
|
|
237
|
-
x = t.right
|
|
231
|
+
x = t.right!;
|
|
238
232
|
} else {
|
|
239
233
|
x = splay(i, t.left, comparator);
|
|
240
234
|
x.right = t.right;
|
|
@@ -242,42 +236,39 @@ export default class Tree<Key=number, Value=any> {
|
|
|
242
236
|
this._size--;
|
|
243
237
|
return x;
|
|
244
238
|
}
|
|
245
|
-
return t;
|
|
239
|
+
return t; /* It wasn't there */
|
|
246
240
|
}
|
|
247
241
|
|
|
248
|
-
|
|
249
242
|
/**
|
|
250
243
|
* Removes and returns the node with smallest key
|
|
251
244
|
*/
|
|
252
|
-
public pop
|
|
245
|
+
public pop(): { key: Key; data: Value } | null {
|
|
253
246
|
let node = this._root;
|
|
254
247
|
if (node) {
|
|
255
248
|
while (node.left) node = node.left;
|
|
256
|
-
this._root = splay(node.key,
|
|
249
|
+
this._root = splay(node.key, this._root!, this._comparator);
|
|
257
250
|
this._root = this._remove(node.key, this._root, this._comparator);
|
|
258
251
|
return { key: node.key, data: node.data };
|
|
259
252
|
}
|
|
260
253
|
return null;
|
|
261
254
|
}
|
|
262
255
|
|
|
263
|
-
|
|
264
256
|
/**
|
|
265
257
|
* Find without splaying
|
|
266
258
|
*/
|
|
267
|
-
public findStatic
|
|
268
|
-
let current
|
|
259
|
+
public findStatic(key: Key): Node<Key, Value> | null {
|
|
260
|
+
let current = this._root;
|
|
269
261
|
const compare = this._comparator;
|
|
270
262
|
while (current) {
|
|
271
263
|
const cmp = compare(key, current.key);
|
|
272
|
-
if (cmp === 0)
|
|
264
|
+
if (cmp === 0) return current;
|
|
273
265
|
else if (cmp < 0) current = current.left;
|
|
274
|
-
else
|
|
266
|
+
else current = current.right;
|
|
275
267
|
}
|
|
276
268
|
return null;
|
|
277
269
|
}
|
|
278
270
|
|
|
279
|
-
|
|
280
|
-
public find (key:Key) : Node<Key, Value>|null {
|
|
271
|
+
public find(key: Key): Node<Key, Value> | null {
|
|
281
272
|
if (this._root) {
|
|
282
273
|
this._root = splay(key, this._root, this._comparator);
|
|
283
274
|
if (this._comparator(key, this._root.key) !== 0) return null;
|
|
@@ -285,32 +276,30 @@ export default class Tree<Key=number, Value=any> {
|
|
|
285
276
|
return this._root;
|
|
286
277
|
}
|
|
287
278
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
let current = this._root;
|
|
279
|
+
public contains(key: Key): boolean {
|
|
280
|
+
let current = this._root;
|
|
291
281
|
const compare = this._comparator;
|
|
292
282
|
while (current) {
|
|
293
283
|
const cmp = compare(key, current.key);
|
|
294
|
-
if (cmp === 0)
|
|
284
|
+
if (cmp === 0) return true;
|
|
295
285
|
else if (cmp < 0) current = current.left;
|
|
296
|
-
else
|
|
286
|
+
else current = current.right;
|
|
297
287
|
}
|
|
298
288
|
return false;
|
|
299
289
|
}
|
|
300
290
|
|
|
301
|
-
|
|
302
|
-
public forEach (visitor:Visitor<Key, Value>, ctx?:any) : Tree<Key, Value> {
|
|
291
|
+
public forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value> {
|
|
303
292
|
let current = this._root;
|
|
304
|
-
const Q = [];
|
|
293
|
+
const Q = []; /* Initialize stack s */
|
|
305
294
|
let done = false;
|
|
306
295
|
|
|
307
296
|
while (!done) {
|
|
308
|
-
if (current !==
|
|
297
|
+
if (current !== null) {
|
|
309
298
|
Q.push(current);
|
|
310
299
|
current = current.left;
|
|
311
300
|
} else {
|
|
312
301
|
if (Q.length !== 0) {
|
|
313
|
-
current = Q.pop()
|
|
302
|
+
current = Q.pop()!;
|
|
314
303
|
visitor.call(ctx, current);
|
|
315
304
|
|
|
316
305
|
current = current.right;
|
|
@@ -320,11 +309,15 @@ export default class Tree<Key=number, Value=any> {
|
|
|
320
309
|
return this;
|
|
321
310
|
}
|
|
322
311
|
|
|
323
|
-
|
|
324
312
|
/**
|
|
325
313
|
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
|
|
326
314
|
*/
|
|
327
|
-
public range
|
|
315
|
+
public range(
|
|
316
|
+
low: Key,
|
|
317
|
+
high: Key,
|
|
318
|
+
fn: Visitor<Key, Value>,
|
|
319
|
+
ctx?: any
|
|
320
|
+
): Tree<Key, Value> {
|
|
328
321
|
const Q = [];
|
|
329
322
|
const compare = this._comparator;
|
|
330
323
|
let node = this._root;
|
|
@@ -335,7 +328,7 @@ export default class Tree<Key=number, Value=any> {
|
|
|
335
328
|
Q.push(node);
|
|
336
329
|
node = node.left;
|
|
337
330
|
} else {
|
|
338
|
-
node = Q.pop()
|
|
331
|
+
node = Q.pop()!;
|
|
339
332
|
cmp = compare(node.key, high);
|
|
340
333
|
if (cmp > 0) {
|
|
341
334
|
break;
|
|
@@ -348,55 +341,52 @@ export default class Tree<Key=number, Value=any> {
|
|
|
348
341
|
return this;
|
|
349
342
|
}
|
|
350
343
|
|
|
351
|
-
|
|
352
344
|
/**
|
|
353
345
|
* Returns array of keys
|
|
354
346
|
*/
|
|
355
|
-
public keys
|
|
356
|
-
const keys:Key[] = [];
|
|
357
|
-
this.forEach(({ key }) =>
|
|
347
|
+
public keys(): Key[] {
|
|
348
|
+
const keys: Key[] = [];
|
|
349
|
+
this.forEach(({ key }) => {
|
|
350
|
+
keys.push(key);
|
|
351
|
+
});
|
|
358
352
|
return keys;
|
|
359
353
|
}
|
|
360
354
|
|
|
361
|
-
|
|
362
355
|
/**
|
|
363
356
|
* Returns array of all the data in the nodes
|
|
364
357
|
*/
|
|
365
|
-
public values
|
|
366
|
-
const values:Value[] = [];
|
|
367
|
-
this.forEach(({ data }) =>
|
|
358
|
+
public values(): Value[] {
|
|
359
|
+
const values: Value[] = [];
|
|
360
|
+
this.forEach(({ data }) => {
|
|
361
|
+
values.push(data);
|
|
362
|
+
});
|
|
368
363
|
return values;
|
|
369
364
|
}
|
|
370
365
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
if (this._root) return this.minNode(this._root).key;
|
|
366
|
+
public min(): Key | null {
|
|
367
|
+
if (this._root) return this.minNode(this._root)!.key;
|
|
374
368
|
return null;
|
|
375
369
|
}
|
|
376
370
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if (this._root) return this.maxNode(this._root).key;
|
|
371
|
+
public max(): Key | null {
|
|
372
|
+
if (this._root) return this.maxNode(this._root)!.key;
|
|
380
373
|
return null;
|
|
381
374
|
}
|
|
382
375
|
|
|
383
|
-
|
|
384
|
-
public minNode(t = this._root) : Node<Key, Value> | null {
|
|
376
|
+
public minNode(t = this._root): Node<Key, Value> | null {
|
|
385
377
|
if (t) while (t.left) t = t.left;
|
|
386
378
|
return t;
|
|
387
379
|
}
|
|
388
380
|
|
|
389
|
-
|
|
390
|
-
public maxNode(t = this._root) : Node<Key, Value> | null {
|
|
381
|
+
public maxNode(t = this._root): Node<Key, Value> | null {
|
|
391
382
|
if (t) while (t.right) t = t.right;
|
|
392
383
|
return t;
|
|
393
384
|
}
|
|
394
385
|
|
|
395
|
-
|
|
396
386
|
/**
|
|
397
387
|
* Returns node at given index
|
|
398
388
|
*/
|
|
399
|
-
public at
|
|
389
|
+
public at(index: number): Node<Key, Value> | null {
|
|
400
390
|
let current = this._root;
|
|
401
391
|
let done = false;
|
|
402
392
|
let i = 0;
|
|
@@ -408,18 +398,17 @@ export default class Tree<Key=number, Value=any> {
|
|
|
408
398
|
current = current.left;
|
|
409
399
|
} else {
|
|
410
400
|
if (Q.length > 0) {
|
|
411
|
-
current = Q.pop()
|
|
401
|
+
current = Q.pop()!;
|
|
412
402
|
if (i === index) return current;
|
|
413
403
|
i++;
|
|
414
|
-
current = current.right
|
|
404
|
+
current = current.right!;
|
|
415
405
|
} else done = true;
|
|
416
406
|
}
|
|
417
407
|
}
|
|
418
408
|
return null;
|
|
419
409
|
}
|
|
420
410
|
|
|
421
|
-
|
|
422
|
-
public next (d:Node<Key, Value>) : Node<Key, Value>|null {
|
|
411
|
+
public next(d: Node<Key, Value>): Node<Key, Value> | null {
|
|
423
412
|
let root = this._root;
|
|
424
413
|
let successor = null;
|
|
425
414
|
|
|
@@ -442,8 +431,7 @@ export default class Tree<Key=number, Value=any> {
|
|
|
442
431
|
return successor;
|
|
443
432
|
}
|
|
444
433
|
|
|
445
|
-
|
|
446
|
-
public prev (d:Node<Key, Value>) : Node<Key, Value>|null {
|
|
434
|
+
public prev(d: Node<Key, Value>): Node<Key, Value> | null {
|
|
447
435
|
let root = this._root;
|
|
448
436
|
let predecessor = null;
|
|
449
437
|
|
|
@@ -466,107 +454,128 @@ export default class Tree<Key=number, Value=any> {
|
|
|
466
454
|
return predecessor;
|
|
467
455
|
}
|
|
468
456
|
|
|
469
|
-
|
|
470
|
-
public clear () : Tree<Key, Value> {
|
|
457
|
+
public clear(): Tree<Key, Value> {
|
|
471
458
|
this._root = null;
|
|
472
459
|
this._size = 0;
|
|
473
460
|
return this;
|
|
474
461
|
}
|
|
475
462
|
|
|
476
|
-
|
|
477
463
|
public toList() {
|
|
478
|
-
return toList(this._root);
|
|
464
|
+
return toList(this._root!);
|
|
479
465
|
}
|
|
480
466
|
|
|
481
|
-
|
|
482
467
|
/**
|
|
483
468
|
* Bulk-load items. Both array have to be same size
|
|
484
469
|
*/
|
|
485
|
-
public load
|
|
470
|
+
public load(keys: Key[], values: Value[] = [], presort: boolean = false) {
|
|
486
471
|
let size = keys.length;
|
|
487
472
|
const comparator = this._comparator;
|
|
488
473
|
|
|
489
474
|
// sort if needed
|
|
490
475
|
if (presort) sort(keys, values, 0, size - 1, comparator);
|
|
491
476
|
|
|
492
|
-
if (this._root === null) {
|
|
477
|
+
if (this._root === null) {
|
|
478
|
+
// empty tree
|
|
493
479
|
this._root = loadRecursive(keys, values, 0, size);
|
|
494
480
|
this._size = size;
|
|
495
|
-
} else {
|
|
496
|
-
|
|
481
|
+
} else {
|
|
482
|
+
// that re-builds the whole tree from two in-order traversals
|
|
483
|
+
const mergedList = mergeLists(
|
|
484
|
+
this.toList(),
|
|
485
|
+
createList(keys, values),
|
|
486
|
+
comparator
|
|
487
|
+
);
|
|
497
488
|
size = this._size + size;
|
|
498
489
|
this._root = sortedListToBST({ head: mergedList }, 0, size);
|
|
499
490
|
}
|
|
500
491
|
return this;
|
|
501
492
|
}
|
|
502
493
|
|
|
494
|
+
public isEmpty(): boolean {
|
|
495
|
+
return this._root === null;
|
|
496
|
+
}
|
|
503
497
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
get
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
public toString (printNode:NodePrinter<Key, Value> = (n) => String(n.key)) : string {
|
|
511
|
-
const out:string[] = [];
|
|
512
|
-
printRow(this._root, '', true, (v) => out.push(v), printNode);
|
|
513
|
-
return out.join('');
|
|
498
|
+
get size(): number {
|
|
499
|
+
return this._size;
|
|
500
|
+
}
|
|
501
|
+
get root(): Node<Key, Value> | null {
|
|
502
|
+
return this._root;
|
|
514
503
|
}
|
|
515
504
|
|
|
505
|
+
public toString(
|
|
506
|
+
printNode: NodePrinter<Key, Value> = (n) => String(n.key)
|
|
507
|
+
): string {
|
|
508
|
+
const out: string[] = [];
|
|
509
|
+
printRow(this._root!, "", true, (v) => out.push(v), printNode);
|
|
510
|
+
return out.join("");
|
|
511
|
+
}
|
|
516
512
|
|
|
517
|
-
public update
|
|
513
|
+
public update(key: Key, newKey: Key, newData?: Value): void {
|
|
518
514
|
const comparator = this._comparator;
|
|
519
|
-
let { left, right } = split(key, this._root
|
|
515
|
+
let { left, right } = split(key, this._root!, comparator);
|
|
520
516
|
if (comparator(key, newKey) < 0) {
|
|
521
|
-
right = insert(newKey, newData, right
|
|
517
|
+
right = insert(newKey, newData, right!, comparator);
|
|
522
518
|
} else {
|
|
523
|
-
left = insert(newKey, newData, left
|
|
519
|
+
left = insert(newKey, newData, left!, comparator);
|
|
524
520
|
}
|
|
525
521
|
this._root = merge(left, right, comparator);
|
|
526
522
|
}
|
|
527
523
|
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
return split(key, this._root, this._comparator);
|
|
524
|
+
public split(key: Key) {
|
|
525
|
+
return split(key, this._root!, this._comparator);
|
|
531
526
|
}
|
|
532
527
|
|
|
533
528
|
*[Symbol.iterator]() {
|
|
534
|
-
let
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
529
|
+
let current = this._root;
|
|
530
|
+
const Q: Node<Key, Value>[] = []; /* Initialize stack s */
|
|
531
|
+
let done = false;
|
|
532
|
+
|
|
533
|
+
while (!done) {
|
|
534
|
+
if (current !== null) {
|
|
535
|
+
Q.push(current);
|
|
536
|
+
current = current.left;
|
|
537
|
+
} else {
|
|
538
|
+
if (Q.length !== 0) {
|
|
539
|
+
current = Q.pop()!;
|
|
540
|
+
yield current;
|
|
541
|
+
|
|
542
|
+
current = current.right;
|
|
543
|
+
} else done = true;
|
|
544
|
+
}
|
|
538
545
|
}
|
|
539
546
|
}
|
|
540
547
|
}
|
|
541
548
|
|
|
542
|
-
|
|
543
|
-
|
|
549
|
+
function loadRecursive(
|
|
550
|
+
keys: Key[],
|
|
551
|
+
values: Value[],
|
|
552
|
+
start: number,
|
|
553
|
+
end: number
|
|
554
|
+
): Node<Key, Value> | null {
|
|
544
555
|
const size = end - start;
|
|
545
556
|
if (size > 0) {
|
|
546
557
|
const middle = start + Math.floor(size / 2);
|
|
547
|
-
const key
|
|
548
|
-
const data
|
|
549
|
-
const node
|
|
550
|
-
node.left
|
|
551
|
-
node.right
|
|
558
|
+
const key = keys[middle];
|
|
559
|
+
const data = values[middle];
|
|
560
|
+
const node = new Node(key, data);
|
|
561
|
+
node.left = loadRecursive(keys, values, start, middle);
|
|
562
|
+
node.right = loadRecursive(keys, values, middle + 1, end);
|
|
552
563
|
return node;
|
|
553
564
|
}
|
|
554
565
|
return null;
|
|
555
566
|
}
|
|
556
567
|
|
|
557
|
-
|
|
558
|
-
function createList(keys:Key[], values:Value[]) : Node<Key, Value> {
|
|
568
|
+
function createList(keys: Key[], values: Value[]): Node<Key, Value> {
|
|
559
569
|
const head = new Node<Key, Value>(null, null);
|
|
560
|
-
let p:Node<Key, Value> = head;
|
|
570
|
+
let p: Node<Key, Value> = head;
|
|
561
571
|
for (let i = 0; i < keys.length; i++) {
|
|
562
572
|
p = p.next = new Node(keys[i], values[i]);
|
|
563
573
|
}
|
|
564
574
|
p.next = null;
|
|
565
|
-
return head.next
|
|
575
|
+
return head.next!;
|
|
566
576
|
}
|
|
567
577
|
|
|
568
|
-
|
|
569
|
-
function toList (root:Node<Key, Value>) : Node<Key, Value> {
|
|
578
|
+
function toList(root: Node<Key, Value>): Node<Key, Value> {
|
|
570
579
|
let current = root;
|
|
571
580
|
const Q = [];
|
|
572
581
|
let done = false;
|
|
@@ -577,53 +586,57 @@ function toList (root:Node<Key, Value>) : Node<Key, Value> {
|
|
|
577
586
|
while (!done) {
|
|
578
587
|
if (current) {
|
|
579
588
|
Q.push(current);
|
|
580
|
-
current = current.left
|
|
589
|
+
current = current.left!;
|
|
581
590
|
} else {
|
|
582
591
|
if (Q.length > 0) {
|
|
583
|
-
current = p = p.next = Q.pop()
|
|
584
|
-
current = current.right
|
|
592
|
+
current = p = p.next = Q.pop()!;
|
|
593
|
+
current = current.right!;
|
|
585
594
|
} else done = true;
|
|
586
595
|
}
|
|
587
596
|
}
|
|
588
597
|
p.next = null; // that'll work even if the tree was empty
|
|
589
|
-
return head.next
|
|
598
|
+
return head.next!;
|
|
590
599
|
}
|
|
591
600
|
|
|
592
|
-
|
|
593
|
-
|
|
601
|
+
function sortedListToBST(
|
|
602
|
+
list: TreeNodeList<Key, Value>,
|
|
603
|
+
start: number,
|
|
604
|
+
end: number
|
|
605
|
+
) {
|
|
594
606
|
const size = end - start;
|
|
595
607
|
if (size > 0) {
|
|
596
608
|
const middle = start + Math.floor(size / 2);
|
|
597
609
|
const left = sortedListToBST(list, start, middle);
|
|
598
610
|
|
|
599
611
|
const root = list.head;
|
|
600
|
-
root
|
|
612
|
+
root!.left = left;
|
|
601
613
|
|
|
602
|
-
list.head = list.head
|
|
614
|
+
list.head = list.head!.next;
|
|
603
615
|
|
|
604
|
-
root
|
|
605
|
-
return root
|
|
616
|
+
root!.right = sortedListToBST(list, middle + 1, end);
|
|
617
|
+
return root!;
|
|
606
618
|
}
|
|
607
619
|
return null;
|
|
608
620
|
}
|
|
609
621
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
compare:Comparator<Key>
|
|
614
|
-
|
|
622
|
+
function mergeLists<Key, Value>(
|
|
623
|
+
l1: Node<Key, Value>,
|
|
624
|
+
l2: Node<Key, Value>,
|
|
625
|
+
compare: Comparator<Key>
|
|
626
|
+
): Node<Key, Value> {
|
|
627
|
+
const head: Node<Key, Value> = new Node<Key, Value>(null as Key, null); // dummy
|
|
615
628
|
let p = head;
|
|
616
629
|
|
|
617
|
-
let p1:Node<Key, Value> = l1;
|
|
618
|
-
let p2:Node<Key, Value> = l2;
|
|
630
|
+
let p1: Node<Key, Value> = l1;
|
|
631
|
+
let p2: Node<Key, Value> = l2;
|
|
619
632
|
|
|
620
633
|
while (p1 !== null && p2 !== null) {
|
|
621
634
|
if (compare(p1.key, p2.key) < 0) {
|
|
622
635
|
p.next = p1;
|
|
623
|
-
p1 = p1.next
|
|
636
|
+
p1 = p1.next!;
|
|
624
637
|
} else {
|
|
625
638
|
p.next = p2;
|
|
626
|
-
p2 = p2.next
|
|
639
|
+
p2 = p2.next!;
|
|
627
640
|
}
|
|
628
641
|
p = p.next;
|
|
629
642
|
}
|
|
@@ -634,13 +647,15 @@ function mergeLists<Key, Value> (
|
|
|
634
647
|
p.next = p2;
|
|
635
648
|
}
|
|
636
649
|
|
|
637
|
-
return head.next
|
|
650
|
+
return head.next!;
|
|
638
651
|
}
|
|
639
652
|
|
|
640
|
-
|
|
641
653
|
function sort(
|
|
642
|
-
keys:Key[],
|
|
643
|
-
|
|
654
|
+
keys: Key[],
|
|
655
|
+
values: Value[],
|
|
656
|
+
left: number,
|
|
657
|
+
right: number,
|
|
658
|
+
compare: Comparator<Key>
|
|
644
659
|
) {
|
|
645
660
|
if (left >= right) return;
|
|
646
661
|
|
|
@@ -649,8 +664,10 @@ function sort(
|
|
|
649
664
|
let j = right + 1;
|
|
650
665
|
|
|
651
666
|
while (true) {
|
|
652
|
-
do i++;
|
|
653
|
-
|
|
667
|
+
do i++;
|
|
668
|
+
while (compare(keys[i], pivot) < 0);
|
|
669
|
+
do j--;
|
|
670
|
+
while (compare(keys[j], pivot) > 0);
|
|
654
671
|
if (i >= j) break;
|
|
655
672
|
|
|
656
673
|
let tmp = keys[i];
|
|
@@ -662,6 +679,6 @@ function sort(
|
|
|
662
679
|
values[j] = tmp;
|
|
663
680
|
}
|
|
664
681
|
|
|
665
|
-
sort(keys, values,
|
|
682
|
+
sort(keys, values, left, j, compare);
|
|
666
683
|
sort(keys, values, j + 1, right, compare);
|
|
667
684
|
}
|