data-structure-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 (169) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.github/workflows/release-package.yml +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE_OF_CONDUCT.md +32 -10
  6. package/COMMANDS.md +3 -1
  7. package/CONTRIBUTING.md +4 -3
  8. package/README.md +103 -28
  9. package/SECURITY.md +1 -1
  10. package/benchmark/report.html +46 -1
  11. package/benchmark/report.json +563 -8
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
  19. package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
  20. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  25. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
  26. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  28. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
  30. package/dist/cjs/data-structures/hash/hash-map.js +5 -8
  31. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
  33. package/dist/cjs/data-structures/heap/heap.js +52 -34
  34. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  36. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  39. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  40. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  48. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  51. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  52. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  53. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  54. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  55. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
  57. package/dist/cjs/data-structures/queue/deque.js +3 -0
  58. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  59. package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
  60. package/dist/cjs/data-structures/queue/queue.js +3 -0
  61. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  62. package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
  63. package/dist/cjs/data-structures/stack/stack.js +10 -2
  64. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
  66. package/dist/cjs/data-structures/trie/trie.js +19 -4
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
  69. package/dist/cjs/types/common.d.ts +7 -0
  70. package/dist/cjs/types/common.js.map +1 -1
  71. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  72. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  73. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
  74. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  75. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  76. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  77. package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
  78. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  79. package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
  80. package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
  81. package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
  82. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  83. package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
  84. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  85. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
  86. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  87. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
  88. package/dist/mjs/data-structures/hash/hash-map.js +5 -8
  89. package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
  90. package/dist/mjs/data-structures/heap/heap.js +53 -35
  91. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  92. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  93. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  94. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  95. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  96. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  97. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  98. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
  99. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  102. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  103. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  104. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  105. package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
  106. package/dist/mjs/data-structures/queue/deque.js +3 -0
  107. package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
  108. package/dist/mjs/data-structures/queue/queue.js +3 -0
  109. package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
  110. package/dist/mjs/data-structures/stack/stack.js +10 -2
  111. package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
  112. package/dist/mjs/data-structures/trie/trie.js +20 -4
  113. package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
  114. package/dist/mjs/types/common.d.ts +7 -0
  115. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  116. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  117. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
  118. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  119. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  120. package/dist/umd/data-structure-typed.js +629 -595
  121. package/dist/umd/data-structure-typed.min.js +2 -2
  122. package/dist/umd/data-structure-typed.min.js.map +1 -1
  123. package/package.json +1 -1
  124. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  125. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  126. package/src/data-structures/binary-tree/bst.ts +193 -170
  127. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  128. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  129. package/src/data-structures/graph/abstract-graph.ts +1 -1
  130. package/src/data-structures/hash/hash-map.ts +8 -8
  131. package/src/data-structures/heap/heap.ts +57 -39
  132. package/src/data-structures/heap/max-heap.ts +5 -5
  133. package/src/data-structures/heap/min-heap.ts +5 -5
  134. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  135. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  136. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  137. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  138. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  139. package/src/data-structures/queue/deque.ts +4 -0
  140. package/src/data-structures/queue/queue.ts +4 -0
  141. package/src/data-structures/stack/stack.ts +12 -3
  142. package/src/data-structures/trie/trie.ts +23 -4
  143. package/src/interfaces/binary-tree.ts +14 -2
  144. package/src/types/common.ts +15 -1
  145. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  146. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  147. package/src/types/data-structures/hash/hash-map.ts +1 -2
  148. package/src/types/data-structures/heap/heap.ts +3 -1
  149. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  150. package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
  151. package/test/integration/index.html +87 -0
  152. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  153. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  154. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
  159. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
  160. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
  161. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  162. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  163. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  165. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  166. package/test/unit/data-structures/stack/stack.test.ts +2 -2
  167. package/test/unit/unrestricted-interconversion.test.ts +156 -0
  168. package/tsconfig-cjs.json +1 -1
  169. package/test/integration/conversion.test.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.47.5",
