data-structure-typed 0.9.16 → 1.12.10
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/README.md +102 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
- package/dist/data-structures/binary-tree/avl-tree.js +57 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
- package/dist/data-structures/binary-tree/binary-tree.js +283 -15
- package/dist/data-structures/binary-tree/bst.d.ts +16 -0
- package/dist/data-structures/binary-tree/bst.js +12 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/segment-tree.js +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
- package/dist/data-structures/graph/abstract-graph.js +166 -12
- package/dist/data-structures/graph/directed-graph.d.ts +111 -1
- package/dist/data-structures/graph/directed-graph.js +122 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +67 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
- package/dist/data-structures/hash/coordinate-map.js +32 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +25 -0
- package/dist/data-structures/heap/heap.d.ts +26 -31
- package/dist/data-structures/heap/heap.js +23 -32
- package/dist/data-structures/heap/max-heap.d.ts +6 -1
- package/dist/data-structures/heap/max-heap.js +6 -1
- package/dist/data-structures/heap/min-heap.d.ts +7 -1
- package/dist/data-structures/heap/min-heap.js +7 -1
- package/dist/data-structures/index.d.ts +0 -1
- package/dist/data-structures/index.js +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
- package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
- package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +11 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
- package/dist/data-structures/matrix/matrix2d.js +82 -2
- package/dist/data-structures/matrix/navigator.d.ts +32 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +139 -15
- package/dist/data-structures/matrix/vector2d.js +139 -15
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
- package/dist/data-structures/priority-queue/priority-queue.js +126 -0
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +69 -0
- package/dist/data-structures/queue/queue.d.ts +30 -32
- package/dist/data-structures/queue/queue.js +30 -32
- package/dist/data-structures/stack/stack.d.ts +26 -31
- package/dist/data-structures/stack/stack.js +26 -31
- package/dist/data-structures/trie/trie.d.ts +26 -6
- package/dist/data-structures/trie/trie.js +26 -7
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/index.d.ts +1 -1
- package/dist/data-structures/types/index.js +1 -1
- package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
- package/dist/data-structures/types/tree-multiset.d.ts +1 -1
- package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
- package/dist/{data-structures → utils}/types/utils.js +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +19 -11
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +57 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
- package/src/data-structures/binary-tree/binary-tree.ts +288 -18
- package/src/data-structures/binary-tree/bst.ts +16 -1
- package/src/data-structures/binary-tree/segment-tree.ts +5 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
- package/src/data-structures/diagrams/README.md +5 -7
- package/src/data-structures/graph/abstract-graph.ts +167 -15
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +127 -5
- package/src/data-structures/graph/undirected-graph.ts +67 -1
- package/src/data-structures/hash/coordinate-map.ts +32 -0
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/heap/heap.ts +27 -32
- package/src/data-structures/heap/max-heap.ts +6 -1
- package/src/data-structures/heap/min-heap.ts +7 -1
- package/src/data-structures/index.ts +0 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
- package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +82 -2
- package/src/data-structures/matrix/navigator.ts +32 -0
- package/src/data-structures/matrix/vector2d.ts +139 -15
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/priority-queue.ts +130 -0
- package/src/data-structures/queue/deque.ts +69 -0
- package/src/data-structures/queue/queue.ts +30 -32
- package/src/data-structures/stack/stack.ts +26 -31
- package/src/data-structures/trie/trie.ts +26 -7
- package/src/data-structures/types/avl-tree.ts +1 -1
- package/src/data-structures/types/binary-tree.ts +1 -1
- package/src/data-structures/types/bst.ts +1 -1
- package/src/data-structures/types/index.ts +1 -1
- package/src/data-structures/types/navigator.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -15
- package/src/data-structures/types/tree-multiset.ts +1 -1
- package/src/{data-structures → utils}/trampoline.ts +6 -6
- package/src/utils/types/index.ts +1 -0
- package/src/{data-structures → utils}/types/utils.ts +7 -0
- package/src/utils/utils.ts +12 -2
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/tests/unit/data-structures/graph/index.ts +3 -0
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- 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/dist/{data-structures → utils}/trampoline.js +0 -0
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
export class TrieNode {
|
|
2
6
|
protected _value;
|
|
3
7
|
|
|
@@ -114,8 +118,9 @@ export class Trie {
|
|
|
114
118
|
|
|
115
119
|
// --- start additional methods ---
|
|
116
120
|
/**
|
|
117
|
-
* Only can present as a prefix, not a word
|
|
118
|
-
* @param input
|
|
121
|
+
* The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
|
|
122
|
+
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
123
|
+
* @returns a boolean value.
|
|
119
124
|
*/
|
|
120
125
|
isAbsPrefix(input: string): boolean {
|
|
121
126
|
let cur = this._root;
|
|
@@ -128,8 +133,9 @@ export class Trie {
|
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
/**
|
|
131
|
-
* Can present as a abs prefix or word
|
|
132
|
-
* @param input
|
|
136
|
+
* The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
|
|
137
|
+
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
138
|
+
* @returns a boolean value.
|
|
133
139
|
*/
|
|
134
140
|
isPrefix(input: string): boolean {
|
|
135
141
|
let cur = this._root;
|
|
@@ -142,8 +148,10 @@ export class Trie {
|
|
|
142
148
|
}
|
|
143
149
|
|
|
144
150
|
/**
|
|
145
|
-
* Check if the input string is the common prefix of all the words
|
|
146
|
-
* @param input
|
|
151
|
+
* The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
|
|
152
|
+
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
153
|
+
* in the Trie data structure.
|
|
154
|
+
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
147
155
|
*/
|
|
148
156
|
isCommonPrefix(input: string): boolean {
|
|
149
157
|
let commonPre = '';
|
|
@@ -158,7 +166,12 @@ export class Trie {
|
|
|
158
166
|
return commonPre === input;
|
|
159
167
|
}
|
|
160
168
|
|
|
161
|
-
|
|
169
|
+
/**
|
|
170
|
+
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
171
|
+
* structure.
|
|
172
|
+
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
173
|
+
* Trie.
|
|
174
|
+
*/
|
|
162
175
|
getLongestCommonPrefix(): string {
|
|
163
176
|
let commonPre = '';
|
|
164
177
|
const dfs = (cur: TrieNode) => {
|
|
@@ -171,6 +184,12 @@ export class Trie {
|
|
|
171
184
|
return commonPre;
|
|
172
185
|
}
|
|
173
186
|
|
|
187
|
+
/**
|
|
188
|
+
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
189
|
+
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
190
|
+
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
191
|
+
* @returns an array of strings.
|
|
192
|
+
*/
|
|
174
193
|
getAll(prefix = ''): string[] {
|
|
175
194
|
const words: string[] = [];
|
|
176
195
|
|
|
@@ -1,15 +1 @@
|
|
|
1
|
-
|
|
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;
|
|
1
|
+
export {}
|
|
@@ -1,20 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
6
|
+
|
|
1
7
|
export const THUNK_SYMBOL = Symbol('thunk')
|
|
2
8
|
|
|
3
9
|
export const isThunk = (fnOrValue: any) => {
|
|
4
10
|
return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
|
|
5
11
|
}
|
|
6
12
|
|
|
7
|
-
type ToThunkFn = () => ReturnType<TrlFn>;
|
|
8
|
-
|
|
9
|
-
type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
|
|
10
|
-
|
|
11
13
|
export const toThunk = (fn: ToThunkFn): Thunk => {
|
|
12
14
|
const thunk = () => fn()
|
|
13
15
|
thunk.__THUNK__ = THUNK_SYMBOL
|
|
14
16
|
return thunk
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
type TrlFn = (...args: any[]) => any;
|
|
18
19
|
export const trampoline = (fn: TrlFn) => {
|
|
19
20
|
const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
|
|
20
21
|
|
|
@@ -32,7 +33,6 @@ export const trampoline = (fn: TrlFn) => {
|
|
|
32
33
|
)
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
type TrlAsyncFn = (...args: any[]) => any;
|
|
36
36
|
export const trampolineAsync = (fn: TrlAsyncFn) => {
|
|
37
37
|
const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
|
|
38
38
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -171,3 +171,10 @@ export type CurryFunc<T> = T extends (...args: infer Args) => infer R
|
|
|
171
171
|
: R
|
|
172
172
|
: T;
|
|
173
173
|
|
|
174
|
+
|
|
175
|
+
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
176
|
+
const THUNK_SYMBOL = Symbol('thunk')
|
|
177
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
|
|
178
|
+
export type TrlFn = (...args: any[]) => any;
|
|
179
|
+
export type TrlAsyncFn = (...args: any[]) => any;
|
|
180
|
+
|
package/src/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _ from 'lodash';
|
|
2
|
-
import {AnyFunction} from '
|
|
2
|
+
import {AnyFunction} from './types';
|
|
3
3
|
|
|
4
4
|
export type JSONSerializable = {
|
|
5
5
|
[key: string]: any
|
|
@@ -323,7 +323,17 @@ export class StringUtil {
|
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
export type CaseType =
|
|
326
|
+
export type CaseType =
|
|
327
|
+
'camel'
|
|
328
|
+
| 'snake'
|
|
329
|
+
| 'pascal'
|
|
330
|
+
| 'constant'
|
|
331
|
+
| 'kebab'
|
|
332
|
+
| 'lower'
|
|
333
|
+
| 'title'
|
|
334
|
+
| 'sentence'
|
|
335
|
+
| 'path'
|
|
336
|
+
| 'dot';
|
|
327
337
|
export const deepKeysConvert = (obj: any, toType?: CaseType): any => {
|
|
328
338
|
const _toType = toType || 'snake';
|
|
329
339
|
if (Array.isArray(obj)) {
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import {BST, BSTNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('bst-case6', () => {
|
|
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
|
+
|
|
7
|
+
const tree = new BST();
|
|
8
|
+
|
|
9
|
+
expect(tree).toBeInstanceOf(BST);
|
|
10
|
+
|
|
11
|
+
for (const i of arr) tree.put(i, i);
|
|
12
|
+
|
|
13
|
+
expect(tree.root).toBeInstanceOf(BSTNode);
|
|
14
|
+
if (tree.root) expect(tree.root.id).toBe(11);
|
|
15
|
+
expect(tree.count).toBe(16);
|
|
16
|
+
expect(tree.has(6)).toBe(true);
|
|
17
|
+
|
|
18
|
+
const node6 = tree.get(6);
|
|
19
|
+
expect(node6 && tree.getHeight(node6)).toBe(2);
|
|
20
|
+
expect(node6 && tree.getDepth(node6)).toBe(3);
|
|
21
|
+
const getNodeById = tree.get(10, 'id');
|
|
22
|
+
expect(getNodeById?.id).toBe(10);
|
|
23
|
+
|
|
24
|
+
const getNodesByCount = tree.getNodes(1, 'count');
|
|
25
|
+
expect(getNodesByCount.length).toBe(16);
|
|
26
|
+
|
|
27
|
+
const getMinNodeByRoot = tree.getLeftMost();
|
|
28
|
+
expect(getMinNodeByRoot?.id).toBe(1);
|
|
29
|
+
|
|
30
|
+
const node15 = tree.get(15);
|
|
31
|
+
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
|
32
|
+
expect(getMinNodeBySpecificNode?.id).toBe(12);
|
|
33
|
+
|
|
34
|
+
const subTreeSum = node15 && tree.subTreeSum(node15);
|
|
35
|
+
expect(subTreeSum).toBe(70);
|
|
36
|
+
|
|
37
|
+
const lesserSum = tree.lesserSum(10);
|
|
38
|
+
expect(lesserSum).toBe(45);
|
|
39
|
+
|
|
40
|
+
expect(node15).toBeInstanceOf(BSTNode);
|
|
41
|
+
if (node15 instanceof BSTNode) {
|
|
42
|
+
const subTreeAdd = tree.subTreeAdd(node15, 1, 'count');
|
|
43
|
+
expect(subTreeAdd).toBeDefined();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const node11 = tree.get(11);
|
|
48
|
+
expect(node11).toBeInstanceOf(BSTNode);
|
|
49
|
+
if (node11 instanceof BSTNode) {
|
|
50
|
+
const allGreaterNodesAdd = tree.allGreaterNodesAdd(node11, 2, 'count');
|
|
51
|
+
expect(allGreaterNodesAdd).toBeDefined();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const dfs = tree.DFS('in', 'node');
|
|
55
|
+
expect(dfs[0].id).toBe(1);
|
|
56
|
+
expect(dfs[dfs.length - 1].id).toBe(16);
|
|
57
|
+
|
|
58
|
+
tree.balance();
|
|
59
|
+
const bfs = tree.BFS('node');
|
|
60
|
+
expect(tree.isBalanced()).toBe(true);
|
|
61
|
+
expect(bfs[0].id).toBe(8);
|
|
62
|
+
expect(bfs[bfs.length - 1].id).toBe(16);
|
|
63
|
+
|
|
64
|
+
const removed11 = tree.remove(11, true);
|
|
65
|
+
expect(removed11).toBeInstanceOf(Array);
|
|
66
|
+
expect(removed11[0]).toBeDefined();
|
|
67
|
+
expect(removed11[0].deleted).toBeDefined();
|
|
68
|
+
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
|
|
69
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
70
|
+
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
71
|
+
|
|
72
|
+
const removed1 = tree.remove(1, true);
|
|
73
|
+
expect(removed1).toBeInstanceOf(Array);
|
|
74
|
+
expect(removed1[0]).toBeDefined();
|
|
75
|
+
expect(removed1[0].deleted).toBeDefined();
|
|
76
|
+
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
|
|
77
|
+
|
|
78
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
79
|
+
expect(tree.getHeight()).toBe(4);
|
|
80
|
+
|
|
81
|
+
const removed4 = tree.remove(4, true);
|
|
82
|
+
expect(removed4).toBeInstanceOf(Array);
|
|
83
|
+
expect(removed4[0]).toBeDefined();
|
|
84
|
+
expect(removed4[0].deleted).toBeDefined();
|
|
85
|
+
if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4);
|
|
86
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
87
|
+
expect(tree.getHeight()).toBe(4);
|
|
88
|
+
|
|
89
|
+
const removed10 = tree.remove(10, true);
|
|
90
|
+
expect(removed10).toBeInstanceOf(Array);
|
|
91
|
+
expect(removed10[0]).toBeDefined();
|
|
92
|
+
expect(removed10[0].deleted).toBeDefined();
|
|
93
|
+
if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10);
|
|
94
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
95
|
+
expect(tree.getHeight()).toBe(4);
|
|
96
|
+
|
|
97
|
+
const removed15 = tree.remove(15, true);
|
|
98
|
+
expect(removed15).toBeInstanceOf(Array);
|
|
99
|
+
expect(removed15[0]).toBeDefined();
|
|
100
|
+
expect(removed15[0].deleted).toBeDefined();
|
|
101
|
+
if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15);
|
|
102
|
+
|
|
103
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
104
|
+
expect(tree.getHeight()).toBe(3);
|
|
105
|
+
|
|
106
|
+
const removed5 = tree.remove(5, true);
|
|
107
|
+
expect(removed5).toBeInstanceOf(Array);
|
|
108
|
+
expect(removed5[0]).toBeDefined();
|
|
109
|
+
expect(removed5[0].deleted).toBeDefined();
|
|
110
|
+
if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5);
|
|
111
|
+
|
|
112
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
113
|
+
expect(tree.getHeight()).toBe(3);
|
|
114
|
+
|
|
115
|
+
const removed13 = tree.remove(13, true);
|
|
116
|
+
expect(removed13).toBeInstanceOf(Array);
|
|
117
|
+
expect(removed13[0]).toBeDefined();
|
|
118
|
+
expect(removed13[0].deleted).toBeDefined();
|
|
119
|
+
if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13);
|
|
120
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
121
|
+
expect(tree.getHeight()).toBe(3);
|
|
122
|
+
|
|
123
|
+
const removed3 = tree.remove(3, true);
|
|
124
|
+
expect(removed3).toBeInstanceOf(Array);
|
|
125
|
+
expect(removed3[0]).toBeDefined();
|
|
126
|
+
expect(removed3[0].deleted).toBeDefined();
|
|
127
|
+
if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3);
|
|
128
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
129
|
+
expect(tree.getHeight()).toBe(3);
|
|
130
|
+
|
|
131
|
+
const removed8 = tree.remove(8, true);
|
|
132
|
+
expect(removed8).toBeInstanceOf(Array);
|
|
133
|
+
expect(removed8[0]).toBeDefined();
|
|
134
|
+
expect(removed8[0].deleted).toBeDefined();
|
|
135
|
+
if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8);
|
|
136
|
+
expect(tree.isAVLBalanced()).toBe(true);
|
|
137
|
+
expect(tree.getHeight()).toBe(3);
|
|
138
|
+
|
|
139
|
+
const removed6 = tree.remove(6, true);
|
|
140
|
+
expect(removed6).toBeInstanceOf(Array);
|
|
141
|
+
expect(removed6[0]).toBeDefined();
|
|
142
|
+
expect(removed6[0].deleted).toBeDefined();
|
|
143
|
+
if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6);
|
|
144
|
+
expect(tree.remove(6, true).length).toBe(0);
|
|
145
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
146
|
+
expect(tree.getHeight()).toBe(3);
|
|
147
|
+
|
|
148
|
+
const removed7 = tree.remove(7, true);
|
|
149
|
+
expect(removed7).toBeInstanceOf(Array);
|
|
150
|
+
expect(removed7[0]).toBeDefined();
|
|
151
|
+
expect(removed7[0].deleted).toBeDefined();
|
|
152
|
+
if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7);
|
|
153
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
154
|
+
expect(tree.getHeight()).toBe(3);
|
|
155
|
+
|
|
156
|
+
const removed9 = tree.remove(9, true);
|
|
157
|
+
expect(removed9).toBeInstanceOf(Array);
|
|
158
|
+
expect(removed9[0]).toBeDefined();
|
|
159
|
+
expect(removed9[0].deleted).toBeDefined();
|
|
160
|
+
if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9);
|
|
161
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
162
|
+
expect(tree.getHeight()).toBe(3);
|
|
163
|
+
|
|
164
|
+
const removed14 = tree.remove(14, true);
|
|
165
|
+
expect(removed14).toBeInstanceOf(Array);
|
|
166
|
+
expect(removed14[0]).toBeDefined();
|
|
167
|
+
expect(removed14[0].deleted).toBeDefined();
|
|
168
|
+
if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14);
|
|
169
|
+
expect(tree.isAVLBalanced()).toBe(false);
|
|
170
|
+
expect(tree.getHeight()).toBe(2);
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
expect(!tree.isAVLBalanced()).toBe(true);
|
|
174
|
+
|
|
175
|
+
const lastBFSIds = tree.BFS();
|
|
176
|
+
expect(lastBFSIds[0]).toBe(2);
|
|
177
|
+
expect(lastBFSIds[1]).toBe(12);
|
|
178
|
+
expect(lastBFSIds[2]).toBe(16);
|
|
179
|
+
|
|
180
|
+
const lastBFSNodes = tree.BFS('node');
|
|
181
|
+
expect(lastBFSNodes[0].id).toBe(2);
|
|
182
|
+
expect(lastBFSNodes[1].id).toBe(12);
|
|
183
|
+
expect(lastBFSNodes[2].id).toBe(16);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DirectedGraph, DirectedVertex, DirectedEdge } from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('DirectedGraph', () => {
|
|
4
|
+
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
graph = new DirectedGraph();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should add vertices', () => {
|
|
11
|
+
const vertex1 = new DirectedVertex('A');
|
|
12
|
+
const vertex2 = new DirectedVertex('B');
|
|
13
|
+
|
|
14
|
+
graph.addVertex(vertex1);
|
|
15
|
+
graph.addVertex(vertex2);
|
|
16
|
+
|
|
17
|
+
expect(graph.containsVertex(vertex1)).toBe(true);
|
|
18
|
+
expect(graph.containsVertex(vertex2)).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should add edges', () => {
|
|
22
|
+
const vertex1 = new DirectedVertex('A');
|
|
23
|
+
const vertex2 = new DirectedVertex('B');
|
|
24
|
+
const edge = new DirectedEdge('A', 'B');
|
|
25
|
+
|
|
26
|
+
graph.addVertex(vertex1);
|
|
27
|
+
graph.addVertex(vertex2);
|
|
28
|
+
graph.addEdge(edge);
|
|
29
|
+
|
|
30
|
+
expect(graph.containsEdge('A', 'B')).toBe(true);
|
|
31
|
+
expect(graph.containsEdge('B', 'A')).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should remove edges', () => {
|
|
35
|
+
const vertex1 = new DirectedVertex('A');
|
|
36
|
+
const vertex2 = new DirectedVertex('B');
|
|
37
|
+
const edge = new DirectedEdge('A', 'B');
|
|
38
|
+
|
|
39
|
+
graph.addVertex(vertex1);
|
|
40
|
+
graph.addVertex(vertex2);
|
|
41
|
+
graph.addEdge(edge);
|
|
42
|
+
|
|
43
|
+
expect(graph.removeEdge(edge)).toBe(edge);
|
|
44
|
+
expect(graph.containsEdge('A', 'B')).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Add more test cases for other methods...
|
|
48
|
+
|
|
49
|
+
it('should perform topological sort', () => {
|
|
50
|
+
// Create a graph with vertices and edges
|
|
51
|
+
const vertexA = new DirectedVertex('A');
|
|
52
|
+
const vertexB = new DirectedVertex('B');
|
|
53
|
+
const vertexC = new DirectedVertex('C');
|
|
54
|
+
const edgeAB = new DirectedEdge('A', 'B');
|
|
55
|
+
const edgeBC = new DirectedEdge('B', 'C');
|
|
56
|
+
|
|
57
|
+
graph.addVertex(vertexA);
|
|
58
|
+
graph.addVertex(vertexB);
|
|
59
|
+
graph.addVertex(vertexC);
|
|
60
|
+
graph.addEdge(edgeAB);
|
|
61
|
+
graph.addEdge(edgeBC);
|
|
62
|
+
|
|
63
|
+
// Perform topological sort
|
|
64
|
+
const topologicalOrder = graph.topologicalSort();
|
|
65
|
+
|
|
66
|
+
if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']);
|
|
67
|
+
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Add more test cases for other methods...
|
|
71
|
+
});
|
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
File without changes
|