directed-graph-typed 1.47.5 → 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 (71) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +36 -18
  2. package/dist/data-structures/binary-tree/avl-tree.js +46 -29
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
  4. package/dist/data-structures/binary-tree/binary-tree.js +182 -184
  5. package/dist/data-structures/binary-tree/bst.d.ts +73 -63
  6. package/dist/data-structures/binary-tree/bst.js +168 -169
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
  8. package/dist/data-structures/binary-tree/rb-tree.js +77 -31
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  10. package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  13. package/dist/data-structures/hash/hash-map.js +5 -8
  14. package/dist/data-structures/heap/heap.d.ts +19 -21
  15. package/dist/data-structures/heap/heap.js +52 -34
  16. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  17. package/dist/data-structures/heap/max-heap.js +2 -2
  18. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/min-heap.js +2 -2
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  23. package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
  24. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  26. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  30. package/dist/data-structures/queue/deque.d.ts +1 -0
  31. package/dist/data-structures/queue/deque.js +3 -0
  32. package/dist/data-structures/queue/queue.d.ts +1 -0
  33. package/dist/data-structures/queue/queue.js +3 -0
  34. package/dist/data-structures/stack/stack.d.ts +2 -1
  35. package/dist/data-structures/stack/stack.js +10 -2
  36. package/dist/data-structures/trie/trie.d.ts +3 -0
  37. package/dist/data-structures/trie/trie.js +19 -4
  38. package/dist/interfaces/binary-tree.d.ts +4 -2
  39. package/dist/types/common.d.ts +7 -0
  40. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  41. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  42. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  43. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  44. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  47. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  48. package/src/data-structures/binary-tree/bst.ts +193 -170
  49. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  50. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  51. package/src/data-structures/graph/abstract-graph.ts +1 -1
  52. package/src/data-structures/hash/hash-map.ts +8 -8
  53. package/src/data-structures/heap/heap.ts +57 -39
  54. package/src/data-structures/heap/max-heap.ts +5 -5
  55. package/src/data-structures/heap/min-heap.ts +5 -5
  56. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  57. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  58. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  59. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  60. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  61. package/src/data-structures/queue/deque.ts +4 -0
  62. package/src/data-structures/queue/queue.ts +4 -0
  63. package/src/data-structures/stack/stack.ts +12 -3
  64. package/src/data-structures/trie/trie.ts +23 -4
  65. package/src/interfaces/binary-tree.ts +14 -2
  66. package/src/types/common.ts +15 -1
  67. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  68. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  69. package/src/types/data-structures/hash/hash-map.ts +1 -2
  70. package/src/types/data-structures/heap/heap.ts +3 -1
  71. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -6,22 +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';
9
+ import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNKey, BTNodeExemplar } from '../../types';
10
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
- options: AVLTreeOptions;
18
27
  /**
19
- * This is a constructor function for an AVL tree data structure in TypeScript.
20
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
21
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
22
- * 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.
23
35
  */
24
- constructor(options?: AVLTreeOptions);
36
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>);
25
37
  /**
26
38
  * The function creates a new AVL tree node with the specified key and value.
27
39
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -32,6 +44,13 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
32
44
  * @returns a new AVLTreeNode object with the specified key and value.
33
45
  */
34
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
+ */
35
54
  createTree(options?: AVLTreeOptions): TREE;
36
55
  /**
37
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.
@@ -41,17 +60,15 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
41
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.
42
61
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
43
62
  *
44
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
45
- * balances the structure if necessary.
46
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
47
- * `BTNKey`, `N`, `null`, or `undefined`.
48
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
49
- * added to the binary search tree.
50
- * @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`.
51
68
  */
52
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
69
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
53
70
  /**
54
- * 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.
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.
55
72
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
56
73
  */
57
74
  /**
@@ -71,7 +88,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
71
88
  */
72
89
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BiTreeDeleteResult<N>[];
73
90
  /**
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
  }
@@ -9,7 +9,6 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
9
9
  * @license MIT License
10
10
  */
11
11
  const bst_1 = require("./bst");
12
- const types_1 = require("../../types");
13
12
  class AVLTreeNode extends bst_1.BSTNode {
14
13
  constructor(key, value) {
15
14
  super(key, value);
@@ -17,21 +16,30 @@ class AVLTreeNode extends bst_1.BSTNode {
17
16
  }
18
17
  }
19
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
+ */
20
29
  class AVLTree extends bst_1.BST {
21
30
  /**
22
- * This is a constructor function for an AVL tree data structure in TypeScript.
23
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
24
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
25
- * 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.
26
38
  */
27
- constructor(options) {
28
- super(options);
29
- if (options) {
30
- this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
31
- }
32
- else {
33
- this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
34
- }
39
+ constructor(elements, options) {
40
+ super([], options);
41
+ if (elements)
42
+ super.addMany(elements);
35
43
  }
36
44
  /**
37
45
  * The function creates a new AVL tree node with the specified key and value.
@@ -45,8 +53,15 @@ class AVLTree extends bst_1.BST {
45
53
  createNode(key, value) {
46
54
  return new AVLTreeNode(key, value);
47
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
+ */
48
63
  createTree(options) {
49
- return new AVLTree(Object.assign(Object.assign({}, this.options), options));
64
+ return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
50
65
  }
51
66
  /**
52
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.
@@ -56,24 +71,22 @@ class AVLTree extends bst_1.BST {
56
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.
57
72
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
58
73
  *
59
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
60
- * balances the structure if necessary.
61
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
62
- * `BTNKey`, `N`, `null`, or `undefined`.
63
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
64
- * added to the binary search tree.
65
- * @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`.
66
79
  */
67
- add(keyOrNode, value) {
68
- if (keyOrNode === null)
80
+ add(keyOrNodeOrEntry) {
81
+ if (keyOrNodeOrEntry === null)
69
82
  return undefined;
70
- const inserted = super.add(keyOrNode, value);
83
+ const inserted = super.add(keyOrNodeOrEntry);
71
84
  if (inserted)
72
85
  this._balancePath(inserted);
73
86
  return inserted;
74
87
  }
75
88
  /**
76
- * 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.
89
+ * 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.
77
90
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
78
91
  */
79
92
  /**
@@ -103,7 +116,7 @@ class AVLTree extends bst_1.BST {
103
116
  return deletedResults;
104
117
  }
105
118
  /**
106
- * 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
107
120
  * tree.
108
121
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
109
122
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -112,9 +125,9 @@ class AVLTree extends bst_1.BST {
112
125
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
113
126
  * if either `srcNode` or `destNode` is undefined.
114
127
  */
115
- _swap(srcNode, destNode) {
116
- srcNode = this.ensureNotKey(srcNode);
117
- destNode = this.ensureNotKey(destNode);
128
+ _swapProperties(srcNode, destNode) {
129
+ srcNode = this.ensureNode(srcNode);
130
+ destNode = this.ensureNode(destNode);
118
131
  if (srcNode && destNode) {
119
132
  const { key, value, height } = destNode;
120
133
  const tempNode = this.createNode(key, value);
@@ -425,5 +438,9 @@ class AVLTree extends bst_1.BST {
425
438
  B && this._updateHeight(B);
426
439
  C && this._updateHeight(C);
427
440
  }
441
+ _replaceNode(oldNode, newNode) {
442
+ newNode.height = oldNode.height;
443
+ return super._replaceNode(oldNode, newNode);
444
+ }
428
445
  }
429
446
  exports.AVLTree = AVLTree;