red-black-tree-typed 1.48.2 → 1.48.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -24
- package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +89 -87
- package/dist/data-structures/binary-tree/binary-tree.js +67 -58
- package/dist/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/data-structures/binary-tree/bst.js +54 -57
- package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +4 -0
- package/dist/data-structures/graph/directed-graph.d.ts +25 -7
- package/dist/data-structures/graph/directed-graph.js +58 -12
- package/dist/data-structures/graph/undirected-graph.d.ts +25 -6
- package/dist/data-structures/graph/undirected-graph.js +70 -7
- 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/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/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +147 -136
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +5 -0
- package/src/data-structures/graph/directed-graph.ts +61 -12
- package/src/data-structures/graph/undirected-graph.ts +75 -7
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- 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/README.md
CHANGED
|
@@ -39,37 +39,34 @@ yarn add red-black-tree-typed
|
|
|
39
39
|
#### TS
|
|
40
40
|
|
|
41
41
|
```typescript
|
|
42
|
-
import {RedBlackTree
|
|
42
|
+
import {RedBlackTree} from 'data-structure-typed';
|
|
43
43
|
// /* or if you prefer */ import {RedBlackTree} from 'red-black-tree-typed';
|
|
44
44
|
|
|
45
|
-
const rbTree = new RedBlackTree<
|
|
45
|
+
const rbTree = new RedBlackTree<number>();
|
|
46
46
|
|
|
47
47
|
const idsOrVals = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
48
|
-
rbTree.addMany(idsOrVals
|
|
48
|
+
rbTree.addMany(idsOrVals);
|
|
49
49
|
|
|
50
|
-
const node6 = rbTree.
|
|
50
|
+
const node6 = rbTree.getNode(6);
|
|
51
51
|
node6 && rbTree.getHeight(node6) // 3
|
|
52
52
|
node6 && rbTree.getDepth(node6) // 1
|
|
53
|
-
const getNodeById = rbTree.
|
|
53
|
+
const getNodeById = rbTree.getNodeByKey(10);
|
|
54
54
|
getNodeById?.id // 10
|
|
55
55
|
|
|
56
56
|
const getMinNodeByRoot = rbTree.getLeftMost();
|
|
57
57
|
getMinNodeByRoot?.id // 1
|
|
58
58
|
|
|
59
|
-
const node15 = rbTree.
|
|
59
|
+
const node15 = rbTree.getNodeByKey(15);
|
|
60
60
|
const getMinNodeBySpecificNode = node15 && rbTree.getLeftMost(node15);
|
|
61
61
|
getMinNodeBySpecificNode?.id // 12
|
|
62
62
|
|
|
63
|
-
const subTreeSum = node15 && rbTree.subTreeSum(node15);
|
|
64
|
-
subTreeSum // 70
|
|
65
|
-
|
|
66
63
|
const lesserSum = rbTree.lesserSum(10);
|
|
67
64
|
lesserSum // 45
|
|
68
65
|
|
|
69
|
-
const node11 = rbTree.
|
|
66
|
+
const node11 = rbTree.getNodeByKey(11);
|
|
70
67
|
node11?.id // 11
|
|
71
68
|
|
|
72
|
-
const dfs = rbTree.dfs('in'
|
|
69
|
+
const dfs = rbTree.dfs('in');
|
|
73
70
|
dfs[0].id // 1
|
|
74
71
|
rbTree.perfectlyBalance();
|
|
75
72
|
const bfs = rbTree.bfs('node');
|
|
@@ -146,7 +143,7 @@ const rbTree = new RedBlackTree();
|
|
|
146
143
|
const idsOrVals = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
147
144
|
rbTree.addMany(idsOrVals, idsOrVals);
|
|
148
145
|
|
|
149
|
-
const node6 = rbTree.
|
|
146
|
+
const node6 = rbTree.getNodeByKey(6);
|
|
150
147
|
node6 && rbTree.getHeight(node6) // 3
|
|
151
148
|
node6 && rbTree.getDepth(node6) // 1
|
|
152
149
|
const getNodeById = rbTree.get(10, 'id');
|
|
@@ -155,23 +152,17 @@ getNodeById?.id // 10
|
|
|
155
152
|
const getMinNodeByRoot = rbTree.getLeftMost();
|
|
156
153
|
getMinNodeByRoot?.id // 1
|
|
157
154
|
|
|
158
|
-
const node15 = rbTree.
|
|
155
|
+
const node15 = rbTree.getNodeByKey(15);
|
|
159
156
|
const getMinNodeBySpecificNode = node15 && rbTree.getLeftMost(node15);
|
|
160
157
|
getMinNodeBySpecificNode?.id // 12
|
|
161
158
|
|
|
162
|
-
const
|
|
163
|
-
subTreeSum // 70
|
|
164
|
-
|
|
165
|
-
const lesserSum = rbTree.lesserSum(10);
|
|
166
|
-
lesserSum // 45
|
|
167
|
-
|
|
168
|
-
const node11 = rbTree.get(11);
|
|
159
|
+
const node11 = rbTree.getNodeByKey(11);
|
|
169
160
|
node11?.id // 11
|
|
170
161
|
|
|
171
|
-
const dfs = rbTree.
|
|
162
|
+
const dfs = rbTree.dfs('in');
|
|
172
163
|
dfs[0].id // 1
|
|
173
164
|
rbTree.perfectlyBalance();
|
|
174
|
-
const bfs = rbTree.
|
|
165
|
+
const bfs = rbTree.bfs('node');
|
|
175
166
|
rbTree.isPerfectlyBalanced() && bfs[0].id // 8
|
|
176
167
|
|
|
177
168
|
rbTree.delete(11, true)[0].deleted?.id // 11
|
|
@@ -227,10 +218,10 @@ rbTree.isAVLBalanced(); // true
|
|
|
227
218
|
rbTree.getHeight() // 1
|
|
228
219
|
|
|
229
220
|
rbTree.isAVLBalanced(); // true
|
|
230
|
-
const lastBFSIds = rbTree.
|
|
221
|
+
const lastBFSIds = rbTree.bfs();
|
|
231
222
|
lastBFSIds[0] // 12
|
|
232
223
|
|
|
233
|
-
const lastBFSNodes = rbTree.
|
|
224
|
+
const lastBFSNodes = rbTree.bfs('node');
|
|
234
225
|
lastBFSNodes[0].id // 12
|
|
235
226
|
```
|
|
236
227
|
|
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode,
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNodeExemplar } from '../../types';
|
|
10
10
|
import { BTNCallback } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
12
|
+
export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
13
13
|
height: number;
|
|
14
|
-
constructor(key:
|
|
14
|
+
constructor(key: K, value?: V);
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
@@ -23,27 +23,27 @@ export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeN
|
|
|
23
23
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
24
24
|
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
25
25
|
*/
|
|
26
|
-
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
26
|
+
export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
27
27
|
/**
|
|
28
28
|
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
29
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
29
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
30
30
|
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
31
31
|
* initialization.
|
|
32
32
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
33
33
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
34
34
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
35
35
|
*/
|
|
36
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions
|
|
36
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>);
|
|
37
37
|
/**
|
|
38
38
|
* The function creates a new AVL tree node with the specified key and value.
|
|
39
|
-
* @param {
|
|
39
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
40
40
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
41
41
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
42
42
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
43
43
|
* node type `N`.
|
|
44
44
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
45
45
|
*/
|
|
46
|
-
createNode(key:
|
|
46
|
+
createNode(key: K, value?: V): N;
|
|
47
47
|
/**
|
|
48
48
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
49
49
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -51,13 +51,13 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
51
51
|
* being created.
|
|
52
52
|
* @returns a new AVLTree object.
|
|
53
53
|
*/
|
|
54
|
-
createTree(options?: AVLTreeOptions): TREE;
|
|
54
|
+
createTree(options?: AVLTreeOptions<K>): TREE;
|
|
55
55
|
/**
|
|
56
56
|
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
57
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
57
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
58
58
|
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
59
59
|
*/
|
|
60
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
60
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
61
61
|
/**
|
|
62
62
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
63
63
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -72,7 +72,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
72
72
|
* entry.
|
|
73
73
|
* @returns The method is returning either the inserted node or `undefined`.
|
|
74
74
|
*/
|
|
75
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
75
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
78
78
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -96,14 +96,14 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
96
96
|
/**
|
|
97
97
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
98
98
|
* tree.
|
|
99
|
-
* @param {
|
|
100
|
-
* needs to be swapped with the destination node. It can be of type `
|
|
101
|
-
* @param {
|
|
99
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
100
|
+
* needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
|
|
101
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
102
102
|
* node where the values from the source node will be swapped to.
|
|
103
103
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
104
104
|
* if either `srcNode` or `destNode` is undefined.
|
|
105
105
|
*/
|
|
106
|
-
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
106
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
|
|
107
107
|
/**
|
|
108
108
|
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
|
|
109
109
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -29,7 +29,7 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
29
29
|
class AVLTree extends bst_1.BST {
|
|
30
30
|
/**
|
|
31
31
|
* The constructor function initializes an AVLTree object 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 a collection of elements that will be added to the AVL tree during
|
|
34
34
|
* initialization.
|
|
35
35
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -43,7 +43,7 @@ class AVLTree extends bst_1.BST {
|
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* The function creates a new AVL tree node with the specified key and value.
|
|
46
|
-
* @param {
|
|
46
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
47
47
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
48
48
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
49
49
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
@@ -61,11 +61,11 @@ class AVLTree extends bst_1.BST {
|
|
|
61
61
|
* @returns a new AVLTree object.
|
|
62
62
|
*/
|
|
63
63
|
createTree(options) {
|
|
64
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType,
|
|
64
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
67
|
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
68
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
68
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
69
69
|
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
70
70
|
*/
|
|
71
71
|
isNode(exemplar) {
|
|
@@ -126,9 +126,9 @@ class AVLTree extends bst_1.BST {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
128
128
|
* tree.
|
|
129
|
-
* @param {
|
|
130
|
-
* needs to be swapped with the destination node. It can be of type `
|
|
131
|
-
* @param {
|
|
129
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
130
|
+
* needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
|
|
131
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
132
132
|
* node where the values from the source node will be swapped to.
|
|
133
133
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
134
134
|
* if either `srcNode` or `destNode` is undefined.
|