directed-graph-typed 1.48.0 → 1.49.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/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- 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 +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +335 -84
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback,
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, BTNodeKeyOrNode, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
12
12
|
color: RBTNColor;
|
|
13
|
-
constructor(key:
|
|
13
|
+
constructor(key: K, value?: V, color?: RBTNColor);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* 1. Each node is either red or black.
|
|
@@ -19,12 +19,12 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
19
19
|
* 4. Red nodes must have black children.
|
|
20
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
21
|
*/
|
|
22
|
-
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
22
|
+
export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
26
|
* initializes the tree with optional elements and options.
|
|
27
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
28
28
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
30
|
* elements to the
|
|
@@ -32,14 +32,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions
|
|
35
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
39
39
|
get size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
43
43
|
* identify and compare nodes in the Red-Black Tree.
|
|
44
44
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
45
45
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -49,7 +49,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
49
49
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
50
|
* value, and color.
|
|
51
51
|
*/
|
|
52
|
-
createNode(key:
|
|
52
|
+
createNode(key: K, value?: V, color?: RBTNColor): N;
|
|
53
53
|
/**
|
|
54
54
|
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
55
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -57,34 +57,47 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
57
57
|
* class.
|
|
58
58
|
* @returns a new instance of a RedBlackTree object.
|
|
59
59
|
*/
|
|
60
|
-
createTree(options?: RBTreeOptions): TREE;
|
|
60
|
+
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
62
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
63
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
63
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
64
64
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
65
65
|
* class.
|
|
66
66
|
*/
|
|
67
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
67
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
68
68
|
/**
|
|
69
|
-
* The function
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* that is not a valid exemplar.
|
|
74
|
-
* @returns a variable `node` which is of type `N | undefined`.
|
|
69
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
70
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
71
|
+
* data type.
|
|
72
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
75
73
|
*/
|
|
76
|
-
|
|
74
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
75
|
+
/**
|
|
76
|
+
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
78
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
79
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
80
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
81
|
+
* @returns a node of type N or undefined.
|
|
82
|
+
*/
|
|
83
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
77
84
|
/**
|
|
78
85
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
86
|
* Space Complexity: O(1)
|
|
80
87
|
*/
|
|
81
88
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* `
|
|
89
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
|
+
* Space Complexity: O(1)
|
|
91
|
+
*
|
|
92
|
+
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
93
|
+
* color changes to maintain the red-black tree properties.
|
|
94
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
95
|
+
* entry.
|
|
96
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
97
|
+
* being added to the binary search tree.
|
|
98
|
+
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
86
99
|
*/
|
|
87
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N
|
|
100
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
88
101
|
/**
|
|
89
102
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
103
|
* Space Complexity: O(1)
|
|
@@ -110,28 +123,15 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
110
123
|
* Space Complexity: O(1)
|
|
111
124
|
*/
|
|
112
125
|
isRealNode(node: N | undefined): node is N;
|
|
113
|
-
getNode<C extends BTNCallback<N,
|
|
126
|
+
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
114
127
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
115
128
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
116
129
|
/**
|
|
117
|
-
* Time Complexity: O(log n)
|
|
118
|
-
* Space Complexity: O(1)
|
|
119
|
-
*/
|
|
120
|
-
/**
|
|
121
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
122
|
-
* Space Complexity: O(1)
|
|
123
|
-
*
|
|
124
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
125
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
126
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
127
|
-
*/
|
|
128
|
-
getSuccessor(x: N): N | undefined;
|
|
129
|
-
/**
|
|
130
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
130
|
+
* Time Complexity: O(log n)
|
|
131
131
|
* Space Complexity: O(1)
|
|
132
132
|
*/
|
|
133
133
|
/**
|
|
134
|
-
* Time Complexity: O(log n)
|
|
134
|
+
* Time Complexity: O(log n)
|
|
135
135
|
* Space Complexity: O(1)
|
|
136
136
|
*
|
|
137
137
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -29,7 +29,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
29
29
|
/**
|
|
30
30
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
31
31
|
* initializes the tree with optional elements and options.
|
|
32
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
32
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
33
33
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
34
34
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
35
35
|
* elements to the
|
|
@@ -53,7 +53,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
56
|
-
* @param {
|
|
56
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
57
57
|
* identify and compare nodes in the Red-Black Tree.
|
|
58
58
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
59
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -74,11 +74,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
74
74
|
* @returns a new instance of a RedBlackTree object.
|
|
75
75
|
*/
|
|
76
76
|
createTree(options) {
|
|
77
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType,
|
|
77
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
81
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
81
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
82
82
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
83
83
|
* class.
|
|
84
84
|
*/
|
|
@@ -86,14 +86,23 @@ class RedBlackTree extends bst_1.BST {
|
|
|
86
86
|
return exemplar instanceof RedBlackTreeNode;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
|
-
* The function
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* that is not a valid exemplar.
|
|
94
|
-
* @returns a variable `node` which is of type `N | undefined`.
|
|
89
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
90
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
91
|
+
* data type.
|
|
92
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
95
93
|
*/
|
|
96
|
-
|
|
94
|
+
isNotNodeInstance(potentialKey) {
|
|
95
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
99
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
100
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
101
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
102
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
103
|
+
* @returns a node of type N or undefined.
|
|
104
|
+
*/
|
|
105
|
+
exemplarToNode(exemplar, value) {
|
|
97
106
|
let node;
|
|
98
107
|
if (exemplar === null || exemplar === undefined) {
|
|
99
108
|
return;
|
|
@@ -110,8 +119,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
110
119
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
111
120
|
}
|
|
112
121
|
}
|
|
113
|
-
else if (this.
|
|
114
|
-
node = this.createNode(exemplar,
|
|
122
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
123
|
+
node = this.createNode(exemplar, value, types_1.RBTNColor.RED);
|
|
115
124
|
}
|
|
116
125
|
else {
|
|
117
126
|
return;
|
|
@@ -123,13 +132,19 @@ class RedBlackTree extends bst_1.BST {
|
|
|
123
132
|
* Space Complexity: O(1)
|
|
124
133
|
*/
|
|
125
134
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* `
|
|
135
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
136
|
+
* Space Complexity: O(1)
|
|
137
|
+
*
|
|
138
|
+
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
139
|
+
* color changes to maintain the red-black tree properties.
|
|
140
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
141
|
+
* entry.
|
|
142
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
143
|
+
* being added to the binary search tree.
|
|
144
|
+
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
130
145
|
*/
|
|
131
|
-
add(keyOrNodeOrEntry) {
|
|
132
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
146
|
+
add(keyOrNodeOrEntry, value) {
|
|
147
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
133
148
|
if (newNode === undefined)
|
|
134
149
|
return;
|
|
135
150
|
newNode.left = this.Sentinel;
|
|
@@ -257,7 +272,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
257
272
|
* Space Complexity: O(1)
|
|
258
273
|
*/
|
|
259
274
|
isRealNode(node) {
|
|
260
|
-
|
|
275
|
+
if (node === this.Sentinel || node === undefined)
|
|
276
|
+
return false;
|
|
277
|
+
return node instanceof RedBlackTreeNode;
|
|
261
278
|
}
|
|
262
279
|
/**
|
|
263
280
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -276,7 +293,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
276
293
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
277
294
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
278
295
|
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
279
|
-
* @param {
|
|
296
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
280
297
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
281
298
|
* provided, the search will start from the root of the binary tree.
|
|
282
299
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
@@ -292,35 +309,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
292
309
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
293
310
|
}
|
|
294
311
|
/**
|
|
295
|
-
* Time Complexity: O(log n)
|
|
312
|
+
* Time Complexity: O(log n)
|
|
296
313
|
* Space Complexity: O(1)
|
|
297
314
|
*/
|
|
298
315
|
/**
|
|
299
|
-
* Time Complexity: O(log n)
|
|
300
|
-
* Space Complexity: O(1)
|
|
301
|
-
*
|
|
302
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
303
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
304
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
305
|
-
*/
|
|
306
|
-
getSuccessor(x) {
|
|
307
|
-
var _a;
|
|
308
|
-
if (x.right !== this.Sentinel) {
|
|
309
|
-
return (_a = this.getLeftMost(x.right)) !== null && _a !== void 0 ? _a : undefined;
|
|
310
|
-
}
|
|
311
|
-
let y = x.parent;
|
|
312
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
313
|
-
x = y;
|
|
314
|
-
y = y.parent;
|
|
315
|
-
}
|
|
316
|
-
return y;
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
320
|
-
* Space Complexity: O(1)
|
|
321
|
-
*/
|
|
322
|
-
/**
|
|
323
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
316
|
+
* Time Complexity: O(log n)
|
|
324
317
|
* Space Complexity: O(1)
|
|
325
318
|
*
|
|
326
319
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -329,11 +322,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
329
322
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
330
323
|
*/
|
|
331
324
|
getPredecessor(x) {
|
|
332
|
-
if (x.left
|
|
325
|
+
if (this.isRealNode(x.left)) {
|
|
333
326
|
return this.getRightMost(x.left);
|
|
334
327
|
}
|
|
335
328
|
let y = x.parent;
|
|
336
|
-
while (
|
|
329
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
337
330
|
x = y;
|
|
338
331
|
y = y.parent;
|
|
339
332
|
}
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNodeKeyOrNode,
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
8
|
+
import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeKeyOrNode, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
12
|
+
export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
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`.
|
|
@@ -21,42 +21,52 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
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:
|
|
24
|
+
constructor(key: K, value?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
|
-
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions
|
|
29
|
+
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
34
34
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
35
|
-
* @param {
|
|
35
|
+
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
36
36
|
* distinguish one node from another in the tree.
|
|
37
37
|
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
38
38
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
39
39
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
40
40
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
41
41
|
*/
|
|
42
|
-
createNode(key:
|
|
43
|
-
createTree(options?: TreeMultimapOptions): TREE;
|
|
42
|
+
createNode(key: K, value?: V, count?: number): N;
|
|
43
|
+
createTree(options?: TreeMultimapOptions<K>): TREE;
|
|
44
44
|
/**
|
|
45
45
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
46
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
46
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
47
47
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
48
|
* class.
|
|
49
49
|
*/
|
|
50
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
50
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
51
|
+
/**
|
|
52
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
53
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
54
|
+
* data type.
|
|
55
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
56
|
+
*/
|
|
57
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
51
58
|
/**
|
|
52
59
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
53
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`,
|
|
54
|
-
*
|
|
60
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
|
|
61
|
+
* can be one of the following:
|
|
62
|
+
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
63
|
+
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
64
|
+
* it defaults to `undefined`.
|
|
55
65
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
56
|
-
* times the
|
|
57
|
-
* @returns a
|
|
66
|
+
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
67
|
+
* @returns a node of type `N` or `undefined`.
|
|
58
68
|
*/
|
|
59
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
69
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | undefined;
|
|
60
70
|
/**
|
|
61
71
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
62
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -65,15 +75,19 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
65
75
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
66
76
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
67
77
|
*
|
|
68
|
-
* The
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @param [
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
79
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
80
|
+
* entry. It represents the key, node, or entry that you want to add to the binary tree.
|
|
81
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
82
|
+
* binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
|
|
83
|
+
* method.
|
|
84
|
+
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
85
|
+
* be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
|
|
86
|
+
* added once. However, you can specify a different value for `count` if you want to add
|
|
87
|
+
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
88
|
+
* was not successful.
|
|
89
|
+
*/
|
|
90
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | undefined;
|
|
77
91
|
/**
|
|
78
92
|
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
79
93
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -88,7 +102,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
88
102
|
* either keys, nodes, or entries.
|
|
89
103
|
* @returns The method is returning an array of type `N | undefined`.
|
|
90
104
|
*/
|
|
91
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
|
|
105
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[];
|
|
92
106
|
/**
|
|
93
107
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
94
108
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -137,6 +151,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
137
151
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
138
152
|
*/
|
|
139
153
|
clear(): void;
|
|
154
|
+
/**
|
|
155
|
+
* Time complexity: O(n)
|
|
156
|
+
* Space complexity: O(n)
|
|
157
|
+
*/
|
|
158
|
+
/**
|
|
159
|
+
* Time complexity: O(n)
|
|
160
|
+
* Space complexity: O(n)
|
|
161
|
+
*
|
|
162
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
163
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
164
|
+
*/
|
|
165
|
+
clone(): TREE;
|
|
140
166
|
/**
|
|
141
167
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
142
168
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -146,22 +172,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
146
172
|
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
147
173
|
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
148
174
|
* `undefined` if there is no node to add.
|
|
149
|
-
* @param {
|
|
175
|
+
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
150
176
|
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
151
|
-
* (`
|
|
177
|
+
* (`K`).
|
|
152
178
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
153
179
|
* added, or `undefined` if no node was added.
|
|
154
180
|
*/
|
|
155
|
-
protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
|
|
181
|
+
protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined;
|
|
156
182
|
/**
|
|
157
183
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
158
|
-
* @param {
|
|
159
|
-
* which the values will be swapped. It can be of type `
|
|
160
|
-
* @param {
|
|
184
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
185
|
+
* which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
|
|
186
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
161
187
|
* node where the values from the source node will be swapped to.
|
|
162
188
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
163
189
|
* if either `srcNode` or `destNode` is undefined.
|
|
164
190
|
*/
|
|
165
|
-
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
191
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
|
|
166
192
|
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
167
193
|
}
|