3
+ "version": "1.47.7",
4
4
  "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -6,8 +6,16 @@
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, IterationType } from '../../types';
9
+ import type {
10
+ AVLTreeNested,
11
+ AVLTreeNodeNested,
12
+ AVLTreeOptions,
13
+ BiTreeDeleteResult,
14
+ BSTNodeKeyOrNode,
15
+ BTNKey,
16
+ BTNodeExemplar
17
+ } from '../../types';
18
+ import { BTNCallback } from '../../types';
11
19
  import { IBinaryTree } from '../../interfaces';
12
20
 
13
21
  export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
@@ -19,25 +27,32 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
19
27
  }
20
28
  }
21
29
 
30
+ /**
31
+ * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
32
+ * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
33
+ * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
34
+ * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
35
+ * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
36
+ * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
37
+ * 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.
38
+ * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
39
+ */
22
40
  export 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>>>
23
41
  extends BST<V, N, TREE>
24
42
  implements IBinaryTree<V, N, TREE> {
25
43
 
26
- override options: AVLTreeOptions;
27
-
28
44
  /**
29
- * This is a constructor function for an AVL tree data structure in TypeScript.
30
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
31
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
32
- * options.
45
+ * The constructor function initializes an AVLTree object with optional elements and options.
46
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
47
+ * objects. It represents a collection of elements that will be added to the AVL tree during
48
+ * initialization.
49
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
50
+ * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
51
+ * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
33
52
  */
34
- constructor(options?: AVLTreeOptions) {
35
- super(options);
36
- if (options) {
37
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
38
- } else {
39
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
40
- }
53
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>) {
54
+ super([], options);
55
+ if (elements) super.addMany(elements);
41
56
  }
42
57
 
43
58
  /**
@@ -53,8 +68,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
53
68
  return new AVLTreeNode<V, N>(key, value) as N;
54
69
  }
55
70
 
71
+ /**
72
+ * The function creates a new AVL tree with the specified options and returns it.
73
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
74
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
75
+ * being created.
76
+ * @returns a new AVLTree object.
77
+ */
56
78
  override createTree(options?: AVLTreeOptions): TREE {
57
- return new AVLTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
79
+ return new AVLTree<V, N, TREE>([], {
80
+ iterationType: this.iterationType,
81
+ comparator: this.comparator, ...options
82
+ }) as TREE;
58
83
  }
59
84
 
60
85
  /**
@@ -66,23 +91,21 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
66
91
  * 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.
67
92
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
68
93
  *
69
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
70
- * balances the structure if necessary.
71
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
72
- * `BTNKey`, `N`, `null`, or `undefined`.
73
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
74
- * added to the binary search tree.
75
- * @returns The method is returning either a node (N) or undefined.
94
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
95
+ * a new node.
96
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
97
+ * entry.
98
+ * @returns The method is returning either the inserted node or `undefined`.
76
99
  */
77
- override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
78
- if (keyOrNode === null) return undefined;
79
- const inserted = super.add(keyOrNode, value);
100
+ override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
101
+ if (keyOrNodeOrEntry === null) return undefined;
102
+ const inserted = super.add(keyOrNodeOrEntry);
80
103
  if (inserted) this._balancePath(inserted);
81
104
  return inserted;
82
105
  }
83
106
 
84
107
  /**
85
- * 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.
108
+ * 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.
86
109
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
87
110
  */
88
111
 
@@ -115,8 +138,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
115
138
  return deletedResults;
116
139
  }
117
140
 
141
+
118
142
  /**
119
- * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
143
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
120
144
  * tree.
121
145
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
122
146
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -125,9 +149,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
125
149
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
126
150
  * if either `srcNode` or `destNode` is undefined.
127
151
  */
128
- protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
129
- srcNode = this.ensureNotKey(srcNode);
130
- destNode = this.ensureNotKey(destNode);
152
+ protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
153
+ srcNode = this.ensureNode(srcNode);
154
+ destNode = this.ensureNode(destNode);
131
155
 
132
156
  if (srcNode && destNode) {
133
157
  const { key, value, height } = destNode;
@@ -440,4 +464,10 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
440
464
  B && this._updateHeight(B);
441
465
  C && this._updateHeight(C);
442
466
  }
467
+
468
+ protected _replaceNode(oldNode: N, newNode: N): N {
469
+ newNode.height = oldNode.height;
470
+
471
+ return super._replaceNode(oldNode, newNode)
472
+ }
443
473
  }