data-structure-typed 1.12.10 → 1.12.21
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 +7 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +14 -5
- package/dist/data-structures/binary-tree/avl-tree.js +15 -6
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +26 -17
- package/dist/data-structures/binary-tree/binary-tree.js +72 -62
- package/dist/data-structures/binary-tree/bst.d.ts +92 -5
- package/dist/data-structures/binary-tree/bst.js +89 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +41 -2
- package/dist/data-structures/binary-tree/segment-tree.js +41 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/data-structures/graph/abstract-graph.js +12 -4
- package/dist/data-structures/graph/directed-graph.d.ts +18 -4
- package/dist/data-structures/graph/directed-graph.js +24 -37
- package/dist/data-structures/graph/undirected-graph.d.ts +13 -0
- package/dist/data-structures/graph/undirected-graph.js +18 -2
- package/dist/data-structures/hash/coordinate-map.d.ts +5 -2
- package/dist/data-structures/hash/coordinate-map.js +5 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +5 -2
- package/dist/data-structures/hash/coordinate-set.js +5 -2
- package/dist/data-structures/heap/heap.d.ts +9 -6
- package/dist/data-structures/heap/heap.js +8 -8
- package/dist/data-structures/heap/max-heap.d.ts +5 -2
- package/dist/data-structures/heap/max-heap.js +5 -2
- package/dist/data-structures/heap/min-heap.d.ts +5 -2
- package/dist/data-structures/heap/min-heap.js +5 -2
- 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 +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +4 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +5 -2
- package/dist/data-structures/matrix/matrix.d.ts +5 -2
- package/dist/data-structures/matrix/matrix.js +5 -2
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
- package/dist/data-structures/matrix/matrix2d.js +5 -2
- package/dist/data-structures/matrix/navigator.d.ts +5 -2
- package/dist/data-structures/matrix/vector2d.d.ts +5 -2
- package/dist/data-structures/matrix/vector2d.js +5 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -4
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +12 -9
- package/dist/data-structures/queue/deque.js +12 -9
- package/dist/data-structures/queue/queue.d.ts +4 -4
- package/dist/data-structures/queue/queue.js +4 -4
- package/dist/data-structures/stack/stack.d.ts +1 -1
- package/dist/data-structures/stack/stack.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +6 -3
- package/dist/data-structures/trie/trie.js +7 -4
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/utils.d.ts +8 -10
- package/dist/utils/types/utils.js +0 -1
- package/dist/utils/utils.d.ts +18 -8
- package/dist/utils/utils.js +93 -47
- package/package.json +3 -3
- package/src/assets/logo.png +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +15 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +11 -2
- package/src/data-structures/binary-tree/binary-tree.ts +70 -58
- package/src/data-structures/binary-tree/bst.ts +94 -7
- package/src/data-structures/binary-tree/segment-tree.ts +41 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +35 -4
- package/src/data-structures/graph/abstract-graph.ts +12 -4
- package/src/data-structures/graph/directed-graph.ts +26 -39
- package/src/data-structures/graph/undirected-graph.ts +18 -2
- package/src/data-structures/hash/coordinate-map.ts +5 -2
- package/src/data-structures/hash/coordinate-set.ts +5 -2
- package/src/data-structures/heap/heap.ts +13 -10
- package/src/data-structures/heap/max-heap.ts +5 -2
- package/src/data-structures/heap/min-heap.ts +5 -2
- package/src/data-structures/index.ts +2 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -6
- package/src/data-structures/linked-list/singly-linked-list.ts +5 -2
- package/src/data-structures/matrix/matrix.ts +5 -2
- package/src/data-structures/matrix/matrix2d.ts +5 -2
- package/src/data-structures/matrix/navigator.ts +5 -2
- package/src/data-structures/matrix/vector2d.ts +5 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +5 -2
- package/src/data-structures/priority-queue/min-priority-queue.ts +5 -2
- package/src/data-structures/priority-queue/priority-queue.ts +7 -4
- package/src/data-structures/queue/deque.ts +12 -9
- package/src/data-structures/queue/queue.ts +4 -4
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +7 -4
- package/src/utils/index.ts +2 -1
- package/src/utils/types/utils.ts +10 -12
- package/src/utils/utils.ts +57 -11
- package/tests/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/tests/unit/data-structures/graph/directed-graph.test.ts +1 -0
- package/dist/utils/trampoline.d.ts +0 -14
- package/dist/utils/trampoline.js +0 -130
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -85
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2046
- package/docs/classes/AVLTreeNode.html +0 -423
- package/docs/classes/AaTree.html +0 -117
- package/docs/classes/AbstractEdge.html +0 -198
- package/docs/classes/AbstractGraph.html +0 -891
- package/docs/classes/AbstractVertex.html +0 -164
- package/docs/classes/ArrayDeque.html +0 -384
- package/docs/classes/BST.html +0 -1893
- package/docs/classes/BSTNode.html +0 -425
- package/docs/classes/BTree.html +0 -117
- package/docs/classes/BinaryIndexedTree.html +0 -244
- package/docs/classes/BinaryTree.html +0 -1754
- package/docs/classes/BinaryTreeNode.html +0 -396
- package/docs/classes/Character.html +0 -165
- package/docs/classes/CoordinateMap.html +0 -394
- package/docs/classes/CoordinateSet.html +0 -355
- package/docs/classes/Deque.html +0 -617
- package/docs/classes/DirectedEdge.html +0 -247
- package/docs/classes/DirectedGraph.html +0 -1207
- package/docs/classes/DirectedVertex.html +0 -154
- package/docs/classes/DoublyLinkedList.html +0 -619
- package/docs/classes/DoublyLinkedListNode.html +0 -160
- package/docs/classes/Heap.html +0 -315
- package/docs/classes/Matrix2D.html +0 -447
- package/docs/classes/MatrixNTI2D.html +0 -181
- package/docs/classes/MaxHeap.html +0 -325
- package/docs/classes/MaxPriorityQueue.html +0 -668
- package/docs/classes/MinHeap.html +0 -326
- package/docs/classes/MinPriorityQueue.html +0 -668
- package/docs/classes/Navigator.html +0 -285
- package/docs/classes/ObjectDeque.html +0 -289
- package/docs/classes/PriorityQueue.html +0 -643
- package/docs/classes/Queue.html +0 -337
- package/docs/classes/RBTree.html +0 -117
- package/docs/classes/SegmentTree.html +0 -234
- package/docs/classes/SegmentTreeNode.html +0 -302
- package/docs/classes/SinglyLinkedList.html +0 -1035
- package/docs/classes/SinglyLinkedListNode.html +0 -304
- package/docs/classes/SplayTree.html +0 -117
- package/docs/classes/Stack.html +0 -313
- package/docs/classes/TreeMultiSet.html +0 -1897
- package/docs/classes/Trie.html +0 -317
- package/docs/classes/TrieNode.html +0 -221
- package/docs/classes/TwoThreeTree.html +0 -117
- package/docs/classes/UndirectedEdge.html +0 -220
- package/docs/classes/UndirectedGraph.html +0 -1006
- package/docs/classes/UndirectedVertex.html +0 -154
- package/docs/classes/Vector2D.html +0 -746
- package/docs/enums/CP.html +0 -126
- package/docs/enums/FamilyPosition.html +0 -126
- package/docs/enums/LoopType.html +0 -119
- package/docs/index.html +0 -288
- package/docs/modules.html +0 -146
- package/src/utils/trampoline.ts +0 -51
package/README.md
CHANGED
|
@@ -15,6 +15,13 @@ yarn add data-structure-typed
|
|
|
15
15
|
```bash
|
|
16
16
|
npm install data-structure-typed
|
|
17
17
|
```
|
|
18
|
+
## Online examples
|
|
19
|
+
|
|
20
|
+
[Online Examples](https://data-structure-typed-examples.vercel.app)
|
|
21
|
+
|
|
22
|
+
## Examples Repository
|
|
23
|
+
|
|
24
|
+
[Example Repository](https://github.com/zrwusa/data-structure-typed-examples)
|
|
18
25
|
|
|
19
26
|
## api docs
|
|
20
27
|
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
import { BST, BSTNode } from './bst';
|
|
6
9
|
import type { AVLTreeDeleted, BinaryTreeNodeId } from '../types';
|
|
7
10
|
export declare class AVLTreeNode<T> extends BSTNode<T> {
|
|
11
|
+
/**
|
|
12
|
+
* The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
|
|
13
|
+
* id, value, and count.
|
|
14
|
+
* @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
|
|
15
|
+
* the current instance.
|
|
16
|
+
*/
|
|
8
17
|
clone(): AVLTreeNode<T>;
|
|
9
18
|
}
|
|
10
19
|
export declare class AVLTree<T> extends BST<T> {
|
|
11
20
|
createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode<T>;
|
|
12
21
|
/**
|
|
13
|
-
* The function overrides the
|
|
22
|
+
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
14
23
|
* balances the tree.
|
|
15
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to
|
|
24
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
|
|
16
25
|
* update in the AVL tree.
|
|
17
26
|
* @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
|
|
18
27
|
* `id`. It can be of type `T` (the generic type) or `null`.
|
|
@@ -21,7 +30,7 @@ export declare class AVLTree<T> extends BST<T> {
|
|
|
21
30
|
* to `1`, indicating that the value should be inserted once.
|
|
22
31
|
* @returns The method is returning either an AVLTreeNode<T> object or null.
|
|
23
32
|
*/
|
|
24
|
-
|
|
33
|
+
add(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null;
|
|
25
34
|
/**
|
|
26
35
|
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
27
36
|
* then balances the tree if necessary.
|
|
@@ -28,8 +28,11 @@ var __values = (this && this.__values) || function(o) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* data-structure-typed
|
|
32
|
+
*
|
|
33
|
+
* @author Tyler Zeng
|
|
34
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
35
|
+
* @license MIT License
|
|
33
36
|
*/
|
|
34
37
|
var bst_1 = require("./bst");
|
|
35
38
|
var AVLTreeNode = /** @class */ (function (_super) {
|
|
@@ -37,6 +40,12 @@ var AVLTreeNode = /** @class */ (function (_super) {
|
|
|
37
40
|
function AVLTreeNode() {
|
|
38
41
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
39
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
|
|
45
|
+
* id, value, and count.
|
|
46
|
+
* @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
|
|
47
|
+
* the current instance.
|
|
48
|
+
*/
|
|
40
49
|
AVLTreeNode.prototype.clone = function () {
|
|
41
50
|
return new AVLTreeNode(this.id, this.val, this.count);
|
|
42
51
|
};
|
|
@@ -52,9 +61,9 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
52
61
|
return new AVLTreeNode(id, val, count);
|
|
53
62
|
};
|
|
54
63
|
/**
|
|
55
|
-
* The function overrides the
|
|
64
|
+
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
56
65
|
* balances the tree.
|
|
57
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to
|
|
66
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
|
|
58
67
|
* update in the AVL tree.
|
|
59
68
|
* @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
|
|
60
69
|
* `id`. It can be of type `T` (the generic type) or `null`.
|
|
@@ -63,8 +72,8 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
63
72
|
* to `1`, indicating that the value should be inserted once.
|
|
64
73
|
* @returns The method is returning either an AVLTreeNode<T> object or null.
|
|
65
74
|
*/
|
|
66
|
-
AVLTree.prototype.
|
|
67
|
-
var inserted = _super.prototype.
|
|
75
|
+
AVLTree.prototype.add = function (id, val, count) {
|
|
76
|
+
var inserted = _super.prototype.add.call(this, id, val, count);
|
|
68
77
|
if (inserted)
|
|
69
78
|
this.balancePath(inserted);
|
|
70
79
|
return inserted;
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
export declare class BinaryIndexedTree {
|
|
6
9
|
private readonly _sumTree;
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
12
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
13
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
14
|
+
* The size of the sum tree array is `n + 1` because
|
|
15
|
+
*/
|
|
7
16
|
constructor(n: number);
|
|
8
17
|
static lowBit(x: number): number;
|
|
9
18
|
/**
|
|
@@ -2,10 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BinaryIndexedTree = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
7
10
|
*/
|
|
8
11
|
var BinaryIndexedTree = /** @class */ (function () {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
14
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
15
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
16
|
+
* The size of the sum tree array is `n + 1` because
|
|
17
|
+
*/
|
|
9
18
|
function BinaryIndexedTree(n) {
|
|
10
19
|
this._sumTree = new Array(n + 1).fill(0);
|
|
11
20
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
import type { BinaryTreeDeleted, BinaryTreeNodeId, BinaryTreeNodePropertyName, DFSOrderPattern, NodeOrPropertyName, ResultsByProperty } from '../types';
|
|
6
9
|
export declare enum FamilyPosition {
|
|
@@ -8,6 +11,12 @@ export declare enum FamilyPosition {
|
|
|
8
11
|
left = 1,
|
|
9
12
|
right = 2
|
|
10
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Enum representing different loop types.
|
|
16
|
+
*
|
|
17
|
+
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
18
|
+
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
19
|
+
*/
|
|
11
20
|
export declare enum LoopType {
|
|
12
21
|
iterative = 1,
|
|
13
22
|
recursive = 2
|
|
@@ -93,34 +102,34 @@ export declare class BinaryTree<T> {
|
|
|
93
102
|
*/
|
|
94
103
|
isEmpty(): boolean;
|
|
95
104
|
/**
|
|
96
|
-
* The function inserts a new node
|
|
97
|
-
* @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
|
|
98
|
-
* `null`. It represents the node that needs to be inserted into the binary tree.
|
|
99
|
-
* @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
|
|
100
|
-
* will be inserted as a child.
|
|
101
|
-
* @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
|
|
102
|
-
*/
|
|
103
|
-
putTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T>): BinaryTreeNode<T> | null | undefined;
|
|
104
|
-
/**
|
|
105
|
-
* The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
105
|
+
* The `add` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
106
106
|
* already exists.
|
|
107
107
|
* @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
|
|
108
108
|
* identify each node in the binary tree.
|
|
109
109
|
* @param {T} val - The value to be inserted into the binary tree.
|
|
110
110
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
111
111
|
* value should be inserted into the binary tree. If not provided, it defaults to 1.
|
|
112
|
-
* @returns The function `
|
|
112
|
+
* @returns The function `add` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
|
|
113
113
|
* is inserted, or `undefined` if the insertion fails.
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
add(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* The function inserts a new node into a binary tree as the left or right child of a given parent node.
|
|
118
|
+
* @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
|
|
119
|
+
* `null`. It represents the node that needs to be inserted into the binary tree.
|
|
120
|
+
* @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
|
|
121
|
+
* will be inserted as a child.
|
|
122
|
+
* @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
|
|
123
|
+
*/
|
|
124
|
+
addTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T>): BinaryTreeNode<T> | null | undefined;
|
|
116
125
|
/**
|
|
117
|
-
* The `
|
|
126
|
+
* The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
|
|
118
127
|
* null/undefined values.
|
|
119
128
|
* @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
|
|
120
129
|
* array of `BinaryTreeNode<T>` objects.
|
|
121
|
-
* @returns The function `
|
|
130
|
+
* @returns The function `addMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
|
|
122
131
|
*/
|
|
123
|
-
|
|
132
|
+
addMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[];
|
|
124
133
|
/**
|
|
125
134
|
* The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
|
|
126
135
|
* was successful.
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
5
8
|
*/
|
|
6
9
|
var __values = (this && this.__values) || function(o) {
|
|
7
10
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
@@ -32,13 +35,20 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
32
35
|
};
|
|
33
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
37
|
exports.BinaryTree = exports.BinaryTreeNode = exports.LoopType = exports.FamilyPosition = void 0;
|
|
35
|
-
var
|
|
38
|
+
var utils_1 = require("../../utils");
|
|
39
|
+
/* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */
|
|
36
40
|
var FamilyPosition;
|
|
37
41
|
(function (FamilyPosition) {
|
|
38
42
|
FamilyPosition[FamilyPosition["root"] = 0] = "root";
|
|
39
43
|
FamilyPosition[FamilyPosition["left"] = 1] = "left";
|
|
40
44
|
FamilyPosition[FamilyPosition["right"] = 2] = "right";
|
|
41
45
|
})(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
|
|
46
|
+
/**
|
|
47
|
+
* Enum representing different loop types.
|
|
48
|
+
*
|
|
49
|
+
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
50
|
+
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
51
|
+
*/
|
|
42
52
|
var LoopType;
|
|
43
53
|
(function (LoopType) {
|
|
44
54
|
LoopType[LoopType["iterative"] = 1] = "iterative";
|
|
@@ -255,60 +265,17 @@ var BinaryTree = /** @class */ (function () {
|
|
|
255
265
|
return this.size === 0;
|
|
256
266
|
};
|
|
257
267
|
/**
|
|
258
|
-
* The function inserts a new node
|
|
259
|
-
* @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
|
|
260
|
-
* `null`. It represents the node that needs to be inserted into the binary tree.
|
|
261
|
-
* @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
|
|
262
|
-
* will be inserted as a child.
|
|
263
|
-
* @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
|
|
264
|
-
*/
|
|
265
|
-
BinaryTree.prototype.putTo = function (newNode, parent) {
|
|
266
|
-
var _a, _b;
|
|
267
|
-
if (parent) {
|
|
268
|
-
if (parent.left === undefined) {
|
|
269
|
-
if (newNode) {
|
|
270
|
-
newNode.parent = parent;
|
|
271
|
-
newNode.familyPosition = FamilyPosition.left;
|
|
272
|
-
}
|
|
273
|
-
parent.left = newNode;
|
|
274
|
-
if (newNode !== null) {
|
|
275
|
-
this.size++;
|
|
276
|
-
this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 0;
|
|
277
|
-
}
|
|
278
|
-
return parent.left;
|
|
279
|
-
}
|
|
280
|
-
else if (parent.right === undefined) {
|
|
281
|
-
if (newNode) {
|
|
282
|
-
newNode.parent = parent;
|
|
283
|
-
newNode.familyPosition = FamilyPosition.right;
|
|
284
|
-
}
|
|
285
|
-
parent.right = newNode;
|
|
286
|
-
if (newNode !== null) {
|
|
287
|
-
this.size++;
|
|
288
|
-
this.count += (_b = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _b !== void 0 ? _b : 0;
|
|
289
|
-
}
|
|
290
|
-
return parent.right;
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
else {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
/**
|
|
301
|
-
* The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
268
|
+
* The `add` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
302
269
|
* already exists.
|
|
303
270
|
* @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
|
|
304
271
|
* identify each node in the binary tree.
|
|
305
272
|
* @param {T} val - The value to be inserted into the binary tree.
|
|
306
273
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
307
274
|
* value should be inserted into the binary tree. If not provided, it defaults to 1.
|
|
308
|
-
* @returns The function `
|
|
275
|
+
* @returns The function `add` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
|
|
309
276
|
* is inserted, or `undefined` if the insertion fails.
|
|
310
277
|
*/
|
|
311
|
-
BinaryTree.prototype.
|
|
278
|
+
BinaryTree.prototype.add = function (id, val, count) {
|
|
312
279
|
var _this = this;
|
|
313
280
|
count = count !== null && count !== void 0 ? count : 1;
|
|
314
281
|
var _bfs = function (root, newNode) {
|
|
@@ -316,7 +283,7 @@ var BinaryTree = /** @class */ (function () {
|
|
|
316
283
|
while (queue.length > 0) {
|
|
317
284
|
var cur = queue.shift();
|
|
318
285
|
if (cur) {
|
|
319
|
-
var inserted_1 = _this.
|
|
286
|
+
var inserted_1 = _this.addTo(newNode, cur);
|
|
320
287
|
if (inserted_1 !== undefined)
|
|
321
288
|
return inserted_1;
|
|
322
289
|
if (cur.left)
|
|
@@ -356,13 +323,56 @@ var BinaryTree = /** @class */ (function () {
|
|
|
356
323
|
return inserted;
|
|
357
324
|
};
|
|
358
325
|
/**
|
|
359
|
-
* The
|
|
326
|
+
* The function inserts a new node into a binary tree as the left or right child of a given parent node.
|
|
327
|
+
* @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
|
|
328
|
+
* `null`. It represents the node that needs to be inserted into the binary tree.
|
|
329
|
+
* @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
|
|
330
|
+
* will be inserted as a child.
|
|
331
|
+
* @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
|
|
332
|
+
*/
|
|
333
|
+
BinaryTree.prototype.addTo = function (newNode, parent) {
|
|
334
|
+
var _a, _b;
|
|
335
|
+
if (parent) {
|
|
336
|
+
if (parent.left === undefined) {
|
|
337
|
+
if (newNode) {
|
|
338
|
+
newNode.parent = parent;
|
|
339
|
+
newNode.familyPosition = FamilyPosition.left;
|
|
340
|
+
}
|
|
341
|
+
parent.left = newNode;
|
|
342
|
+
if (newNode !== null) {
|
|
343
|
+
this.size++;
|
|
344
|
+
this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 0;
|
|
345
|
+
}
|
|
346
|
+
return parent.left;
|
|
347
|
+
}
|
|
348
|
+
else if (parent.right === undefined) {
|
|
349
|
+
if (newNode) {
|
|
350
|
+
newNode.parent = parent;
|
|
351
|
+
newNode.familyPosition = FamilyPosition.right;
|
|
352
|
+
}
|
|
353
|
+
parent.right = newNode;
|
|
354
|
+
if (newNode !== null) {
|
|
355
|
+
this.size++;
|
|
356
|
+
this.count += (_b = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _b !== void 0 ? _b : 0;
|
|
357
|
+
}
|
|
358
|
+
return parent.right;
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
/**
|
|
369
|
+
* The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
|
|
360
370
|
* null/undefined values.
|
|
361
371
|
* @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
|
|
362
372
|
* array of `BinaryTreeNode<T>` objects.
|
|
363
|
-
* @returns The function `
|
|
373
|
+
* @returns The function `addMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
|
|
364
374
|
*/
|
|
365
|
-
BinaryTree.prototype.
|
|
375
|
+
BinaryTree.prototype.addMany = function (data) {
|
|
366
376
|
var e_1, _a, e_2, _b;
|
|
367
377
|
var _c;
|
|
368
378
|
var inserted = [];
|
|
@@ -387,33 +397,33 @@ var BinaryTree = /** @class */ (function () {
|
|
|
387
397
|
var item = data_2_1.value;
|
|
388
398
|
var count = this._isDuplicatedVal ? 1 : map.get(item);
|
|
389
399
|
if (item instanceof BinaryTreeNode) {
|
|
390
|
-
inserted.push(this.
|
|
400
|
+
inserted.push(this.add(item.id, item.val, item.count));
|
|
391
401
|
}
|
|
392
402
|
else if (typeof item === 'number' && !this._autoIncrementId) {
|
|
393
403
|
if (!this._isDuplicatedVal) {
|
|
394
404
|
if (map.get(item) !== undefined) {
|
|
395
|
-
inserted.push(this.
|
|
405
|
+
inserted.push(this.add(item, item, count));
|
|
396
406
|
map.delete(item);
|
|
397
407
|
}
|
|
398
408
|
}
|
|
399
409
|
else {
|
|
400
|
-
inserted.push(this.
|
|
410
|
+
inserted.push(this.add(item, item, 1));
|
|
401
411
|
}
|
|
402
412
|
}
|
|
403
413
|
else {
|
|
404
414
|
if (item !== null) {
|
|
405
415
|
if (!this._isDuplicatedVal) {
|
|
406
416
|
if (map.get(item) !== undefined) {
|
|
407
|
-
inserted.push(this.
|
|
417
|
+
inserted.push(this.add(++this._maxId, item, count));
|
|
408
418
|
map.delete(item);
|
|
409
419
|
}
|
|
410
420
|
}
|
|
411
421
|
else {
|
|
412
|
-
inserted.push(this.
|
|
422
|
+
inserted.push(this.add(++this._maxId, item, 1));
|
|
413
423
|
}
|
|
414
424
|
}
|
|
415
425
|
else {
|
|
416
|
-
inserted.push(this.
|
|
426
|
+
inserted.push(this.add(Number.MAX_SAFE_INTEGER, item, 0));
|
|
417
427
|
}
|
|
418
428
|
}
|
|
419
429
|
}
|
|
@@ -436,7 +446,7 @@ var BinaryTree = /** @class */ (function () {
|
|
|
436
446
|
*/
|
|
437
447
|
BinaryTree.prototype.fill = function (data) {
|
|
438
448
|
this.clear();
|
|
439
|
-
return data.length === this.
|
|
449
|
+
return data.length === this.addMany(data).length;
|
|
440
450
|
};
|
|
441
451
|
/**
|
|
442
452
|
* The function removes a node from a binary tree and returns information about the deleted node.
|
|
@@ -718,7 +728,7 @@ var BinaryTree = /** @class */ (function () {
|
|
|
718
728
|
}
|
|
719
729
|
else {
|
|
720
730
|
// Indirect implementation of iteration using tail recursion optimization
|
|
721
|
-
var _traverse_3 = (0,
|
|
731
|
+
var _traverse_3 = (0, utils_1.trampoline)(function (cur) {
|
|
722
732
|
if (!cur.left)
|
|
723
733
|
return cur;
|
|
724
734
|
return _traverse_3.cont(cur.left);
|
|
@@ -748,7 +758,7 @@ var BinaryTree = /** @class */ (function () {
|
|
|
748
758
|
}
|
|
749
759
|
else {
|
|
750
760
|
// Indirect implementation of iteration using tail recursion optimization
|
|
751
|
-
var _traverse_5 = (0,
|
|
761
|
+
var _traverse_5 = (0, utils_1.trampoline)(function (cur) {
|
|
752
762
|
if (!cur.right)
|
|
753
763
|
return cur;
|
|
754
764
|
return _traverse_5.cont(cur.right);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult } from '../types';
|
|
6
9
|
import { BinaryTree, BinaryTreeNode, LoopType } from './binary-tree';
|
|
@@ -13,13 +16,17 @@ export declare class BSTNode<T> extends BinaryTreeNode<T> {
|
|
|
13
16
|
clone(): BSTNode<T>;
|
|
14
17
|
}
|
|
15
18
|
export declare class BST<T> extends BinaryTree<T> {
|
|
19
|
+
/**
|
|
20
|
+
* The constructor function accepts an optional options object and sets the comparator property if provided.
|
|
21
|
+
* @param [options] - An optional object that can contain the following properties:
|
|
22
|
+
*/
|
|
16
23
|
constructor(options?: {
|
|
17
24
|
comparator?: BSTComparator;
|
|
18
25
|
loopType?: LoopType;
|
|
19
26
|
});
|
|
20
27
|
createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
21
28
|
/**
|
|
22
|
-
* The `
|
|
29
|
+
* The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
23
30
|
* the ID matches, and returns the inserted node.
|
|
24
31
|
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
25
32
|
* determine the position of the node in the binary search tree.
|
|
@@ -28,17 +35,97 @@ export declare class BST<T> extends BinaryTree<T> {
|
|
|
28
35
|
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
29
36
|
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
30
37
|
* inserted once.
|
|
31
|
-
* @returns The method `
|
|
38
|
+
* @returns The method `add` returns a `BSTNode<T>` object or `null`.
|
|
39
|
+
*/
|
|
40
|
+
add(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
41
|
+
/**
|
|
42
|
+
* The `get` function returns the first node in a binary search tree that matches the given property value or name.
|
|
43
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
44
|
+
* generic type `T`. It represents the value of the property that you want to search for in the binary search tree.
|
|
45
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
46
|
+
* specifies the property name to use for searching the binary search tree nodes. If not provided, it defaults to
|
|
47
|
+
* `'id'`.
|
|
48
|
+
* @returns The method is returning a BSTNode<T> object or null.
|
|
32
49
|
*/
|
|
33
|
-
put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
34
50
|
get(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): BSTNode<T> | null;
|
|
51
|
+
/**
|
|
52
|
+
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
53
|
+
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
54
|
+
* @returns The function `lastKey()` returns the ID of the rightmost node in a binary tree. If the comparison between
|
|
55
|
+
* the first two elements in the tree is less than, it returns the ID of the rightmost node. If the comparison is
|
|
56
|
+
* greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
|
|
57
|
+
* there are no nodes in
|
|
58
|
+
*/
|
|
35
59
|
lastKey(): number;
|
|
60
|
+
/**
|
|
61
|
+
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
62
|
+
* the deleted node and any nodes that need to be balanced.
|
|
63
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
64
|
+
* from the binary search tree.
|
|
65
|
+
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
66
|
+
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
67
|
+
* set to false or not provided, the count of the node will be taken into account and the
|
|
68
|
+
* @returns an array of `BSTDeletedResult<T>` objects.
|
|
69
|
+
*/
|
|
36
70
|
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[];
|
|
71
|
+
/**
|
|
72
|
+
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
73
|
+
* option to specify the property name and whether to return only one node.
|
|
74
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
75
|
+
* generic type `T`. It represents the property value that you want to search for in the binary search tree.
|
|
76
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
77
|
+
* specifies the property of the nodes to compare with the `nodeProperty` parameter. If not provided, it defaults to
|
|
78
|
+
* `'id'`.
|
|
79
|
+
* @param {boolean} [onlyOne] - A boolean value indicating whether to return only one node that matches the given
|
|
80
|
+
* nodeProperty. If set to true, the function will stop traversing the tree and return the first matching node. If set
|
|
81
|
+
* to false or not provided, the function will return all nodes that match the given nodeProperty.
|
|
82
|
+
* @returns an array of BSTNode<T> objects.
|
|
83
|
+
*/
|
|
37
84
|
getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): BSTNode<T>[];
|
|
85
|
+
/**
|
|
86
|
+
* The `lesserSum` function calculates the sum of a specified property in all nodes with an ID less than a given ID in
|
|
87
|
+
* a binary search tree.
|
|
88
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node for which you want to
|
|
89
|
+
* calculate the lesser sum.
|
|
90
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
91
|
+
* specifies the property of the binary tree node to use for calculating the sum. If not provided, it defaults to 'id'.
|
|
92
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
93
|
+
* binary search tree that have a property value lesser than the given `id`.
|
|
94
|
+
*/
|
|
38
95
|
lesserSum(id: BinaryTreeNodeId, propertyName?: BinaryTreeNodePropertyName): number;
|
|
96
|
+
/**
|
|
97
|
+
* The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
|
|
98
|
+
* that have a greater value than a given node.
|
|
99
|
+
* @param node - The `node` parameter is of type `BSTNode<T>`, which represents a node in a binary search tree. It
|
|
100
|
+
* contains properties such as `id` and `count`.
|
|
101
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
102
|
+
* each node should be increased.
|
|
103
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
104
|
+
* property of the BSTNode to be modified. It can be either 'id' or 'count'. If propertyName is not provided, it
|
|
105
|
+
* defaults to 'id'.
|
|
106
|
+
* @returns a boolean value.
|
|
107
|
+
*/
|
|
39
108
|
allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
|
|
111
|
+
* recursive or iterative approach.
|
|
112
|
+
* @returns The `balance()` function returns a boolean value.
|
|
113
|
+
*/
|
|
40
114
|
balance(): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* The function `isAVLBalanced` checks if a binary search tree is balanced according to the AVL tree property.
|
|
117
|
+
* @returns The function `isAVLBalanced()` returns a boolean value. It returns `true` if the binary search tree (BST)
|
|
118
|
+
* is balanced according to the AVL tree property, and `false` otherwise.
|
|
119
|
+
*/
|
|
41
120
|
isAVLBalanced(): boolean;
|
|
42
121
|
protected _comparator: BSTComparator;
|
|
122
|
+
/**
|
|
123
|
+
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
124
|
+
* greater than, less than, or equal to the second ID.
|
|
125
|
+
* @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
|
|
126
|
+
* @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
|
|
127
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
128
|
+
* than), or CP.eq (equal).
|
|
129
|
+
*/
|
|
43
130
|
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
|
|
44
131
|
}
|