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
|
@@ -11,7 +11,7 @@ import { BTNCallback } from '../../types';
|
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
14
|
-
constructor(key: BTNKey,
|
|
14
|
+
constructor(key: BTNKey, value?: V);
|
|
15
15
|
}
|
|
16
16
|
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
17
17
|
/**
|
|
@@ -25,22 +25,22 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
25
25
|
* The function creates a new AVL tree node with the specified key and value.
|
|
26
26
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
27
27
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
28
|
-
* @param [
|
|
29
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
28
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
29
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
30
30
|
* node type `N`.
|
|
31
31
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
32
32
|
*/
|
|
33
|
-
createNode(key: BTNKey,
|
|
33
|
+
createNode(key: BTNKey, value?: V): N;
|
|
34
34
|
/**
|
|
35
35
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
36
36
|
* a new node.
|
|
37
37
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
38
38
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
39
|
-
* @param [
|
|
39
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
40
40
|
* are adding to the binary search tree.
|
|
41
41
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
add(keyOrNode: BTNKey | N | null,
|
|
43
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
44
44
|
/**
|
|
45
45
|
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
46
46
|
* node if necessary.
|
|
@@ -10,8 +10,8 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
|
-
constructor(key,
|
|
14
|
-
super(key,
|
|
13
|
+
constructor(key, value) {
|
|
14
|
+
super(key, value);
|
|
15
15
|
this.height = 0;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -30,26 +30,25 @@ class AVLTree extends bst_1.BST {
|
|
|
30
30
|
* The function creates a new AVL tree node with the specified key and value.
|
|
31
31
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
32
32
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
33
|
-
* @param [
|
|
34
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
33
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
34
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
35
35
|
* node type `N`.
|
|
36
36
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
37
37
|
*/
|
|
38
|
-
createNode(key,
|
|
39
|
-
return new AVLTreeNode(key,
|
|
38
|
+
createNode(key, value) {
|
|
39
|
+
return new AVLTreeNode(key, value);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
43
43
|
* a new node.
|
|
44
44
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
45
45
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
46
|
-
* @param [
|
|
46
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
47
47
|
* are adding to the binary search tree.
|
|
48
48
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
49
49
|
*/
|
|
50
|
-
add(keyOrNode,
|
|
51
|
-
|
|
52
|
-
const inserted = super.add(keyOrNode, val);
|
|
50
|
+
add(keyOrNode, value) {
|
|
51
|
+
const inserted = super.add(keyOrNode, value);
|
|
53
52
|
if (inserted)
|
|
54
53
|
this._balancePath(inserted);
|
|
55
54
|
return inserted;
|
|
@@ -86,15 +85,15 @@ class AVLTree extends bst_1.BST {
|
|
|
86
85
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
87
86
|
*/
|
|
88
87
|
_swap(srcNode, destNode) {
|
|
89
|
-
const { key,
|
|
90
|
-
const tempNode = this.createNode(key,
|
|
88
|
+
const { key, value, height } = destNode;
|
|
89
|
+
const tempNode = this.createNode(key, value);
|
|
91
90
|
if (tempNode) {
|
|
92
91
|
tempNode.height = height;
|
|
93
92
|
destNode.key = srcNode.key;
|
|
94
|
-
destNode.
|
|
93
|
+
destNode.value = srcNode.value;
|
|
95
94
|
destNode.height = srcNode.height;
|
|
96
95
|
srcNode.key = tempNode.key;
|
|
97
|
-
srcNode.
|
|
96
|
+
srcNode.value = tempNode.value;
|
|
98
97
|
srcNode.height = tempNode.height;
|
|
99
98
|
}
|
|
100
99
|
return destNode;
|
|
@@ -13,13 +13,10 @@ export declare class BinaryIndexedTree {
|
|
|
13
13
|
});
|
|
14
14
|
protected _freqMap: Record<number, number>;
|
|
15
15
|
get freqMap(): Record<number, number>;
|
|
16
|
-
set freqMap(value: Record<number, number>);
|
|
17
16
|
protected _msb: number;
|
|
18
17
|
get msb(): number;
|
|
19
|
-
set msb(value: number);
|
|
20
18
|
protected _negativeCount: number;
|
|
21
19
|
get negativeCount(): number;
|
|
22
|
-
set negativeCount(value: number);
|
|
23
20
|
get freq(): number;
|
|
24
21
|
get max(): number;
|
|
25
22
|
/**
|
|
@@ -26,21 +26,12 @@ class BinaryIndexedTree {
|
|
|
26
26
|
get freqMap() {
|
|
27
27
|
return this._freqMap;
|
|
28
28
|
}
|
|
29
|
-
set freqMap(value) {
|
|
30
|
-
this._freqMap = value;
|
|
31
|
-
}
|
|
32
29
|
get msb() {
|
|
33
30
|
return this._msb;
|
|
34
31
|
}
|
|
35
|
-
set msb(value) {
|
|
36
|
-
this._msb = value;
|
|
37
|
-
}
|
|
38
32
|
get negativeCount() {
|
|
39
33
|
return this._negativeCount;
|
|
40
34
|
}
|
|
41
|
-
set negativeCount(value) {
|
|
42
|
-
this._negativeCount = value;
|
|
43
|
-
}
|
|
44
35
|
get freq() {
|
|
45
36
|
return this._freq;
|
|
46
37
|
}
|
|
@@ -198,10 +189,10 @@ class BinaryIndexedTree {
|
|
|
198
189
|
*/
|
|
199
190
|
_updateNegativeCount(freqCur, freqNew) {
|
|
200
191
|
if (freqCur < 0 && freqNew >= 0) {
|
|
201
|
-
this.
|
|
192
|
+
this._negativeCount--;
|
|
202
193
|
}
|
|
203
194
|
else if (freqCur >= 0 && freqNew < 0) {
|
|
204
|
-
this.
|
|
195
|
+
this._negativeCount++;
|
|
205
196
|
}
|
|
206
197
|
}
|
|
207
198
|
/**
|
|
@@ -21,7 +21,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
21
21
|
/**
|
|
22
22
|
* The value stored in the node.
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
value: V | undefined;
|
|
25
25
|
/**
|
|
26
26
|
* The parent node of the current node.
|
|
27
27
|
*/
|
|
@@ -29,10 +29,10 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
29
29
|
/**
|
|
30
30
|
* Creates a new instance of BinaryTreeNode.
|
|
31
31
|
* @param {BTNKey} key - The key associated with the node.
|
|
32
|
-
* @param {V}
|
|
32
|
+
* @param {V} value - The value stored in the node.
|
|
33
33
|
*/
|
|
34
|
-
constructor(key: BTNKey,
|
|
35
|
-
|
|
34
|
+
constructor(key: BTNKey, value?: V);
|
|
35
|
+
protected _left: N | null | undefined;
|
|
36
36
|
/**
|
|
37
37
|
* Get the left child node.
|
|
38
38
|
*/
|
|
@@ -42,7 +42,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
42
42
|
* @param {N | null | undefined} v - The left child node.
|
|
43
43
|
*/
|
|
44
44
|
set left(v: N | null | undefined);
|
|
45
|
-
|
|
45
|
+
protected _right: N | null | undefined;
|
|
46
46
|
/**
|
|
47
47
|
* Get the right child node.
|
|
48
48
|
*/
|
|
@@ -63,27 +63,18 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
63
63
|
* @template N - The type of the binary tree's nodes.
|
|
64
64
|
*/
|
|
65
65
|
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
|
|
66
|
+
iterationType: IterationType;
|
|
66
67
|
/**
|
|
67
68
|
* Creates a new instance of BinaryTree.
|
|
68
69
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
69
70
|
*/
|
|
70
71
|
constructor(options?: BinaryTreeOptions);
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Get the iteration type used in the binary tree.
|
|
74
|
-
*/
|
|
75
|
-
get iterationType(): IterationType;
|
|
76
|
-
/**
|
|
77
|
-
* Set the iteration type for the binary tree.
|
|
78
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
79
|
-
*/
|
|
80
|
-
set iterationType(v: IterationType);
|
|
81
|
-
private _root;
|
|
72
|
+
protected _root: N | null;
|
|
82
73
|
/**
|
|
83
74
|
* Get the root node of the binary tree.
|
|
84
75
|
*/
|
|
85
76
|
get root(): N | null;
|
|
86
|
-
|
|
77
|
+
protected _size: number;
|
|
87
78
|
/**
|
|
88
79
|
* Get the number of nodes in the binary tree.
|
|
89
80
|
*/
|
|
@@ -91,10 +82,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
91
82
|
/**
|
|
92
83
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
93
84
|
* @param {BTNKey} key - The key for the new node.
|
|
94
|
-
* @param {V}
|
|
85
|
+
* @param {V} value - The value for the new node.
|
|
95
86
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
96
87
|
*/
|
|
97
|
-
createNode(key: BTNKey,
|
|
88
|
+
createNode(key: BTNKey, value?: V): N;
|
|
98
89
|
/**
|
|
99
90
|
* Clear the binary tree, removing all nodes.
|
|
100
91
|
*/
|
|
@@ -107,10 +98,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
107
98
|
/**
|
|
108
99
|
* Add a node with the given key and value to the binary tree.
|
|
109
100
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
110
|
-
* @param {V}
|
|
101
|
+
* @param {V} value - The value for the new node (optional).
|
|
111
102
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
112
103
|
*/
|
|
113
|
-
add(keyOrNode: BTNKey | N | null,
|
|
104
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
114
105
|
/**
|
|
115
106
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
116
107
|
* values, and adds them to the binary tree.
|
|
@@ -361,10 +352,4 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
361
352
|
* type `N` or `null`.
|
|
362
353
|
*/
|
|
363
354
|
protected _setRoot(v: N | null): void;
|
|
364
|
-
/**
|
|
365
|
-
* The function sets the value of the protected property "_size" to the given number.
|
|
366
|
-
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
367
|
-
* set.
|
|
368
|
-
*/
|
|
369
|
-
protected _setSize(v: number): void;
|
|
370
355
|
}
|
|
@@ -20,11 +20,11 @@ class BinaryTreeNode {
|
|
|
20
20
|
/**
|
|
21
21
|
* Creates a new instance of BinaryTreeNode.
|
|
22
22
|
* @param {BTNKey} key - The key associated with the node.
|
|
23
|
-
* @param {V}
|
|
23
|
+
* @param {V} value - The value stored in the node.
|
|
24
24
|
*/
|
|
25
|
-
constructor(key,
|
|
25
|
+
constructor(key, value) {
|
|
26
26
|
this.key = key;
|
|
27
|
-
this.
|
|
27
|
+
this.value = value;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Get the left child node.
|
|
@@ -87,27 +87,14 @@ class BinaryTree {
|
|
|
87
87
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
88
88
|
*/
|
|
89
89
|
constructor(options) {
|
|
90
|
-
this.
|
|
90
|
+
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
91
91
|
this._root = null;
|
|
92
92
|
this._size = 0;
|
|
93
93
|
if (options !== undefined) {
|
|
94
94
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
95
|
-
this.
|
|
95
|
+
this.iterationType = iterationType;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Get the iteration type used in the binary tree.
|
|
100
|
-
*/
|
|
101
|
-
get iterationType() {
|
|
102
|
-
return this._iterationType;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Set the iteration type for the binary tree.
|
|
106
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
107
|
-
*/
|
|
108
|
-
set iterationType(v) {
|
|
109
|
-
this._iterationType = v;
|
|
110
|
-
}
|
|
111
98
|
/**
|
|
112
99
|
* Get the root node of the binary tree.
|
|
113
100
|
*/
|
|
@@ -123,17 +110,17 @@ class BinaryTree {
|
|
|
123
110
|
/**
|
|
124
111
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
125
112
|
* @param {BTNKey} key - The key for the new node.
|
|
126
|
-
* @param {V}
|
|
113
|
+
* @param {V} value - The value for the new node.
|
|
127
114
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
128
115
|
*/
|
|
129
|
-
createNode(key,
|
|
130
|
-
return new BinaryTreeNode(key,
|
|
116
|
+
createNode(key, value) {
|
|
117
|
+
return new BinaryTreeNode(key, value);
|
|
131
118
|
}
|
|
132
119
|
/**
|
|
133
120
|
* Clear the binary tree, removing all nodes.
|
|
134
121
|
*/
|
|
135
122
|
clear() {
|
|
136
|
-
this.
|
|
123
|
+
this._setRoot(null);
|
|
137
124
|
this._size = 0;
|
|
138
125
|
}
|
|
139
126
|
/**
|
|
@@ -146,10 +133,10 @@ class BinaryTree {
|
|
|
146
133
|
/**
|
|
147
134
|
* Add a node with the given key and value to the binary tree.
|
|
148
135
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
149
|
-
* @param {V}
|
|
136
|
+
* @param {V} value - The value for the new node (optional).
|
|
150
137
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
151
138
|
*/
|
|
152
|
-
add(keyOrNode,
|
|
139
|
+
add(keyOrNode, value) {
|
|
153
140
|
const _bfs = (root, newNode) => {
|
|
154
141
|
const queue = new queue_1.Queue([root]);
|
|
155
142
|
while (queue.size > 0) {
|
|
@@ -175,7 +162,7 @@ class BinaryTree {
|
|
|
175
162
|
needInsert = null;
|
|
176
163
|
}
|
|
177
164
|
else if (typeof keyOrNode === 'number') {
|
|
178
|
-
needInsert = this.createNode(keyOrNode,
|
|
165
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
179
166
|
}
|
|
180
167
|
else if (keyOrNode instanceof BinaryTreeNode) {
|
|
181
168
|
needInsert = keyOrNode;
|
|
@@ -187,7 +174,7 @@ class BinaryTree {
|
|
|
187
174
|
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
188
175
|
if (this.root) {
|
|
189
176
|
if (existNode) {
|
|
190
|
-
existNode.
|
|
177
|
+
existNode.value = value;
|
|
191
178
|
inserted = existNode;
|
|
192
179
|
}
|
|
193
180
|
else {
|
|
@@ -197,10 +184,10 @@ class BinaryTree {
|
|
|
197
184
|
else {
|
|
198
185
|
this._setRoot(needInsert);
|
|
199
186
|
if (needInsert !== null) {
|
|
200
|
-
this.
|
|
187
|
+
this._size = 1;
|
|
201
188
|
}
|
|
202
189
|
else {
|
|
203
|
-
this.
|
|
190
|
+
this._size = 0;
|
|
204
191
|
}
|
|
205
192
|
inserted = this.root;
|
|
206
193
|
}
|
|
@@ -220,13 +207,13 @@ class BinaryTree {
|
|
|
220
207
|
// TODO not sure addMany not be run multi times
|
|
221
208
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
222
209
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
223
|
-
return this.add(keyOrNode.key, keyOrNode.
|
|
210
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
224
211
|
}
|
|
225
212
|
if (keyOrNode === null) {
|
|
226
213
|
return this.add(null);
|
|
227
214
|
}
|
|
228
|
-
const
|
|
229
|
-
return this.add(keyOrNode,
|
|
215
|
+
const value = values === null || values === void 0 ? void 0 : values[i];
|
|
216
|
+
return this.add(keyOrNode, value);
|
|
230
217
|
});
|
|
231
218
|
}
|
|
232
219
|
/**
|
|
@@ -297,7 +284,7 @@ class BinaryTree {
|
|
|
297
284
|
}
|
|
298
285
|
}
|
|
299
286
|
}
|
|
300
|
-
this.
|
|
287
|
+
this._size = this.size - 1;
|
|
301
288
|
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
302
289
|
return bstDeletedResult;
|
|
303
290
|
}
|
|
@@ -1065,13 +1052,13 @@ class BinaryTree {
|
|
|
1065
1052
|
* @returns {N} - The destination node after the swap.
|
|
1066
1053
|
*/
|
|
1067
1054
|
_swap(srcNode, destNode) {
|
|
1068
|
-
const { key,
|
|
1069
|
-
const tempNode = this.createNode(key,
|
|
1055
|
+
const { key, value } = destNode;
|
|
1056
|
+
const tempNode = this.createNode(key, value);
|
|
1070
1057
|
if (tempNode) {
|
|
1071
1058
|
destNode.key = srcNode.key;
|
|
1072
|
-
destNode.
|
|
1059
|
+
destNode.value = srcNode.value;
|
|
1073
1060
|
srcNode.key = tempNode.key;
|
|
1074
|
-
srcNode.
|
|
1061
|
+
srcNode.value = tempNode.value;
|
|
1075
1062
|
}
|
|
1076
1063
|
return destNode;
|
|
1077
1064
|
}
|
|
@@ -1093,14 +1080,14 @@ class BinaryTree {
|
|
|
1093
1080
|
if (parent.left === undefined) {
|
|
1094
1081
|
parent.left = newNode;
|
|
1095
1082
|
if (newNode) {
|
|
1096
|
-
this.
|
|
1083
|
+
this._size = this.size + 1;
|
|
1097
1084
|
}
|
|
1098
1085
|
return parent.left;
|
|
1099
1086
|
}
|
|
1100
1087
|
else if (parent.right === undefined) {
|
|
1101
1088
|
parent.right = newNode;
|
|
1102
1089
|
if (newNode) {
|
|
1103
|
-
this.
|
|
1090
|
+
this._size = this.size + 1;
|
|
1104
1091
|
}
|
|
1105
1092
|
return parent.right;
|
|
1106
1093
|
}
|
|
@@ -1124,13 +1111,5 @@ class BinaryTree {
|
|
|
1124
1111
|
}
|
|
1125
1112
|
this._root = v;
|
|
1126
1113
|
}
|
|
1127
|
-
/**
|
|
1128
|
-
* The function sets the value of the protected property "_size" to the given number.
|
|
1129
|
-
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
1130
|
-
* set.
|
|
1131
|
-
*/
|
|
1132
|
-
_setSize(v) {
|
|
1133
|
-
this._size = v;
|
|
1134
|
-
}
|
|
1135
1114
|
}
|
|
1136
1115
|
exports.BinaryTree = BinaryTree;
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
9
|
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
|
-
constructor(key: BTNKey,
|
|
13
|
+
constructor(key: BTNKey, value?: V);
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
@@ -24,26 +24,26 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
24
24
|
* The function creates a new binary search tree node with the given key and value.
|
|
25
25
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
26
26
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
27
|
-
* @param [
|
|
27
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key: BTNKey,
|
|
31
|
+
createNode(key: BTNKey, value?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
35
35
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
36
36
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
37
|
-
* @param [
|
|
37
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
38
38
|
* binary search tree.
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode: BTNKey | N | null,
|
|
42
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[BTNKey | N, N['
|
|
46
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
@@ -5,8 +5,8 @@ const types_1 = require("../../types");
|
|
|
5
5
|
const binary_tree_1 = require("./binary-tree");
|
|
6
6
|
const queue_1 = require("../queue");
|
|
7
7
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
8
|
-
constructor(key,
|
|
9
|
-
super(key,
|
|
8
|
+
constructor(key, value) {
|
|
9
|
+
super(key, value);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.BSTNode = BSTNode;
|
|
@@ -31,24 +31,24 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
31
31
|
* The function creates a new binary search tree node with the given key and value.
|
|
32
32
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
33
33
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
34
|
-
* @param [
|
|
34
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
35
35
|
* represents the value associated with the node in a binary search tree.
|
|
36
36
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
37
37
|
*/
|
|
38
|
-
createNode(key,
|
|
39
|
-
return new BSTNode(key,
|
|
38
|
+
createNode(key, value) {
|
|
39
|
+
return new BSTNode(key, value);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
43
43
|
* into the tree.
|
|
44
44
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
45
45
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
46
|
-
* @param [
|
|
46
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
47
47
|
* binary search tree.
|
|
48
48
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
49
49
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
50
50
|
*/
|
|
51
|
-
add(keyOrNode,
|
|
51
|
+
add(keyOrNode, value) {
|
|
52
52
|
// TODO support node as a parameter
|
|
53
53
|
let inserted = null;
|
|
54
54
|
let newNode = null;
|
|
@@ -56,14 +56,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
56
56
|
newNode = keyOrNode;
|
|
57
57
|
}
|
|
58
58
|
else if (typeof keyOrNode === 'number') {
|
|
59
|
-
newNode = this.createNode(keyOrNode,
|
|
59
|
+
newNode = this.createNode(keyOrNode, value);
|
|
60
60
|
}
|
|
61
61
|
else if (keyOrNode === null) {
|
|
62
62
|
newNode = null;
|
|
63
63
|
}
|
|
64
64
|
if (this.root === null) {
|
|
65
65
|
this._setRoot(newNode);
|
|
66
|
-
this.
|
|
66
|
+
this._size = this.size + 1;
|
|
67
67
|
inserted = this.root;
|
|
68
68
|
}
|
|
69
69
|
else {
|
|
@@ -73,7 +73,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
73
73
|
if (cur !== null && newNode !== null) {
|
|
74
74
|
if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
|
|
75
75
|
if (newNode) {
|
|
76
|
-
cur.
|
|
76
|
+
cur.value = newNode.value;
|
|
77
77
|
}
|
|
78
78
|
//Duplicates are not accepted.
|
|
79
79
|
traversing = false;
|
|
@@ -87,7 +87,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
87
87
|
}
|
|
88
88
|
//Add to the left of the current node
|
|
89
89
|
cur.left = newNode;
|
|
90
|
-
this.
|
|
90
|
+
this._size = this.size + 1;
|
|
91
91
|
traversing = false;
|
|
92
92
|
inserted = cur.left;
|
|
93
93
|
}
|
|
@@ -105,7 +105,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
105
105
|
}
|
|
106
106
|
//Add to the right of the current node
|
|
107
107
|
cur.right = newNode;
|
|
108
|
-
this.
|
|
108
|
+
this._size = this.size + 1;
|
|
109
109
|
traversing = false;
|
|
110
110
|
inserted = cur.right;
|
|
111
111
|
}
|
|
@@ -126,7 +126,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[BTNKey | N, N['
|
|
129
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
131
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
@@ -170,7 +170,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
170
170
|
throw new Error('Invalid input keysOrNodes');
|
|
171
171
|
}
|
|
172
172
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
173
|
-
sortedData = sorted.map(([,
|
|
173
|
+
sortedData = sorted.map(([, value]) => value);
|
|
174
174
|
const recursive = (arr, data) => {
|
|
175
175
|
if (arr.length === 0)
|
|
176
176
|
return;
|
|
@@ -414,7 +414,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
414
414
|
return;
|
|
415
415
|
const m = l + Math.floor((r - l) / 2);
|
|
416
416
|
const midNode = sorted[m];
|
|
417
|
-
this.add(midNode.key, midNode.
|
|
417
|
+
this.add(midNode.key, midNode.value);
|
|
418
418
|
buildBalanceBST(l, m - 1);
|
|
419
419
|
buildBalanceBST(m + 1, r);
|
|
420
420
|
};
|
|
@@ -430,7 +430,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
430
430
|
if (l <= r) {
|
|
431
431
|
const m = l + Math.floor((r - l) / 2);
|
|
432
432
|
const midNode = sorted[m];
|
|
433
|
-
this.add(midNode.key, midNode.
|
|
433
|
+
this.add(midNode.key, midNode.value);
|
|
434
434
|
stack.push([m + 1, r]);
|
|
435
435
|
stack.push([l, m - 1]);
|
|
436
436
|
}
|
|
@@ -2,12 +2,10 @@ import { BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
|
2
2
|
import { IBinaryTree } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
|
-
constructor(key: BTNKey,
|
|
6
|
-
|
|
7
|
-
get color(): RBColor;
|
|
8
|
-
set color(value: RBColor);
|
|
5
|
+
constructor(key: BTNKey, value?: V);
|
|
6
|
+
color: RBColor;
|
|
9
7
|
}
|
|
10
8
|
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
9
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key: BTNKey,
|
|
10
|
+
createNode(key: BTNKey, value?: V): N;
|
|
13
11
|
}
|
|
@@ -4,15 +4,9 @@ exports.RBTree = exports.RBTreeNode = void 0;
|
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const bst_1 = require("./bst");
|
|
6
6
|
class RBTreeNode extends bst_1.BSTNode {
|
|
7
|
-
constructor(key,
|
|
8
|
-
super(key,
|
|
9
|
-
this.
|
|
10
|
-
}
|
|
11
|
-
get color() {
|
|
12
|
-
return this._color;
|
|
13
|
-
}
|
|
14
|
-
set color(value) {
|
|
15
|
-
this._color = value;
|
|
7
|
+
constructor(key, value) {
|
|
8
|
+
super(key, value);
|
|
9
|
+
this.color = types_1.RBColor.RED;
|
|
16
10
|
}
|
|
17
11
|
}
|
|
18
12
|
exports.RBTreeNode = RBTreeNode;
|
|
@@ -20,8 +14,8 @@ class RBTree extends bst_1.BST {
|
|
|
20
14
|
constructor(options) {
|
|
21
15
|
super(options);
|
|
22
16
|
}
|
|
23
|
-
createNode(key,
|
|
24
|
-
return new RBTreeNode(key,
|
|
17
|
+
createNode(key, value) {
|
|
18
|
+
return new RBTreeNode(key, value);
|
|
25
19
|
}
|
|
26
20
|
}
|
|
27
21
|
exports.RBTree = RBTree;
|