data-structure-typed 1.15.0 → 1.15.2
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 +378 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +30 -30
- package/dist/data-structures/binary-tree/binary-tree.js +55 -55
- package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -17
- package/dist/data-structures/binary-tree/segment-tree.js +30 -30
- package/dist/data-structures/graph/abstract-graph.d.ts +6 -6
- package/dist/data-structures/graph/abstract-graph.js +6 -6
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/hash/coordinate-map.d.ts +2 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +14 -14
- package/dist/data-structures/heap/heap.js +12 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +9 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +7 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -7
- package/dist/data-structures/priority-queue/priority-queue.js +6 -6
- package/dist/data-structures/queue/deque.d.ts +1 -1
- package/dist/utils/types/utils.d.ts +0 -52
- package/dist/utils/types/utils.js +0 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +1 -547
- package/package.json +3 -4
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/binary-tree.ts +83 -76
- package/src/data-structures/binary-tree/segment-tree.ts +55 -36
- package/src/data-structures/graph/abstract-graph.ts +21 -19
- package/src/data-structures/graph/directed-graph.ts +23 -18
- package/src/data-structures/graph/undirected-graph.ts +16 -11
- package/src/data-structures/hash/coordinate-map.ts +11 -8
- package/src/data-structures/hash/coordinate-set.ts +11 -8
- package/src/data-structures/heap/heap.ts +34 -28
- package/src/data-structures/linked-list/doubly-linked-list.ts +40 -26
- package/src/data-structures/linked-list/singly-linked-list.ts +32 -23
- package/src/data-structures/priority-queue/priority-queue.ts +17 -14
- package/src/data-structures/queue/deque.ts +14 -4
- package/src/utils/types/utils.ts +1 -175
- package/src/utils/utils.ts +1 -484
- package/tests/unit/data-structures/binary-tree/bst.test.ts +40 -31
- package/tests/unit/data-structures/graph/directed-graph.test.ts +31 -34
package/src/utils/utils.ts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import type {AnyFunction, JSONObject, JSONSerializable} from './types';
|
|
3
|
-
|
|
4
|
-
export function randomText(length: number) {
|
|
5
|
-
let result = '';
|
|
6
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
7
|
-
const charactersLength = characters.length;
|
|
8
|
-
for (let i = 0; i < length; i++) {
|
|
9
|
-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
1
|
export const uuidV4 = function () {
|
|
15
2
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
16
3
|
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
@@ -18,447 +5,6 @@ export const uuidV4 = function () {
|
|
|
18
5
|
});
|
|
19
6
|
};
|
|
20
7
|
|
|
21
|
-
export class IncrementId {
|
|
22
|
-
private _id: string;
|
|
23
|
-
private readonly _prefix: string;
|
|
24
|
-
|
|
25
|
-
constructor(prefix?: string) {
|
|
26
|
-
this._prefix = prefix ? prefix : '';
|
|
27
|
-
this._id = this._prefix + '0';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
getId() {
|
|
31
|
-
const {_id, _prefix} = this;
|
|
32
|
-
if (!_id) {
|
|
33
|
-
this._id = _prefix + '0';
|
|
34
|
-
} else {
|
|
35
|
-
const idNumStr = _id.substr(_prefix.length, _id.length - _prefix.length);
|
|
36
|
-
const newIdNum = parseInt(idNumStr, 10) + 1;
|
|
37
|
-
this._id = _prefix + newIdNum.toString();
|
|
38
|
-
}
|
|
39
|
-
return this._id;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function incrementId(prefix?: string) {
|
|
44
|
-
const _prefix = prefix ? prefix : '';
|
|
45
|
-
let _id = _prefix + '0';
|
|
46
|
-
return function id() {
|
|
47
|
-
const idNumStr = _id.substr(_prefix.length, _id.length - _prefix.length);
|
|
48
|
-
const newIdNum = parseInt(idNumStr, 10) + 1;
|
|
49
|
-
_id = _prefix + newIdNum.toString();
|
|
50
|
-
return _id;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const getValue = <T, K extends keyof T>(obj: T, names: K[]): Array<T[K]> => names.map(i => obj[i]);
|
|
55
|
-
|
|
56
|
-
export const isObject = (object: string | JSONObject | boolean | AnyFunction | number) => object != null && typeof object === 'object';
|
|
57
|
-
|
|
58
|
-
export const looseEqual = (a: any, b: any): boolean => a == b;
|
|
59
|
-
|
|
60
|
-
export const strictEqual = (a: any, b: any): boolean => a === b;
|
|
61
|
-
|
|
62
|
-
export const strictObjectIsEqual = (a: any, b: any): boolean => Object.is(a, b);
|
|
63
|
-
|
|
64
|
-
export const deepObjectStrictEqual = (object1: JSONSerializable, object2: JSONSerializable) => {
|
|
65
|
-
const keys1 = Object.keys(object1);
|
|
66
|
-
const keys2 = Object.keys(object2);
|
|
67
|
-
if (keys1.length !== keys2.length) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
for (const key of keys1) {
|
|
71
|
-
const val1 = object1[key];
|
|
72
|
-
const val2 = object2[key];
|
|
73
|
-
const areObjects = isObject(val1) && isObject(val2);
|
|
74
|
-
if (
|
|
75
|
-
areObjects && !deepObjectStrictEqual(val1, val2) ||
|
|
76
|
-
!areObjects && val1 !== val2
|
|
77
|
-
) {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return true;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export function reverseColor(oldColor: string) {
|
|
85
|
-
const oldColorTemp = '0x' + oldColor.replace(/#/g, '');
|
|
86
|
-
const str = '000000' + (0xFFFFFF - Number(oldColorTemp)).toString(16);
|
|
87
|
-
return '#' + str.substring(str.length - 6, str.length);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export const isSameStructure = (objA: unknown, objB: unknown) => {
|
|
91
|
-
const objATraversable = objA as JSONSerializable;
|
|
92
|
-
const objBTraversable = objB as JSONSerializable;
|
|
93
|
-
const objAKeys = Object.keys(objATraversable);
|
|
94
|
-
const objBKeys = Object.keys(objBTraversable);
|
|
95
|
-
let isSame = true;
|
|
96
|
-
if (objAKeys.length !== objBKeys.length) {
|
|
97
|
-
return isSame = false;
|
|
98
|
-
} else {
|
|
99
|
-
objAKeys.forEach((i) => {
|
|
100
|
-
if (!objBKeys.includes(i)) {
|
|
101
|
-
return isSame = false;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
return isSame;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const isLeafParent = (obj: JSONObject) => {
|
|
109
|
-
let isLeaf = true;
|
|
110
|
-
Object.values(obj).forEach(value => {
|
|
111
|
-
if (typeof value === 'object' && value instanceof Array) {
|
|
112
|
-
value.forEach(item => {
|
|
113
|
-
if (typeof item === 'object') {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
return isLeaf = true;
|
|
118
|
-
}
|
|
119
|
-
if (!['string', 'boolean', 'number', 'undefined', 'function'].includes(typeof value) && (value !== null)) {
|
|
120
|
-
return isLeaf = false;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return isLeaf;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export const addDays = (date: Date, days: number): Date => {
|
|
127
|
-
date.setDate(date.getDate() + days);
|
|
128
|
-
return date;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
export class WaitManager {
|
|
132
|
-
private _time30 = 20000;
|
|
133
|
-
private readonly _nXSpeed: number = 1;
|
|
134
|
-
|
|
135
|
-
constructor(nXSpeed?: number) {
|
|
136
|
-
if (nXSpeed === undefined) nXSpeed = 1;
|
|
137
|
-
this._nXSpeed = nXSpeed;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private _time1 = 1000;
|
|
141
|
-
|
|
142
|
-
get time1(): number {
|
|
143
|
-
return this._time1 / this._nXSpeed;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
private _time2 = 2000;
|
|
147
|
-
|
|
148
|
-
get time2(): number {
|
|
149
|
-
return this._time2 / this._nXSpeed;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
private _time3 = 3000;
|
|
153
|
-
|
|
154
|
-
get time3(): number {
|
|
155
|
-
return this._time3 / this._nXSpeed;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
private _time4 = 4000;
|
|
159
|
-
|
|
160
|
-
get time4(): number {
|
|
161
|
-
return this._time4 / this._nXSpeed;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private _time10 = 10000;
|
|
165
|
-
|
|
166
|
-
get time10(): number {
|
|
167
|
-
return this._time10 / this._nXSpeed;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
private _time20 = 20000;
|
|
171
|
-
|
|
172
|
-
get time20(): number {
|
|
173
|
-
return this._time20 / this._nXSpeed;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
get time50(): number {
|
|
177
|
-
return this._time30 / this._nXSpeed;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private _time60 = 60000;
|
|
181
|
-
|
|
182
|
-
get time60(): number {
|
|
183
|
-
return this._time60 / this._nXSpeed;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private _cusTime = 1000;
|
|
187
|
-
|
|
188
|
-
get cusTime(): number {
|
|
189
|
-
return this._cusTime / this._nXSpeed;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
set cusTime(v: number) {
|
|
193
|
-
this._cusTime = v;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export const wait = async (ms: number, resolveValue?: any) => {
|
|
198
|
-
return new Promise((resolve, reject) => {
|
|
199
|
-
setTimeout(() => {
|
|
200
|
-
const finalResolveValue = resolveValue || true;
|
|
201
|
-
resolve(finalResolveValue);
|
|
202
|
-
}, ms);
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
export function extractValue<Item>(data: { key: string, value: Item }[]) {
|
|
207
|
-
let result: Item[] = [];
|
|
208
|
-
if (data && data.length > 0) {
|
|
209
|
-
result = data.map(item => item.value);
|
|
210
|
-
}
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function keyValueToArray<Item>(data: { [key: string]: Item }) {
|
|
215
|
-
const itemArray: Array<Item> = [];
|
|
216
|
-
const keys = Object.keys(data);
|
|
217
|
-
for (const i of keys) {
|
|
218
|
-
itemArray.push({...data[i], _id: i});
|
|
219
|
-
}
|
|
220
|
-
return itemArray;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export function minuted(time: number) {
|
|
224
|
-
const minutes = Math.floor(time / 60000).toString();
|
|
225
|
-
const seconds = Math.floor((time % 60000) / 1000).toString().padStart(2, '0');
|
|
226
|
-
return `${minutes}:${seconds}`;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function randomDate(start?: Date, end?: Date, specificProbabilityStart?: Date, specificProbability?: number) {
|
|
230
|
-
if (!start) start = new Date('1970-1-1');
|
|
231
|
-
if (!end) end = new Date();
|
|
232
|
-
|
|
233
|
-
if (specificProbabilityStart) {
|
|
234
|
-
if (!specificProbability) specificProbability = 0.5;
|
|
235
|
-
if (Math.random() <= specificProbability) {
|
|
236
|
-
return new Date(specificProbabilityStart.getTime() + Math.random() * (end.getTime() - specificProbabilityStart.getTime()));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export const capitalizeWords = (str: string) => str.replace(/(?:^|\s)\S/g, (a: string) => a.toUpperCase());
|
|
244
|
-
|
|
245
|
-
export const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
|
|
246
|
-
|
|
247
|
-
export const comparerArray = <T>(otherArray: T[], limitKeys?: string[]) => {
|
|
248
|
-
return function (current: T) {
|
|
249
|
-
return otherArray.filter(function (other: T) {
|
|
250
|
-
if (!limitKeys) {
|
|
251
|
-
return _.isEqual(current, other);
|
|
252
|
-
} else {
|
|
253
|
-
// TODO
|
|
254
|
-
}
|
|
255
|
-
}).length == 0;
|
|
256
|
-
};
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
export const onlyInA = <T>(a: T[], b: T[]) => a.filter(comparerArray(b));
|
|
260
|
-
|
|
261
|
-
export const onlyInB = <T>(a: T[], b: T[]) => b.filter(comparerArray(a));
|
|
262
|
-
|
|
263
|
-
export const diffAB = <T>(a: T[], b: T[]) => onlyInA(a, b).concat(onlyInB(a, b));
|
|
264
|
-
|
|
265
|
-
export class StringUtil {
|
|
266
|
-
// camelCase
|
|
267
|
-
static toCamelCase(str: string) {
|
|
268
|
-
return _.camelCase(str);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// snake_case
|
|
272
|
-
static toSnakeCase(str: string) {
|
|
273
|
-
return _.snakeCase(str);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// PascalCase
|
|
277
|
-
static toPascalCase(str: string) {
|
|
278
|
-
return _.startCase(_.camelCase(str)).replace(/ /g, '');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// CONSTANT_CASE
|
|
282
|
-
static toConstantCase(str: string) {
|
|
283
|
-
return _.upperCase(str).replace(/ /g, '_');
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// kebab-case
|
|
287
|
-
static toKebabCase(str: string) {
|
|
288
|
-
return _.kebabCase(str);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// lowercase
|
|
292
|
-
static toLowerCase(str: string) {
|
|
293
|
-
return _.lowerCase(str).replace(/ /g, '');
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Title Case
|
|
297
|
-
static toTitleCase(str: string) {
|
|
298
|
-
return _.startCase(_.camelCase(str));
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Sentence case
|
|
302
|
-
static toSentenceCase(str: string) {
|
|
303
|
-
return _.upperFirst(_.lowerCase(str));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// path/case
|
|
307
|
-
static toPathCase(str: string) {
|
|
308
|
-
return _.lowerCase(str).replace(/ /g, '/');
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// dot.case
|
|
312
|
-
static toDotCase(str: string) {
|
|
313
|
-
return _.lowerCase(str).replace(/ /g, '.');
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export type CaseType =
|
|
318
|
-
'camel'
|
|
319
|
-
| 'snake'
|
|
320
|
-
| 'pascal'
|
|
321
|
-
| 'constant'
|
|
322
|
-
| 'kebab'
|
|
323
|
-
| 'lower'
|
|
324
|
-
| 'title'
|
|
325
|
-
| 'sentence'
|
|
326
|
-
| 'path'
|
|
327
|
-
| 'dot';
|
|
328
|
-
export const deepKeysConvert = (obj: any, toType?: CaseType): any => {
|
|
329
|
-
const _toType = toType || 'snake';
|
|
330
|
-
if (Array.isArray(obj)) {
|
|
331
|
-
return obj.map(v => deepKeysConvert(v, _toType));
|
|
332
|
-
} else if (obj !== null && obj.constructor === Object) {
|
|
333
|
-
return Object.keys(obj).reduce(
|
|
334
|
-
(result, key) => {
|
|
335
|
-
let newKey = '';
|
|
336
|
-
switch (_toType) {
|
|
337
|
-
case 'camel':
|
|
338
|
-
newKey = StringUtil.toCamelCase(key);
|
|
339
|
-
break;
|
|
340
|
-
case 'snake':
|
|
341
|
-
newKey = StringUtil.toSnakeCase(key);
|
|
342
|
-
break;
|
|
343
|
-
case 'pascal':
|
|
344
|
-
newKey = StringUtil.toPascalCase(key);
|
|
345
|
-
break;
|
|
346
|
-
case 'constant':
|
|
347
|
-
newKey = StringUtil.toConstantCase(key);
|
|
348
|
-
break;
|
|
349
|
-
case 'kebab':
|
|
350
|
-
newKey = StringUtil.toKebabCase(key);
|
|
351
|
-
break;
|
|
352
|
-
case 'lower':
|
|
353
|
-
newKey = StringUtil.toLowerCase(key);
|
|
354
|
-
break;
|
|
355
|
-
case 'title':
|
|
356
|
-
newKey = StringUtil.toTitleCase(key);
|
|
357
|
-
break;
|
|
358
|
-
case 'sentence':
|
|
359
|
-
newKey = StringUtil.toSentenceCase(key);
|
|
360
|
-
break;
|
|
361
|
-
case 'path':
|
|
362
|
-
newKey = StringUtil.toPathCase(key);
|
|
363
|
-
break;
|
|
364
|
-
case 'dot':
|
|
365
|
-
newKey = StringUtil.toDotCase(key);
|
|
366
|
-
break;
|
|
367
|
-
default:
|
|
368
|
-
newKey = StringUtil.toDotCase(key);
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
return {
|
|
372
|
-
...result,
|
|
373
|
-
[newKey]: deepKeysConvert(obj[key], _toType),
|
|
374
|
-
};
|
|
375
|
-
},
|
|
376
|
-
{},
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
return obj;
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
export const deepRemoveByKey = (obj: any, keysToBeRemoved: string[]) => {
|
|
383
|
-
const result = _.transform(obj, function (result: JSONSerializable, value: any, key: string) {
|
|
384
|
-
if (_.isObject(value)) {
|
|
385
|
-
value = deepRemoveByKey(value, keysToBeRemoved);
|
|
386
|
-
}
|
|
387
|
-
if (!keysToBeRemoved.includes(key)) {
|
|
388
|
-
_.isArray(obj) ? result.push(value) : result[key] = value;
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
return result as typeof obj;
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
export const deepRenameKeys = (obj: JSONSerializable, keysMap: { [key in string]: string }) => {
|
|
395
|
-
return _.transform(obj, function (result: JSONSerializable, value: any, key: string | number) {
|
|
396
|
-
const currentKey = keysMap[key] || key;
|
|
397
|
-
result[currentKey] = _.isObject(value) ? deepRenameKeys(value, keysMap) : value;
|
|
398
|
-
});
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
export const deepReplaceValues = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }) => {
|
|
402
|
-
const newObject = _.clone(obj) as JSONSerializable;
|
|
403
|
-
_.each(obj, (val: any, key: string) => {
|
|
404
|
-
for (const item in keyReducerMap) {
|
|
405
|
-
if (key === item) {
|
|
406
|
-
newObject[key] = keyReducerMap[item](newObject);
|
|
407
|
-
} else if (typeof (val) === 'object' || val instanceof Array) {
|
|
408
|
-
newObject[key] = deepReplaceValues(val, keyReducerMap);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
return newObject;
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
// TODO determine depth and pass root node as a param through callback
|
|
416
|
-
export const deepAdd = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }, isItemRootParent?: boolean) => {
|
|
417
|
-
const newObject = _.clone(obj) as JSONObject | [];
|
|
418
|
-
if (_.isObject(newObject) && !_.isArray(newObject)) {
|
|
419
|
-
for (const item in keyReducerMap) {
|
|
420
|
-
newObject[item] = keyReducerMap[item](newObject);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
_.each(obj, (val: any, key: string | number) => {
|
|
424
|
-
if (_.isObject(val)) {
|
|
425
|
-
for (const item in keyReducerMap) {
|
|
426
|
-
// @ts-ignore
|
|
427
|
-
newObject[key] = deepAdd(val, keyReducerMap, isItemRootParent);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
});
|
|
431
|
-
return newObject;
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
const styleString = (color: string) => `color: ${color}; font-weight: bold`;
|
|
435
|
-
|
|
436
|
-
const styleHeader = (header: string) => `%c[${header}]`;
|
|
437
|
-
|
|
438
|
-
export const bunnyConsole = {
|
|
439
|
-
log: (headerLog = 'bunny', ...args: any[]) => {
|
|
440
|
-
return console.log(styleHeader(headerLog), styleString('black'), ...args);
|
|
441
|
-
},
|
|
442
|
-
warn: (headerLog = 'bunny', ...args: any[]) => {
|
|
443
|
-
return console.warn(styleHeader(headerLog), styleString('orange'), ...args);
|
|
444
|
-
},
|
|
445
|
-
error: (headerLog = 'bunny', ...args: any[]) => {
|
|
446
|
-
return console.error(styleHeader(headerLog), styleString('red'), ...args);
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
export const timeStart = () => {
|
|
451
|
-
return performance ? performance.now() : new Date().getTime();
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
export const timeEnd = (startTime: number, headerLog?: string, consoleConditionFn?: (timeSpent: number) => boolean) => {
|
|
455
|
-
const timeSpent = (performance ? performance.now() : new Date().getTime()) - startTime;
|
|
456
|
-
const isPassCondition = consoleConditionFn ? consoleConditionFn(timeSpent) : true;
|
|
457
|
-
if (isPassCondition) {
|
|
458
|
-
bunnyConsole.log(headerLog ? headerLog : 'time spent', timeSpent.toFixed(2));
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
|
|
462
8
|
export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
|
|
463
9
|
let i = -1, len = array ? array.length : 0;
|
|
464
10
|
const result = [];
|
|
@@ -475,35 +21,6 @@ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index:
|
|
|
475
21
|
return result;
|
|
476
22
|
};
|
|
477
23
|
|
|
478
|
-
export function memo() {
|
|
479
|
-
const cache: { [k: string]: any } = {};
|
|
480
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
481
|
-
return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
482
|
-
const originalMethod = descriptor.value;
|
|
483
|
-
descriptor.value = function (...args: any[]) {
|
|
484
|
-
const cacheKey = `__cacheKey__${args.toString()}`;
|
|
485
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
486
|
-
if (!cache.hasOwnProperty(cacheKey)) {
|
|
487
|
-
cache[cacheKey] = originalMethod.apply(this, args);
|
|
488
|
-
}
|
|
489
|
-
return cache[cacheKey];
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?: { isToObj: boolean }) {
|
|
495
|
-
const zipped: [T, T1][] = [];
|
|
496
|
-
const zippedObjCoords: { x: T, y: T1 }[] = [];
|
|
497
|
-
const {isToObj} = options ? options : {isToObj: false};
|
|
498
|
-
for (let i = 0; i < array1.length; i++) {
|
|
499
|
-
if (isToObj) {
|
|
500
|
-
zippedObjCoords.push({x: array1[i], y: array2[i]})
|
|
501
|
-
} else {
|
|
502
|
-
zipped.push([array1[i], array2[i]]);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
return isToObj ? zippedObjCoords : zipped;
|
|
506
|
-
}
|
|
507
24
|
|
|
508
25
|
/**
|
|
509
26
|
* data-structure-typed
|
|
@@ -512,7 +29,7 @@ export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?
|
|
|
512
29
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
513
30
|
* @license MIT License
|
|
514
31
|
*/
|
|
515
|
-
import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
32
|
+
import type {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
516
33
|
|
|
517
34
|
export const THUNK_SYMBOL = Symbol('thunk')
|
|
518
35
|
|
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
import {BST, BSTNode} from '../../../../src';
|
|
2
2
|
|
|
3
|
-
describe('
|
|
3
|
+
describe('BST Case6', () => {
|
|
4
4
|
it('should perform various operations on a Binary Search Tree', () => {
|
|
5
|
-
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
6
5
|
|
|
7
6
|
const tree = new BST();
|
|
8
|
-
|
|
9
7
|
expect(tree).toBeInstanceOf(BST);
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const values = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
10
|
+
tree.addMany(values);
|
|
13
11
|
expect(tree.root).toBeInstanceOf(BSTNode);
|
|
12
|
+
|
|
14
13
|
if (tree.root) expect(tree.root.id).toBe(11);
|
|
14
|
+
|
|
15
15
|
expect(tree.count).toBe(16);
|
|
16
|
+
|
|
16
17
|
expect(tree.has(6)).toBe(true);
|
|
17
18
|
|
|
18
19
|
const node6 = tree.get(6);
|
|
19
20
|
expect(node6 && tree.getHeight(node6)).toBe(2);
|
|
20
21
|
expect(node6 && tree.getDepth(node6)).toBe(3);
|
|
21
|
-
const getNodeById = tree.get(10, 'id');
|
|
22
|
-
expect(getNodeById?.id).toBe(10);
|
|
23
22
|
|
|
24
|
-
const
|
|
25
|
-
expect(
|
|
23
|
+
const nodeId10 = tree.get(10, 'id');
|
|
24
|
+
expect(nodeId10?.id).toBe(10);
|
|
25
|
+
|
|
26
|
+
const nodeVal9 = tree.get(9, 'val');
|
|
27
|
+
expect(nodeVal9?.id).toBe(9);
|
|
28
|
+
|
|
29
|
+
const nodesByCount1 = tree.getNodes(1, 'count');
|
|
30
|
+
expect(nodesByCount1.length).toBe(16);
|
|
26
31
|
|
|
27
|
-
const
|
|
28
|
-
expect(
|
|
32
|
+
const leftMost = tree.getLeftMost();
|
|
33
|
+
expect(leftMost?.id).toBe(1);
|
|
29
34
|
|
|
30
35
|
const node15 = tree.get(15);
|
|
31
|
-
const
|
|
32
|
-
expect(
|
|
36
|
+
const minNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
|
37
|
+
expect(minNodeBySpecificNode?.id).toBe(12);
|
|
33
38
|
|
|
34
39
|
const subTreeSum = node15 && tree.subTreeSum(node15);
|
|
35
40
|
expect(subTreeSum).toBe(70);
|
|
@@ -43,30 +48,33 @@ describe('bst-case6', () => {
|
|
|
43
48
|
expect(subTreeAdd).toBeDefined();
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
|
|
47
51
|
const node11 = tree.get(11);
|
|
48
52
|
expect(node11).toBeInstanceOf(BSTNode);
|
|
49
53
|
if (node11 instanceof BSTNode) {
|
|
50
|
-
const
|
|
51
|
-
expect(
|
|
54
|
+
const allGreaterNodesAdded = tree.allGreaterNodesAdd(node11, 2, 'count');
|
|
55
|
+
expect(allGreaterNodesAdded).toBeDefined();
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
const
|
|
55
|
-
expect(
|
|
56
|
-
expect(
|
|
58
|
+
const dfsInorderNodes = tree.DFS('in', 'node');
|
|
59
|
+
expect(dfsInorderNodes[0].id).toBe(1);
|
|
60
|
+
expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
|
|
57
61
|
|
|
58
62
|
tree.balance();
|
|
59
|
-
const bfs = tree.BFS('node');
|
|
60
63
|
expect(tree.isBalanced()).toBe(true);
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
|
|
65
|
+
const bfsNodesAfterBalanced = tree.BFS('node');
|
|
66
|
+
expect(bfsNodesAfterBalanced[0].id).toBe(8);
|
|
67
|
+
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
|
|
63
68
|
|
|
64
69
|
const removed11 = tree.remove(11, true);
|
|
65
70
|
expect(removed11).toBeInstanceOf(Array);
|
|
66
71
|
expect(removed11[0]).toBeDefined();
|
|
67
72
|
expect(removed11[0].deleted).toBeDefined();
|
|
73
|
+
|
|
68
74
|
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
|
|
75
|
+
|
|
69
76
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
77
|
+
|
|
70
78
|
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
71
79
|
|
|
72
80
|
const removed1 = tree.remove(1, true);
|
|
@@ -76,6 +84,7 @@ describe('bst-case6', () => {
|
|
|
76
84
|
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
|
|
77
85
|
|
|
78
86
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
87
|
+
|
|
79
88
|
expect(tree.getHeight()).toBe(4);
|
|
80
89
|
|
|
81
90
|
const removed4 = tree.remove(4, true);
|
|
@@ -170,16 +179,16 @@ describe('bst-case6', () => {
|
|
|
170
179
|
expect(tree.getHeight()).toBe(2);
|
|
171
180
|
|
|
172
181
|
|
|
173
|
-
expect(
|
|
182
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
174
183
|
|
|
175
|
-
const
|
|
176
|
-
expect(
|
|
177
|
-
expect(
|
|
178
|
-
expect(
|
|
184
|
+
const bfsIDs = tree.BFS();
|
|
185
|
+
expect(bfsIDs[0]).toBe(2);
|
|
186
|
+
expect(bfsIDs[1]).toBe(12);
|
|
187
|
+
expect(bfsIDs[2]).toBe(16);
|
|
179
188
|
|
|
180
|
-
const
|
|
181
|
-
expect(
|
|
182
|
-
expect(
|
|
183
|
-
expect(
|
|
189
|
+
const bfsNodes = tree.BFS('node');
|
|
190
|
+
expect(bfsNodes[0].id).toBe(2);
|
|
191
|
+
expect(bfsNodes[1].id).toBe(12);
|
|
192
|
+
expect(bfsNodes[2].id).toBe(16);
|
|
184
193
|
});
|
|
185
194
|
});
|