data-structure-typed 0.8.18 → 0.9.16
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/.idea/modules.xml +1 -1
- package/README.md +197 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
- package/dist/data-structures/binary-tree/avl-tree.js +93 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
- package/dist/data-structures/binary-tree/binary-tree.js +480 -370
- package/dist/data-structures/binary-tree/bst.d.ts +4 -8
- package/dist/data-structures/binary-tree/bst.js +152 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/segment-tree.js +91 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
- package/dist/data-structures/graph/abstract-graph.js +546 -311
- package/dist/data-structures/graph/directed-graph.d.ts +5 -13
- package/dist/data-structures/graph/directed-graph.js +250 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
- package/dist/data-structures/graph/undirected-graph.js +166 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
- package/dist/data-structures/hash/coordinate-map.js +38 -20
- package/dist/data-structures/hash/coordinate-set.js +33 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +2 -8
- package/dist/data-structures/heap/heap.js +36 -31
- package/dist/data-structures/heap/max-heap.d.ts +3 -2
- package/dist/data-structures/heap/max-heap.js +27 -9
- package/dist/data-structures/heap/min-heap.d.ts +3 -2
- package/dist/data-structures/heap/min-heap.js +27 -9
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
- package/dist/data-structures/matrix/matrix.js +8 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
- package/dist/data-structures/matrix/matrix2d.js +80 -63
- package/dist/data-structures/matrix/navigator.d.ts +2 -16
- package/dist/data-structures/matrix/navigator.js +37 -18
- package/dist/data-structures/matrix/vector2d.d.ts +18 -18
- package/dist/data-structures/matrix/vector2d.js +117 -94
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
- package/dist/data-structures/priority-queue/priority-queue.js +159 -116
- package/dist/data-structures/queue/deque.js +82 -56
- package/dist/data-structures/queue/queue.d.ts +9 -10
- package/dist/data-structures/queue/queue.js +34 -34
- package/dist/data-structures/stack/stack.d.ts +9 -10
- package/dist/data-structures/stack/stack.js +31 -31
- package/dist/data-structures/trampoline.d.ts +14 -23
- package/dist/data-structures/trampoline.js +103 -25
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +234 -80
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/{types → data-structures/types}/utils.d.ts +7 -1
- package/dist/{types → data-structures/types}/utils.js +20 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
- package/dist/utils/utils.js +651 -0
- package/package.json +20 -42
- package/src/data-structures/binary-tree/avl-tree.ts +1 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +184 -139
- package/src/data-structures/binary-tree/bst.ts +15 -24
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +20 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
- package/src/data-structures/diagrams/README.md +7 -0
- package/src/data-structures/graph/abstract-graph.ts +58 -94
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +12 -28
- package/src/data-structures/graph/undirected-graph.ts +11 -10
- package/src/data-structures/hash/coordinate-map.ts +1 -1
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +2 -11
- package/src/data-structures/heap/max-heap.ts +3 -2
- package/src/data-structures/heap/min-heap.ts +3 -2
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
- package/src/data-structures/matrix/matrix2d.ts +11 -11
- package/src/data-structures/matrix/navigator.ts +2 -14
- package/src/data-structures/matrix/vector2d.ts +52 -52
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/priority-queue.ts +70 -78
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +12 -13
- package/src/data-structures/stack/stack.ts +12 -13
- package/src/data-structures/trampoline.ts +31 -71
- package/src/data-structures/trie/trie.ts +61 -11
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +12 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +15 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/{types → data-structures/types}/utils.ts +20 -5
- package/src/utils/index.ts +1 -0
- package/src/{utils.ts → utils/utils.ts} +32 -132
- package/tsconfig.json +9 -6
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
|
@@ -1,91 +1,51 @@
|
|
|
1
|
-
export
|
|
2
|
-
T extends (...args: infer A) => any
|
|
3
|
-
? A
|
|
4
|
-
: never;
|
|
1
|
+
export const THUNK_SYMBOL = Symbol('thunk')
|
|
5
2
|
|
|
6
|
-
export const
|
|
7
|
-
|
|
8
|
-
export interface Thunk<T> extends Function {
|
|
9
|
-
__THUNK__: typeof THUNK_SYMBOL;
|
|
10
|
-
|
|
11
|
-
(): T;
|
|
3
|
+
export const isThunk = (fnOrValue: any) => {
|
|
4
|
+
return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
|
|
12
5
|
}
|
|
13
6
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export type UnwrapThunkDeep<T> = {
|
|
17
|
-
0: T extends Thunk<infer U> ? UnwrapThunkDeep<U> : T;
|
|
18
|
-
}[
|
|
19
|
-
T extends ThunkOrValue<T> ? 0 : never
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
export const isThunk = <T>(value: any): value is Thunk<T> => {
|
|
23
|
-
return typeof value === 'function' && value.__THUNK__ === THUNK_SYMBOL;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const toThunk = <R>(fn: () => R): Thunk<R> => {
|
|
27
|
-
const thunk = () => fn();
|
|
28
|
-
thunk.__THUNK__ = THUNK_SYMBOL;
|
|
29
|
-
return thunk;
|
|
30
|
-
};
|
|
31
|
-
export type UnwrapPromise<T> = T extends Promise<infer U> ? Exclude<U, Promise<T>> : T;
|
|
32
|
-
|
|
33
|
-
export type Unbox<T> = UnwrapThunkDeep<UnwrapPromise<T>>;
|
|
34
|
-
|
|
35
|
-
export type Cont<A extends any[], R> = (...args: A) => Thunk<Unbox<R>>;
|
|
7
|
+
type ToThunkFn = () => ReturnType<TrlFn>;
|
|
36
8
|
|
|
37
|
-
|
|
38
|
-
(...args: ArgumentTypes<F>): Unbox<ReturnType<F>>;
|
|
9
|
+
type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
|
|
39
10
|
|
|
40
|
-
|
|
11
|
+
export const toThunk = (fn: ToThunkFn): Thunk => {
|
|
12
|
+
const thunk = () => fn()
|
|
13
|
+
thunk.__THUNK__ = THUNK_SYMBOL
|
|
14
|
+
return thunk
|
|
41
15
|
}
|
|
42
16
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
cont: Cont<ArgumentTypes<F>, ReturnType<F>>;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const trampoline = <F extends ((...args: any[]) => any)>(fn: F): Trampoline<F> => {
|
|
50
|
-
const cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args));
|
|
17
|
+
type TrlFn = (...args: any[]) => any;
|
|
18
|
+
export const trampoline = (fn: TrlFn) => {
|
|
19
|
+
const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
|
|
51
20
|
|
|
52
21
|
return Object.assign(
|
|
53
|
-
(...args:
|
|
54
|
-
let result
|
|
22
|
+
(...args: [...Parameters<TrlFn>]) => {
|
|
23
|
+
let result = fn(...args)
|
|
55
24
|
|
|
56
|
-
while (isThunk
|
|
57
|
-
result = result()
|
|
25
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
26
|
+
result = result()
|
|
58
27
|
}
|
|
59
28
|
|
|
60
|
-
return result
|
|
29
|
+
return result
|
|
61
30
|
},
|
|
62
|
-
{cont}
|
|
63
|
-
)
|
|
64
|
-
}
|
|
31
|
+
{cont}
|
|
32
|
+
)
|
|
33
|
+
}
|
|
65
34
|
|
|
66
|
-
|
|
67
|
-
|
|
35
|
+
type TrlAsyncFn = (...args: any[]) => any;
|
|
36
|
+
export const trampolineAsync = (fn: TrlAsyncFn) => {
|
|
37
|
+
const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
|
|
68
38
|
|
|
69
39
|
return Object.assign(
|
|
70
|
-
async (...args:
|
|
71
|
-
let result
|
|
40
|
+
async (...args: [...Parameters<TrlAsyncFn>]) => {
|
|
41
|
+
let result = await fn(...args)
|
|
72
42
|
|
|
73
|
-
while (isThunk
|
|
74
|
-
result = await result()
|
|
43
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
44
|
+
result = await result()
|
|
75
45
|
}
|
|
76
46
|
|
|
77
|
-
return result
|
|
47
|
+
return result
|
|
78
48
|
},
|
|
79
|
-
{cont}
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const factorial = trampoline((n: number, acc: number = 1): ThunkOrValue<number> => {
|
|
85
|
-
return n
|
|
86
|
-
// Note: calling factorial.cont instead of factorial directly
|
|
87
|
-
? factorial.cont(n - 1, acc * n)
|
|
88
|
-
: acc;
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// factorial(32768)
|
|
49
|
+
{cont}
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
export class TrieNode {
|
|
2
|
-
protected
|
|
2
|
+
protected _value;
|
|
3
|
+
|
|
4
|
+
constructor(v: string) {
|
|
5
|
+
this._value = v;
|
|
6
|
+
this._isEnd = false;
|
|
7
|
+
this._children = new Map<string, TrieNode>();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
protected _children: Map<string, TrieNode>;
|
|
3
11
|
|
|
4
12
|
get children(): Map<string, TrieNode> {
|
|
5
13
|
return this._children;
|
|
@@ -9,7 +17,7 @@ export class TrieNode {
|
|
|
9
17
|
this._children = v;
|
|
10
18
|
}
|
|
11
19
|
|
|
12
|
-
protected _isEnd
|
|
20
|
+
protected _isEnd: boolean;
|
|
13
21
|
|
|
14
22
|
get isEnd(): boolean {
|
|
15
23
|
return this._isEnd;
|
|
@@ -18,10 +26,28 @@ export class TrieNode {
|
|
|
18
26
|
set isEnd(v: boolean) {
|
|
19
27
|
this._isEnd = v;
|
|
20
28
|
}
|
|
29
|
+
|
|
30
|
+
get val(): string {
|
|
31
|
+
return this._value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set val(v: string) {
|
|
35
|
+
this._value = v;
|
|
36
|
+
}
|
|
21
37
|
}
|
|
22
38
|
|
|
23
39
|
export class Trie {
|
|
40
|
+
constructor(words?: string[]) {
|
|
41
|
+
this._root = new TrieNode('');
|
|
42
|
+
if (words) {
|
|
43
|
+
for (const i of words) {
|
|
44
|
+
this.put(i);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
24
49
|
protected _root: TrieNode;
|
|
50
|
+
|
|
25
51
|
get root() {
|
|
26
52
|
return this._root;
|
|
27
53
|
}
|
|
@@ -30,16 +56,12 @@ export class Trie {
|
|
|
30
56
|
this._root = v;
|
|
31
57
|
}
|
|
32
58
|
|
|
33
|
-
|
|
34
|
-
this._root = new TrieNode();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
put(input: string): boolean {
|
|
59
|
+
put(word: string): boolean {
|
|
38
60
|
let cur = this._root;
|
|
39
|
-
for (const c of
|
|
61
|
+
for (const c of word) {
|
|
40
62
|
let nodeC = cur.children.get(c);
|
|
41
63
|
if (!nodeC) {
|
|
42
|
-
nodeC = new TrieNode();
|
|
64
|
+
nodeC = new TrieNode(c);
|
|
43
65
|
cur.children.set(c, nodeC);
|
|
44
66
|
}
|
|
45
67
|
cur = nodeC;
|
|
@@ -48,7 +70,6 @@ export class Trie {
|
|
|
48
70
|
return true;
|
|
49
71
|
}
|
|
50
72
|
|
|
51
|
-
|
|
52
73
|
has(input: string): boolean {
|
|
53
74
|
let cur = this._root;
|
|
54
75
|
for (const c of input) {
|
|
@@ -107,7 +128,7 @@ export class Trie {
|
|
|
107
128
|
}
|
|
108
129
|
|
|
109
130
|
/**
|
|
110
|
-
* Can present as a prefix or word
|
|
131
|
+
* Can present as a abs prefix or word
|
|
111
132
|
* @param input
|
|
112
133
|
*/
|
|
113
134
|
isPrefix(input: string): boolean {
|
|
@@ -120,6 +141,35 @@ export class Trie {
|
|
|
120
141
|
return true;
|
|
121
142
|
}
|
|
122
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Check if the input string is the common prefix of all the words
|
|
146
|
+
* @param input
|
|
147
|
+
*/
|
|
148
|
+
isCommonPrefix(input: string): boolean {
|
|
149
|
+
let commonPre = '';
|
|
150
|
+
const dfs = (cur: TrieNode) => {
|
|
151
|
+
commonPre += cur.val;
|
|
152
|
+
if (commonPre === input) return;
|
|
153
|
+
if (cur.isEnd) return;
|
|
154
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
155
|
+
else return;
|
|
156
|
+
}
|
|
157
|
+
dfs(this._root);
|
|
158
|
+
return commonPre === input;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Retrieve the longest common prefix of all the words
|
|
162
|
+
getLongestCommonPrefix(): string {
|
|
163
|
+
let commonPre = '';
|
|
164
|
+
const dfs = (cur: TrieNode) => {
|
|
165
|
+
commonPre += cur.val;
|
|
166
|
+
if (cur.isEnd) return;
|
|
167
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
168
|
+
else return;
|
|
169
|
+
}
|
|
170
|
+
dfs(this._root);
|
|
171
|
+
return commonPre;
|
|
172
|
+
}
|
|
123
173
|
|
|
124
174
|
getAll(prefix = ''): string[] {
|
|
125
175
|
const words: string[] = [];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type VertexId = string | number;
|
|
2
|
+
export type DijkstraResult<V> =
|
|
3
|
+
{ distMap: Map<V, number>, preMap: Map<V, V | null>, seen: Set<V>, paths: V[][], minDist: number, minPath: V[] }
|
|
4
|
+
| null;
|
|
5
|
+
|
|
6
|
+
export interface IGraph<V, E> {
|
|
7
|
+
|
|
8
|
+
containsVertex(vertexOrId: V | VertexId): boolean;
|
|
9
|
+
|
|
10
|
+
getVertex(vertexOrId: VertexId | V): V | null;
|
|
11
|
+
|
|
12
|
+
getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
13
|
+
|
|
14
|
+
vertexSet(): Map<VertexId, V>;
|
|
15
|
+
|
|
16
|
+
addVertex(v: V): boolean;
|
|
17
|
+
|
|
18
|
+
removeVertex(vertexOrId: V | VertexId): boolean;
|
|
19
|
+
|
|
20
|
+
removeAllVertices(vertices: V[] | VertexId[]): boolean;
|
|
21
|
+
|
|
22
|
+
degreeOf(vertexOrId: V | VertexId): number;
|
|
23
|
+
|
|
24
|
+
edgesOf(vertexOrId: V | VertexId): E[];
|
|
25
|
+
|
|
26
|
+
containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
|
|
27
|
+
|
|
28
|
+
// containsEdge(e: E): boolean;
|
|
29
|
+
|
|
30
|
+
getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
31
|
+
|
|
32
|
+
// getAllEdges(src: V, dest: V): E[];
|
|
33
|
+
|
|
34
|
+
edgeSet(): E[];
|
|
35
|
+
|
|
36
|
+
addEdge(edge: E): boolean;
|
|
37
|
+
|
|
38
|
+
removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
39
|
+
|
|
40
|
+
removeEdge(edge: E): E | null;
|
|
41
|
+
|
|
42
|
+
// removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[];
|
|
43
|
+
|
|
44
|
+
// removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean;
|
|
45
|
+
|
|
46
|
+
setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean;
|
|
47
|
+
|
|
48
|
+
getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
|
|
49
|
+
|
|
50
|
+
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BinaryTreeNode } from "../binary-tree";
|
|
2
|
+
|
|
3
|
+
export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
|
|
4
|
+
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
|
|
5
|
+
export type DFSOrderPattern = 'in' | 'pre' | 'post';
|
|
6
|
+
export type BinaryTreeNodeId = number;
|
|
7
|
+
export type BinaryTreeDeleted<T> = { deleted: BinaryTreeNode<T> | null | undefined, needBalanced: BinaryTreeNode<T> | null };
|
|
8
|
+
export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
|
|
9
|
+
export type ResultsByProperty<T> = ResultByProperty<T>[];
|
|
10
|
+
|
|
11
|
+
export interface BinaryTreeNodeObj<T> {
|
|
12
|
+
id: BinaryTreeNodeId;
|
|
13
|
+
val: T;
|
|
14
|
+
count?: number;
|
|
15
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BSTNode } from '../binary-tree';
|
|
2
|
+
import type {BinaryTreeNodeId} from './binary-tree';
|
|
3
|
+
|
|
4
|
+
export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
|
|
5
|
+
export type BSTDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {VertexId} from './abstract-graph';
|
|
2
|
+
|
|
3
|
+
export interface IDirectedGraph<V, E> {
|
|
4
|
+
incomingEdgesOf(vertex: V): E[];
|
|
5
|
+
|
|
6
|
+
outgoingEdgesOf(vertex: V): E[];
|
|
7
|
+
|
|
8
|
+
inDegreeOf(vertexOrId: V | VertexId): number;
|
|
9
|
+
|
|
10
|
+
outDegreeOf(vertexOrId: V | VertexId): number;
|
|
11
|
+
|
|
12
|
+
getEdgeSrc(e: E): V | null;
|
|
13
|
+
|
|
14
|
+
getEdgeDest(e: E): V | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 0 means unknown, 1 means visiting, 2 means visited;
|
|
18
|
+
export type TopologicalStatus = 0 | 1 | 2;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DoublyLinkedListGetBy = 'node' | 'val';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './binary-tree';
|
|
2
|
+
export * from './bst';
|
|
3
|
+
export * from './avl-tree';
|
|
4
|
+
export * from './segment-tree';
|
|
5
|
+
export * from './tree-multiset';
|
|
6
|
+
export * from './abstract-graph';
|
|
7
|
+
export * from './directed-graph';
|
|
8
|
+
export * from './priority-queue';
|
|
9
|
+
export * from './heap';
|
|
10
|
+
export * from './singly-linked-list';
|
|
11
|
+
export * from './doubly-linked-list';
|
|
12
|
+
export * from './navigator';
|
|
13
|
+
export * from './utils';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Direction = 'up' | 'right' | 'down' | 'left';
|
|
2
|
+
export type Turning = { [key in Direction]: Direction };
|
|
3
|
+
export interface NavigatorParams<T> {
|
|
4
|
+
matrix: T[][],
|
|
5
|
+
turning: Turning,
|
|
6
|
+
onMove: (cur: [number, number]) => void
|
|
7
|
+
init: {
|
|
8
|
+
cur: [number, number],
|
|
9
|
+
charDir: Direction,
|
|
10
|
+
VISITED: T,
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type SegmentTreeNodeVal = number;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SinglyLinkedList } from "../linked-list";
|
|
2
|
+
|
|
3
|
+
/** Type used for filter and find methods, returning a boolean */
|
|
4
|
+
export type TTestFunction<NodeData> = (
|
|
5
|
+
data: NodeData,
|
|
6
|
+
index: number,
|
|
7
|
+
list: SinglyLinkedList<NodeData>,
|
|
8
|
+
) => boolean;
|
|
9
|
+
|
|
10
|
+
/** Type used for map and forEach methods, returning anything */
|
|
11
|
+
export type TMapFunction<NodeData> = (
|
|
12
|
+
data: any,
|
|
13
|
+
index: number,
|
|
14
|
+
list: SinglyLinkedList<NodeData>,
|
|
15
|
+
) => any;
|
|
@@ -74,9 +74,9 @@ export type DebounceOptions = {
|
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
export interface DebouncedFunction<F extends Procedure> {
|
|
77
|
-
(this: ThisParameterType<F>, ...args: Parameters<F>): void;
|
|
78
|
-
|
|
79
77
|
cancel: () => void;
|
|
78
|
+
|
|
79
|
+
(this: ThisParameterType<F>, ...args: [...Parameters<F>]): void;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
export type MonthKey =
|
|
@@ -123,10 +123,10 @@ export class TreeNode<T> {
|
|
|
123
123
|
if (!this.children) {
|
|
124
124
|
this.children = [];
|
|
125
125
|
}
|
|
126
|
-
if (children instanceof
|
|
127
|
-
this.children = this.children.concat(children);
|
|
128
|
-
} else {
|
|
126
|
+
if (children instanceof TreeNode) {
|
|
129
127
|
this.children.push(children);
|
|
128
|
+
} else {
|
|
129
|
+
this.children = this.children.concat(children);
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
@@ -155,4 +155,19 @@ export class TreeNode<T> {
|
|
|
155
155
|
|
|
156
156
|
export type OrderType = 'InOrder' | 'PreOrder' | 'PostOrder'
|
|
157
157
|
|
|
158
|
+
export type DeepProxy<T> = T extends (...args: any[]) => infer R
|
|
159
|
+
? (...args: [...Parameters<T>]) => DeepProxy<R>
|
|
160
|
+
: T extends object
|
|
161
|
+
? { [K in keyof T]: DeepProxy<T[K]> }
|
|
162
|
+
: T;
|
|
163
|
+
|
|
164
|
+
export type DeepProxyOnChange = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
165
|
+
|
|
166
|
+
export type DeepProxyOnGet = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
167
|
+
|
|
168
|
+
export type CurryFunc<T> = T extends (...args: infer Args) => infer R
|
|
169
|
+
? Args extends [infer Arg, ...infer RestArgs]
|
|
170
|
+
? (arg: Arg) => CurryFunc<(...args: RestArgs) => R>
|
|
171
|
+
: R
|
|
172
|
+
: T;
|
|
158
173
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|