graph-typed 1.39.5 → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +15 -27
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/interfaces/graph.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -7,25 +7,13 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
private _val;
|
|
18
|
-
get val(): SegmentTreeNodeVal | null;
|
|
19
|
-
set val(v: SegmentTreeNodeVal | null);
|
|
20
|
-
private _sum;
|
|
21
|
-
get sum(): number;
|
|
22
|
-
set sum(v: number);
|
|
23
|
-
private _left;
|
|
24
|
-
get left(): SegmentTreeNode | null;
|
|
25
|
-
set left(v: SegmentTreeNode | null);
|
|
26
|
-
private _right;
|
|
27
|
-
get right(): SegmentTreeNode | null;
|
|
28
|
-
set right(v: SegmentTreeNode | null);
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
value: SegmentTreeNodeVal | null;
|
|
13
|
+
sum: number;
|
|
14
|
+
left: SegmentTreeNode | null;
|
|
15
|
+
right: SegmentTreeNode | null;
|
|
16
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null);
|
|
29
17
|
}
|
|
30
18
|
export declare class SegmentTree {
|
|
31
19
|
/**
|
|
@@ -38,13 +26,13 @@ export declare class SegmentTree {
|
|
|
38
26
|
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
39
27
|
*/
|
|
40
28
|
constructor(values: number[], start?: number, end?: number);
|
|
41
|
-
|
|
29
|
+
protected _values: number[];
|
|
42
30
|
get values(): number[];
|
|
43
|
-
|
|
31
|
+
protected _start: number;
|
|
44
32
|
get start(): number;
|
|
45
|
-
|
|
33
|
+
protected _end: number;
|
|
46
34
|
get end(): number;
|
|
47
|
-
|
|
35
|
+
protected _root: SegmentTreeNode | null;
|
|
48
36
|
get root(): SegmentTreeNode | null;
|
|
49
37
|
/**
|
|
50
38
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
@@ -62,12 +50,12 @@ export declare class SegmentTree {
|
|
|
62
50
|
* updated.
|
|
63
51
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
64
52
|
* the `SegmentTreeNode` at the specified `index`.
|
|
65
|
-
* @param {SegmentTreeNodeVal} [
|
|
53
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
66
54
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
67
|
-
* cur.
|
|
55
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
68
56
|
* @returns The function does not return anything.
|
|
69
57
|
*/
|
|
70
|
-
updateNode(index: number, sum: number,
|
|
58
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
|
|
71
59
|
/**
|
|
72
60
|
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
73
61
|
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
@@ -76,8 +64,4 @@ export declare class SegmentTree {
|
|
|
76
64
|
* @returns The function `querySumByRange` returns a number.
|
|
77
65
|
*/
|
|
78
66
|
querySumByRange(indexA: number, indexB: number): number;
|
|
79
|
-
protected _setValues(value: number[]): void;
|
|
80
|
-
protected _setStart(value: number): void;
|
|
81
|
-
protected _setEnd(value: number): void;
|
|
82
|
-
protected _setRoot(v: SegmentTreeNode | null): void;
|
|
83
67
|
}
|
|
@@ -9,53 +9,17 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
11
11
|
class SegmentTreeNode {
|
|
12
|
-
constructor(start, end, sum,
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
}
|
|
24
|
-
get start() {
|
|
25
|
-
return this._start;
|
|
26
|
-
}
|
|
27
|
-
set start(v) {
|
|
28
|
-
this._start = v;
|
|
29
|
-
}
|
|
30
|
-
get end() {
|
|
31
|
-
return this._end;
|
|
32
|
-
}
|
|
33
|
-
set end(v) {
|
|
34
|
-
this._end = v;
|
|
35
|
-
}
|
|
36
|
-
get val() {
|
|
37
|
-
return this._val;
|
|
38
|
-
}
|
|
39
|
-
set val(v) {
|
|
40
|
-
this._val = v;
|
|
41
|
-
}
|
|
42
|
-
get sum() {
|
|
43
|
-
return this._sum;
|
|
44
|
-
}
|
|
45
|
-
set sum(v) {
|
|
46
|
-
this._sum = v;
|
|
47
|
-
}
|
|
48
|
-
get left() {
|
|
49
|
-
return this._left;
|
|
50
|
-
}
|
|
51
|
-
set left(v) {
|
|
52
|
-
this._left = v;
|
|
53
|
-
}
|
|
54
|
-
get right() {
|
|
55
|
-
return this._right;
|
|
56
|
-
}
|
|
57
|
-
set right(v) {
|
|
58
|
-
this._right = v;
|
|
12
|
+
constructor(start, end, sum, value) {
|
|
13
|
+
this.start = 0;
|
|
14
|
+
this.end = 0;
|
|
15
|
+
this.value = null;
|
|
16
|
+
this.sum = 0;
|
|
17
|
+
this.left = null;
|
|
18
|
+
this.right = null;
|
|
19
|
+
this.start = start;
|
|
20
|
+
this.end = end;
|
|
21
|
+
this.sum = sum;
|
|
22
|
+
this.value = value || null;
|
|
59
23
|
}
|
|
60
24
|
}
|
|
61
25
|
exports.SegmentTreeNode = SegmentTreeNode;
|
|
@@ -126,39 +90,39 @@ class SegmentTree {
|
|
|
126
90
|
* updated.
|
|
127
91
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
128
92
|
* the `SegmentTreeNode` at the specified `index`.
|
|
129
|
-
* @param {SegmentTreeNodeVal} [
|
|
93
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
130
94
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
131
|
-
* cur.
|
|
95
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
132
96
|
* @returns The function does not return anything.
|
|
133
97
|
*/
|
|
134
|
-
updateNode(index, sum,
|
|
98
|
+
updateNode(index, sum, value) {
|
|
135
99
|
const root = this.root || null;
|
|
136
100
|
if (!root) {
|
|
137
101
|
return;
|
|
138
102
|
}
|
|
139
|
-
const dfs = (cur, index, sum,
|
|
103
|
+
const dfs = (cur, index, sum, value) => {
|
|
140
104
|
if (cur.start === cur.end && cur.start === index) {
|
|
141
105
|
cur.sum = sum;
|
|
142
|
-
if (
|
|
143
|
-
cur.
|
|
106
|
+
if (value !== undefined)
|
|
107
|
+
cur.value = value;
|
|
144
108
|
return;
|
|
145
109
|
}
|
|
146
110
|
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
147
111
|
if (index <= mid) {
|
|
148
112
|
if (cur.left) {
|
|
149
|
-
dfs(cur.left, index, sum,
|
|
113
|
+
dfs(cur.left, index, sum, value);
|
|
150
114
|
}
|
|
151
115
|
}
|
|
152
116
|
else {
|
|
153
117
|
if (cur.right) {
|
|
154
|
-
dfs(cur.right, index, sum,
|
|
118
|
+
dfs(cur.right, index, sum, value);
|
|
155
119
|
}
|
|
156
120
|
}
|
|
157
121
|
if (cur.left && cur.right) {
|
|
158
122
|
cur.sum = cur.left.sum + cur.right.sum;
|
|
159
123
|
}
|
|
160
124
|
};
|
|
161
|
-
dfs(root, index, sum,
|
|
125
|
+
dfs(root, index, sum, value);
|
|
162
126
|
}
|
|
163
127
|
/**
|
|
164
128
|
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
@@ -212,17 +176,5 @@ class SegmentTree {
|
|
|
212
176
|
};
|
|
213
177
|
return dfs(root, indexA, indexB);
|
|
214
178
|
}
|
|
215
|
-
_setValues(value) {
|
|
216
|
-
this._values = value;
|
|
217
|
-
}
|
|
218
|
-
_setStart(value) {
|
|
219
|
-
this._start = value;
|
|
220
|
-
}
|
|
221
|
-
_setEnd(value) {
|
|
222
|
-
this._end = value;
|
|
223
|
-
}
|
|
224
|
-
_setRoot(v) {
|
|
225
|
-
this._root = v;
|
|
226
|
-
}
|
|
227
179
|
}
|
|
228
180
|
exports.SegmentTree = SegmentTree;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BTNKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult,
|
|
9
|
+
import { BinaryTreeDeletedResult, BTNCallback, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -15,13 +15,13 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
|
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
16
|
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
20
20
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key: BTNKey,
|
|
24
|
+
constructor(key: BTNKey, value?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
@@ -40,26 +40,26 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
41
|
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
|
-
* @param {N}
|
|
43
|
+
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key: BTNKey,
|
|
48
|
+
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
52
52
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
53
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
54
|
* node to be added), or `null` (which represents a null node).
|
|
55
|
-
* @param [
|
|
55
|
+
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
56
56
|
* added to the binary tree.
|
|
57
57
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
58
58
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode: BTNKey | N | null,
|
|
62
|
+
add(keyOrNode: BTNKey | N | null, value?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
@@ -8,14 +8,14 @@ class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
9
|
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
10
10
|
* of the binary tree node.
|
|
11
|
-
* @param {V} [
|
|
11
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
12
|
* tree node. If no value is provided, it will be `undefined`.
|
|
13
13
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
14
14
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
15
15
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
16
16
|
*/
|
|
17
|
-
constructor(key,
|
|
18
|
-
super(key,
|
|
17
|
+
constructor(key, value, count = 1) {
|
|
18
|
+
super(key, value);
|
|
19
19
|
this.count = count;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -41,13 +41,13 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
41
41
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
42
42
|
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
43
43
|
* distinguish one node from another in the tree.
|
|
44
|
-
* @param {N}
|
|
44
|
+
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
45
45
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
46
46
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
47
47
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
48
48
|
*/
|
|
49
|
-
createNode(key,
|
|
50
|
-
return new TreeMultisetNode(key,
|
|
49
|
+
createNode(key, value, count) {
|
|
50
|
+
return new TreeMultisetNode(key, value, count);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
@@ -55,27 +55,27 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
55
55
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
56
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
57
57
|
* node to be added), or `null` (which represents a null node).
|
|
58
|
-
* @param [
|
|
58
|
+
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
59
59
|
* added to the binary tree.
|
|
60
60
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
61
61
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
62
62
|
* count is specified, the default count will be 1.
|
|
63
63
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
64
64
|
*/
|
|
65
|
-
add(keyOrNode,
|
|
65
|
+
add(keyOrNode, value, count = 1) {
|
|
66
66
|
let inserted = undefined, newNode;
|
|
67
67
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
68
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.
|
|
68
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
69
69
|
}
|
|
70
70
|
else if (keyOrNode === null) {
|
|
71
71
|
newNode = null;
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
newNode = this.createNode(keyOrNode,
|
|
74
|
+
newNode = this.createNode(keyOrNode, value, count);
|
|
75
75
|
}
|
|
76
76
|
if (!this.root) {
|
|
77
77
|
this._setRoot(newNode);
|
|
78
|
-
this.
|
|
78
|
+
this._size = this.size + 1;
|
|
79
79
|
newNode && this._setCount(this.count + newNode.count);
|
|
80
80
|
inserted = this.root;
|
|
81
81
|
}
|
|
@@ -86,7 +86,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
86
86
|
if (cur) {
|
|
87
87
|
if (newNode) {
|
|
88
88
|
if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
|
|
89
|
-
cur.
|
|
89
|
+
cur.value = newNode.value;
|
|
90
90
|
cur.count += newNode.count;
|
|
91
91
|
this._setCount(this.count + newNode.count);
|
|
92
92
|
traversing = false;
|
|
@@ -97,7 +97,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
97
97
|
if (cur.left === undefined) {
|
|
98
98
|
//Add to the left of the current node
|
|
99
99
|
cur.left = newNode;
|
|
100
|
-
this.
|
|
100
|
+
this._size = this.size + 1;
|
|
101
101
|
this._setCount(this.count + newNode.count);
|
|
102
102
|
traversing = false;
|
|
103
103
|
inserted = cur.left;
|
|
@@ -113,7 +113,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
113
113
|
if (cur.right === undefined) {
|
|
114
114
|
//Add to the right of the current node
|
|
115
115
|
cur.right = newNode;
|
|
116
|
-
this.
|
|
116
|
+
this._size = this.size + 1;
|
|
117
117
|
this._setCount(this.count + newNode.count);
|
|
118
118
|
traversing = false;
|
|
119
119
|
inserted = cur.right;
|
|
@@ -151,7 +151,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
151
151
|
if (parent.left === undefined) {
|
|
152
152
|
parent.left = newNode;
|
|
153
153
|
if (newNode !== null) {
|
|
154
|
-
this.
|
|
154
|
+
this._size = this.size + 1;
|
|
155
155
|
this._setCount(this.count + newNode.count);
|
|
156
156
|
}
|
|
157
157
|
return parent.left;
|
|
@@ -159,7 +159,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
159
159
|
else if (parent.right === undefined) {
|
|
160
160
|
parent.right = newNode;
|
|
161
161
|
if (newNode !== null) {
|
|
162
|
-
this.
|
|
162
|
+
this._size = (this.size + 1);
|
|
163
163
|
this._setCount(this.count + newNode.count);
|
|
164
164
|
}
|
|
165
165
|
return parent.right;
|
|
@@ -187,7 +187,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
187
187
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
188
188
|
const keyOrNode = keysOrNodes[i];
|
|
189
189
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
190
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.
|
|
190
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
191
191
|
continue;
|
|
192
192
|
}
|
|
193
193
|
if (keyOrNode === null) {
|
|
@@ -217,7 +217,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
217
217
|
return;
|
|
218
218
|
const m = l + Math.floor((r - l) / 2);
|
|
219
219
|
const midNode = sorted[m];
|
|
220
|
-
this.add(midNode.key, midNode.
|
|
220
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
221
221
|
buildBalanceBST(l, m - 1);
|
|
222
222
|
buildBalanceBST(m + 1, r);
|
|
223
223
|
};
|
|
@@ -233,7 +233,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
233
233
|
if (l <= r) {
|
|
234
234
|
const m = l + Math.floor((r - l) / 2);
|
|
235
235
|
const midNode = sorted[m];
|
|
236
|
-
this.add(midNode.key, midNode.
|
|
236
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
237
237
|
stack.push([m + 1, r]);
|
|
238
238
|
stack.push([l, m - 1]);
|
|
239
239
|
}
|
|
@@ -304,7 +304,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
-
this.
|
|
307
|
+
this._size = this.size - 1;
|
|
308
308
|
// TODO How to handle when the count of target node is lesser than current node's count
|
|
309
309
|
this._setCount(this.count - orgCurrent.count);
|
|
310
310
|
}
|
|
@@ -329,16 +329,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
329
329
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
330
330
|
*/
|
|
331
331
|
_swap(srcNode, destNode) {
|
|
332
|
-
const { key,
|
|
333
|
-
const tempNode = this.createNode(key,
|
|
332
|
+
const { key, value, count, height } = destNode;
|
|
333
|
+
const tempNode = this.createNode(key, value, count);
|
|
334
334
|
if (tempNode) {
|
|
335
335
|
tempNode.height = height;
|
|
336
336
|
destNode.key = srcNode.key;
|
|
337
|
-
destNode.
|
|
337
|
+
destNode.value = srcNode.value;
|
|
338
338
|
destNode.count = srcNode.count;
|
|
339
339
|
destNode.height = srcNode.height;
|
|
340
340
|
srcNode.key = tempNode.key;
|
|
341
|
-
srcNode.
|
|
341
|
+
srcNode.value = tempNode.value;
|
|
342
342
|
srcNode.count = tempNode.count;
|
|
343
343
|
srcNode.height = tempNode.height;
|
|
344
344
|
}
|
|
@@ -1,70 +1,52 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexKey } from '../../types';
|
|
2
2
|
import { IGraph } from '../../interfaces';
|
|
3
3
|
export declare abstract class AbstractVertex<V = any> {
|
|
4
|
+
key: VertexKey;
|
|
5
|
+
value: V | undefined;
|
|
4
6
|
/**
|
|
5
7
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
6
8
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
7
9
|
* used to uniquely identify the vertex object.
|
|
8
|
-
* @param {V} [
|
|
10
|
+
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
|
9
11
|
* vertex. If no value is provided, it will be set to undefined.
|
|
10
12
|
*/
|
|
11
|
-
protected constructor(key: VertexKey,
|
|
12
|
-
private _key;
|
|
13
|
-
get key(): VertexKey;
|
|
14
|
-
set key(v: VertexKey);
|
|
15
|
-
private _val;
|
|
16
|
-
get val(): V | undefined;
|
|
17
|
-
set val(value: V | undefined);
|
|
13
|
+
protected constructor(key: VertexKey, value?: V);
|
|
18
14
|
}
|
|
19
|
-
export declare abstract class AbstractEdge<
|
|
15
|
+
export declare abstract class AbstractEdge<E = any> {
|
|
16
|
+
value: E | undefined;
|
|
17
|
+
weight: number;
|
|
20
18
|
/**
|
|
21
19
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
22
20
|
* object.
|
|
23
21
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
24
22
|
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
25
23
|
* will be assigned.
|
|
26
|
-
* @param {VO} [
|
|
24
|
+
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
27
25
|
* meaning it can be omitted when creating an instance of the class.
|
|
28
26
|
*/
|
|
29
|
-
protected constructor(weight?: number,
|
|
30
|
-
private _val;
|
|
31
|
-
get val(): VO | undefined;
|
|
32
|
-
set val(value: VO | undefined);
|
|
33
|
-
private _weight;
|
|
34
|
-
get weight(): number;
|
|
35
|
-
set weight(v: number);
|
|
27
|
+
protected constructor(weight?: number, value?: E);
|
|
36
28
|
protected _hashCode: string;
|
|
37
29
|
get hashCode(): string;
|
|
38
|
-
/**
|
|
39
|
-
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
40
|
-
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* The function sets the value of the _hashCode property to the provided string.
|
|
44
|
-
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
45
|
-
* "_hashCode" property.
|
|
46
|
-
*/
|
|
47
|
-
protected _setHashCode(v: string): void;
|
|
48
30
|
}
|
|
49
31
|
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> {
|
|
50
|
-
|
|
32
|
+
protected _vertices: Map<VertexKey, VO>;
|
|
51
33
|
get vertices(): Map<VertexKey, VO>;
|
|
52
34
|
/**
|
|
53
35
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
54
36
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
55
37
|
* @param key
|
|
56
|
-
* @param
|
|
38
|
+
* @param value
|
|
57
39
|
*/
|
|
58
|
-
abstract createVertex(key: VertexKey,
|
|
40
|
+
abstract createVertex(key: VertexKey, value?: V): VO;
|
|
59
41
|
/**
|
|
60
42
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
61
43
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
62
44
|
* @param srcOrV1
|
|
63
45
|
* @param destOrV2
|
|
64
46
|
* @param weight
|
|
65
|
-
* @param
|
|
47
|
+
* @param value
|
|
66
48
|
*/
|
|
67
|
-
abstract createEdge(srcOrV1: VertexKey
|
|
49
|
+
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
68
50
|
abstract deleteEdge(edge: EO): EO | null;
|
|
69
51
|
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | null;
|
|
70
52
|
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
@@ -88,7 +70,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
88
70
|
*/
|
|
89
71
|
hasVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
90
72
|
addVertex(vertex: VO): boolean;
|
|
91
|
-
addVertex(key: VertexKey,
|
|
73
|
+
addVertex(key: VertexKey, value?: V): boolean;
|
|
92
74
|
/**
|
|
93
75
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
94
76
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
@@ -114,7 +96,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
114
96
|
*/
|
|
115
97
|
hasEdge(v1: VertexKey | VO, v2: VertexKey | VO): boolean;
|
|
116
98
|
addEdge(edge: EO): boolean;
|
|
117
|
-
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number,
|
|
99
|
+
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
|
118
100
|
/**
|
|
119
101
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
120
102
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
@@ -328,5 +310,4 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
328
310
|
protected _addVertexOnly(newVertex: VO): boolean;
|
|
329
311
|
protected _getVertex(vertexOrKey: VertexKey | VO): VO | null;
|
|
330
312
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
331
|
-
protected _setVertices(value: Map<VertexKey, VO>): void;
|
|
332
313
|
}
|