graph-typed 1.47.6 → 1.47.7

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.
Files changed (28) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  11. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  12. package/dist/data-structures/hash/hash-map.js +5 -8
  13. package/dist/data-structures/trie/trie.d.ts +3 -0
  14. package/dist/data-structures/trie/trie.js +19 -4
  15. package/dist/interfaces/binary-tree.d.ts +3 -3
  16. package/dist/types/common.d.ts +6 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  18. package/package.json +2 -2
  19. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  20. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  21. package/src/data-structures/binary-tree/bst.ts +157 -154
  22. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  23. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  24. package/src/data-structures/hash/hash-map.ts +8 -8
  25. package/src/data-structures/trie/trie.ts +23 -4
  26. package/src/interfaces/binary-tree.ts +3 -3
  27. package/src/types/common.ts +11 -1
  28. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -6,21 +6,34 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
- import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
10
- import { BTNCallback, IterableEntriesOrKeys } from '../../types';
9
+ import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNKey, BTNodeExemplar } from '../../types';
10
+ 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
14
  constructor(key: BTNKey, value?: V);
15
15
  }
16
+ /**
17
+ * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
18
+ * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
19
+ * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
20
+ * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
21
+ * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
22
+ * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
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
+ * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
25
+ */
16
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> {
17
27
  /**
18
- * This is a constructor function for an AVL tree data structure in TypeScript.
19
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
20
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
21
- * options.
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>`
30
+ * objects. It represents a collection of elements that will be added to the AVL tree during
31
+ * initialization.
32
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
33
+ * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
34
+ * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
22
35
  */
23
- constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<AVLTreeOptions>);
36
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>);
24
37
  /**
25
38
  * The function creates a new AVL tree node with the specified key and value.
26
39
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -31,20 +44,29 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
31
44
  * @returns a new AVLTreeNode object with the specified key and value.
32
45
  */
33
46
  createNode(key: BTNKey, value?: V): N;
47
+ /**
48
+ * The function creates a new AVL tree with the specified options and returns it.
49
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
50
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
51
+ * being created.
52
+ * @returns a new AVLTree object.
53
+ */
34
54
  createTree(options?: AVLTreeOptions): TREE;
55
+ /**
56
+ * 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.
57
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
58
+ */
35
59
  /**
36
60
  * 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.
37
61
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
38
62
  *
39
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
40
- * balances the structure if necessary.
41
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
42
- * `BTNKey`, `N`, `null`, or `undefined`.
43
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
44
- * added to the binary search tree.
45
- * @returns The method is returning either a node (N) or undefined.
63
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
64
+ * a new node.
65
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
66
+ * entry.
67
+ * @returns The method is returning either the inserted node or `undefined`.
46
68
  */
47
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
69
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
48
70
  /**
49
71
  * 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.
50
72
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -66,12 +88,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
66
88
  */
67
89
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BiTreeDeleteResult<N>[];
68
90
  /**
69
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
70
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
71
- */
72
- init(elements: IterableEntriesOrKeys<V>): void;
73
- /**
74
- * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
91
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
75
92
  * tree.
76
93
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
77
94
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -80,7 +97,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
80
97
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
81
98
  * if either `srcNode` or `destNode` is undefined.
82
99
  */
83
- protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined;
100
+ protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
84
101
  /**
85
102
  * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
86
103
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -170,4 +187,5 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
170
187
  * @param {N} A - A is a node in a binary tree.
171
188
  */
172
189
  protected _balanceRL(A: N): void;
190
+ protected _replaceNode(oldNode: N, newNode: N): N;
173
191
  }
@@ -16,17 +16,30 @@ class AVLTreeNode extends bst_1.BSTNode {
16
16
  }
17
17
  }
18
18
  exports.AVLTreeNode = AVLTreeNode;
19
+ /**
20
+ * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
21
+ * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
22
+ * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
23
+ * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
24
+ * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
25
+ * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
26
+ * 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.
27
+ * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
28
+ */
19
29
  class AVLTree extends bst_1.BST {
20
30
  /**
21
- * This is a constructor function for an AVL tree data structure in TypeScript.
22
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
23
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
24
- * options.
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>`
33
+ * objects. It represents a collection of elements that will be added to the AVL tree during
34
+ * initialization.
35
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
36
+ * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
37
+ * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
25
38
  */
26
39
  constructor(elements, options) {
27
40
  super([], options);
28
41
  if (elements)
29
- this.init(elements);
42
+ super.addMany(elements);
30
43
  }
31
44
  /**
32
45
  * The function creates a new AVL tree node with the specified key and value.
@@ -40,25 +53,34 @@ class AVLTree extends bst_1.BST {
40
53
  createNode(key, value) {
41
54
  return new AVLTreeNode(key, value);
42
55
  }
56
+ /**
57
+ * The function creates a new AVL tree with the specified options and returns it.
58
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
59
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
60
+ * being created.
61
+ * @returns a new AVLTree object.
62
+ */
43
63
  createTree(options) {
44
64
  return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
45
65
  }
66
+ /**
67
+ * 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.
68
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
69
+ */
46
70
  /**
47
71
  * 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.
48
72
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
49
73
  *
50
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
51
- * balances the structure if necessary.
52
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
53
- * `BTNKey`, `N`, `null`, or `undefined`.
54
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
55
- * added to the binary search tree.
56
- * @returns The method is returning either a node (N) or undefined.
74
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
75
+ * a new node.
76
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
77
+ * entry.
78
+ * @returns The method is returning either the inserted node or `undefined`.
57
79
  */
58
- add(keyOrNode, value) {
59
- if (keyOrNode === null)
80
+ add(keyOrNodeOrEntry) {
81
+ if (keyOrNodeOrEntry === null)
60
82
  return undefined;
61
- const inserted = super.add(keyOrNode, value);
83
+ const inserted = super.add(keyOrNodeOrEntry);
62
84
  if (inserted)
63
85
  this._balancePath(inserted);
64
86
  return inserted;
@@ -94,24 +116,7 @@ class AVLTree extends bst_1.BST {
94
116
  return deletedResults;
95
117
  }
96
118
  /**
97
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
98
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
99
- */
100
- init(elements) {
101
- if (elements) {
102
- for (const entryOrKey of elements) {
103
- if (Array.isArray(entryOrKey)) {
104
- const [key, value] = entryOrKey;
105
- this.add(key, value);
106
- }
107
- else {
108
- this.add(entryOrKey);
109
- }
110
- }
111
- }
112
- }
113
- /**
114
- * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
119
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
115
120
  * tree.
116
121
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
117
122
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -120,9 +125,9 @@ class AVLTree extends bst_1.BST {
120
125
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
121
126
  * if either `srcNode` or `destNode` is undefined.
122
127
  */
123
- _swap(srcNode, destNode) {
124
- srcNode = this.ensureNotKey(srcNode);
125
- destNode = this.ensureNotKey(destNode);
128
+ _swapProperties(srcNode, destNode) {
129
+ srcNode = this.ensureNode(srcNode);
130
+ destNode = this.ensureNode(destNode);
126
131
  if (srcNode && destNode) {
127
132
  const { key, value, height } = destNode;
128
133
  const tempNode = this.createNode(key, value);
@@ -433,5 +438,9 @@ class AVLTree extends bst_1.BST {
433
438
  B && this._updateHeight(B);
434
439
  C && this._updateHeight(C);
435
440
  }
441
+ _replaceNode(oldNode, newNode) {
442
+ newNode.height = oldNode.height;
443
+ return super._replaceNode(oldNode, newNode);
444
+ }
436
445
  }
437
446
  exports.AVLTree = AVLTree;