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
@@ -44,30 +44,41 @@ export class BSTNode extends BinaryTreeNode {
44
44
  this._right = v;
45
45
  }
46
46
  }
47
+ /**
48
+ * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
49
+ * 2. Unique Keys: No duplicate keys in a standard BST.
50
+ * 3. Efficient Search: Enables quick search, minimum, and maximum operations.
51
+ * 4. Inorder Traversal: Yields nodes in ascending order.
52
+ * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
53
+ * 6. Balance Variability: Can become unbalanced; special types maintain balance.
54
+ * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
55
+ */
47
56
  export class BST extends BinaryTree {
48
- options;
49
57
  /**
50
- * The constructor function initializes a binary search tree with an optional comparator function.
51
- * @param {BSTOptions} [options] - An optional object that contains additional configuration options
52
- * for the binary search tree.
58
+ * This is the constructor function for a binary search tree class in TypeScript, which initializes
59
+ * the tree with optional elements and options.
60
+ * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
61
+ * binary search tree.
62
+ * @param [options] - The `options` parameter is an optional object that can contain additional
63
+ * configuration options for the binary search tree. It can have the following properties:
53
64
  */
54
- constructor(options) {
55
- super(options);
65
+ constructor(elements, options) {
66
+ super([], options);
56
67
  if (options) {
57
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
58
- }
59
- else {
60
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
68
+ const { comparator } = options;
69
+ if (comparator) {
70
+ this.comparator = comparator;
71
+ }
61
72
  }
62
73
  this._root = undefined;
74
+ if (elements)
75
+ this.addMany(elements);
63
76
  }
64
77
  _root;
65
- /**
66
- * Get the root node of the binary tree.
67
- */
68
78
  get root() {
69
79
  return this._root;
70
80
  }
81
+ comparator = (a, b) => a - b;
71
82
  /**
72
83
  * The function creates a new binary search tree node with the given key and value.
73
84
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -79,8 +90,18 @@ export class BST extends BinaryTree {
79
90
  createNode(key, value) {
80
91
  return new BSTNode(key, value);
81
92
  }
93
+ /**
94
+ * The function creates a new binary search tree with the specified options.
95
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
96
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
97
+ * that defines various options for creating a binary search tree.
98
+ * @returns a new instance of the BST class with the specified options.
99
+ */
82
100
  createTree(options) {
83
- return new BST({ ...this.options, ...options });
101
+ return new BST([], {
102
+ iterationType: this.iterationType,
103
+ comparator: this.comparator, ...options
104
+ });
84
105
  }
85
106
  /**
86
107
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
@@ -90,158 +111,138 @@ export class BST extends BinaryTree {
90
111
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
91
112
  * Space Complexity: O(1) - Constant space is used.
92
113
  *
93
- * The `add` function adds a new node to a binary search tree based on the provided key and value.
94
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
95
- * following types:
96
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
97
- * key or node being added to the binary search tree.
98
- * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
99
- * no node was inserted, it returns `undefined`.
114
+ * The `add` function adds a new node to a binary search tree, either by key or by providing a node
115
+ * object.
116
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
117
+ * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
118
+ * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
100
119
  */
101
- add(keyOrNode, value) {
102
- if (keyOrNode === null)
120
+ add(keyOrNodeOrEntry) {
121
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
103
122
  return undefined;
104
- // TODO support node as a parameter
105
- let inserted;
123
+ }
106
124
  let newNode;
107
- if (keyOrNode instanceof BSTNode) {
108
- newNode = keyOrNode;
125
+ if (keyOrNodeOrEntry instanceof BSTNode) {
126
+ newNode = keyOrNodeOrEntry;
109
127
  }
110
- else if (this.isNodeKey(keyOrNode)) {
111
- newNode = this.createNode(keyOrNode, value);
128
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
129
+ newNode = this.createNode(keyOrNodeOrEntry);
130
+ }
131
+ else if (this.isEntry(keyOrNodeOrEntry)) {
132
+ const [key, value] = keyOrNodeOrEntry;
133
+ if (key === undefined || key === null) {
134
+ return;
135
+ }
136
+ else {
137
+ newNode = this.createNode(key, value);
138
+ }
112
139
  }
113
140
  else {
114
- newNode = undefined;
141
+ return;
115
142
  }
116
143
  if (this.root === undefined) {
117
144
  this._setRoot(newNode);
118
- this._size = this.size + 1;
119
- inserted = this.root;
145
+ this._size++;
146
+ return this.root;
120
147
  }
121
- else {
122
- let cur = this.root;
123
- let traversing = true;
124
- while (traversing) {
125
- if (cur !== undefined && newNode !== undefined) {
126
- if (this._compare(cur.key, newNode.key) === CP.eq) {
127
- if (newNode) {
128
- cur.value = newNode.value;
129
- }
130
- //Duplicates are not accepted.
131
- traversing = false;
132
- inserted = cur;
133
- }
134
- else if (this._compare(cur.key, newNode.key) === CP.gt) {
135
- // Traverse left of the node
136
- if (cur.left === undefined) {
137
- if (newNode) {
138
- newNode.parent = cur;
139
- }
140
- //Add to the left of the current node
141
- cur.left = newNode;
142
- this._size = this.size + 1;
143
- traversing = false;
144
- inserted = cur.left;
145
- }
146
- else {
147
- //Traverse the left of the current node
148
- if (cur.left)
149
- cur = cur.left;
150
- }
151
- }
152
- else if (this._compare(cur.key, newNode.key) === CP.lt) {
153
- // Traverse right of the node
154
- if (cur.right === undefined) {
155
- if (newNode) {
156
- newNode.parent = cur;
157
- }
158
- //Add to the right of the current node
159
- cur.right = newNode;
160
- this._size = this.size + 1;
161
- traversing = false;
162
- inserted = cur.right;
163
- }
164
- else {
165
- //Traverse the left of the current node
166
- if (cur.right)
167
- cur = cur.right;
168
- }
169
- }
148
+ let current = this.root;
149
+ while (current !== undefined) {
150
+ if (this._compare(current.key, newNode.key) === CP.eq) {
151
+ // if (current !== newNode) {
152
+ // The key value is the same but the reference is different, update the value of the existing node
153
+ this._replaceNode(current, newNode);
154
+ return newNode;
155
+ // } else {
156
+ // The key value is the same and the reference is the same, replace the entire node
157
+ // this._replaceNode(current, newNode);
158
+ // return;
159
+ // }
160
+ }
161
+ else if (this._compare(current.key, newNode.key) === CP.gt) {
162
+ if (current.left === undefined) {
163
+ current.left = newNode;
164
+ newNode.parent = current;
165
+ this._size++;
166
+ return newNode;
170
167
  }
171
- else {
172
- traversing = false;
168
+ current = current.left;
169
+ }
170
+ else {
171
+ if (current.right === undefined) {
172
+ current.right = newNode;
173
+ newNode.parent = current;
174
+ this._size++;
175
+ return newNode;
173
176
  }
177
+ current = current.right;
174
178
  }
175
179
  }
176
- return inserted;
180
+ return undefined;
177
181
  }
178
182
  /**
179
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
180
- * Space Complexity: O(n) - Additional space is required for the sorted array.
183
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
184
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
181
185
  */
182
186
  /**
183
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
184
- * Space Complexity: O(n) - Additional space is required for the sorted array.
187
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
188
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
185
189
  *
186
- * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
187
- * to a binary search tree.
188
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
189
- * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
190
- * node), or `undefined`.
191
- * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
192
- * nodes being added. If provided, the length of the `data` array must be the same as the length of
193
- * the `keysOrNodes` array.
190
+ * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
191
+ * unbalanced manner, and returns an array of the inserted nodes.
192
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
193
+ * binary tree.
194
194
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
195
- * adding the nodes. The default value is `true`.
195
+ * adding the nodes. The default value is true.
196
196
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
197
- * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
198
- * default value of `this.iterationType`, which means it will use the iteration type specified in the
199
- * current instance of the binary search tree
200
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
197
+ * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
198
+ * value of `this.iterationType`, which means it will use the iteration type specified by the binary
199
+ * tree instance.
200
+ * @returns The `addMany` function returns an array of `N` or `undefined` values.
201
201
  */
202
- addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.options.iterationType) {
203
- // TODO this addMany function is inefficient, it should be optimized
204
- function hasNoUndefined(arr) {
205
- return arr.indexOf(undefined) === -1;
206
- }
207
- if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
208
- return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
209
- }
202
+ addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
210
203
  const inserted = [];
211
- const combinedArr = keysOrNodes.map((value, index) => [value, data?.[index]]);
212
- let sorted = [];
213
- function _isNodeOrUndefinedTuple(arr) {
214
- for (const [keyOrNode] of arr)
215
- if (keyOrNode instanceof BSTNode)
216
- return true;
217
- return false;
204
+ if (!isBalanceAdd) {
205
+ for (const kve of keysOrNodesOrEntries) {
206
+ const nn = this.add(kve);
207
+ inserted.push(nn);
208
+ }
209
+ return inserted;
218
210
  }
219
- const _isBinaryTreeKeyOrNullTuple = (arr) => {
220
- for (const [keyOrNode] of arr)
221
- if (this.isNodeKey(keyOrNode))
222
- return true;
223
- return false;
211
+ const realBTNExemplars = [];
212
+ const isRealBTNExemplar = (kve) => {
213
+ if (kve === undefined || kve === null)
214
+ return false;
215
+ return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
224
216
  };
225
- let sortedKeysOrNodes = [], sortedData = [];
226
- if (_isNodeOrUndefinedTuple(combinedArr)) {
227
- sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
217
+ for (const kve of keysOrNodesOrEntries) {
218
+ isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
228
219
  }
229
- else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
230
- sorted = combinedArr.sort((a, b) => a[0] - b[0]);
231
- }
232
- else {
233
- throw new Error('Invalid input keysOrNodes');
234
- }
235
- sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
236
- sortedData = sorted.map(([, value]) => value);
237
- const _dfs = (arr, data) => {
220
+ // TODO this addMany function is inefficient, it should be optimized
221
+ let sorted = [];
222
+ sorted = realBTNExemplars.sort((a, b) => {
223
+ let aR, bR;
224
+ if (this.isEntry(a))
225
+ aR = a[0];
226
+ else if (this.isRealNode(a))
227
+ aR = a.key;
228
+ else
229
+ aR = a;
230
+ if (this.isEntry(b))
231
+ bR = b[0];
232
+ else if (this.isRealNode(b))
233
+ bR = b.key;
234
+ else
235
+ bR = b;
236
+ return aR - bR;
237
+ });
238
+ const _dfs = (arr) => {
238
239
  if (arr.length === 0)
239
240
  return;
240
241
  const mid = Math.floor((arr.length - 1) / 2);
241
- const newNode = this.add(arr[mid], data?.[mid]);
242
+ const newNode = this.add(arr[mid]);
242
243
  inserted.push(newNode);
243
- _dfs(arr.slice(0, mid), data?.slice(0, mid));
244
- _dfs(arr.slice(mid + 1), data?.slice(mid + 1));
244
+ _dfs(arr.slice(0, mid));
245
+ _dfs(arr.slice(mid + 1));
245
246
  };
246
247
  const _iterate = () => {
247
248
  const n = sorted.length;
@@ -252,7 +253,7 @@ export class BST extends BinaryTree {
252
253
  const [l, r] = popped;
253
254
  if (l <= r) {
254
255
  const m = l + Math.floor((r - l) / 2);
255
- const newNode = this.add(sortedKeysOrNodes[m], sortedData?.[m]);
256
+ const newNode = this.add(sorted[m]);
256
257
  inserted.push(newNode);
257
258
  stack.push([m + 1, r]);
258
259
  stack.push([l, m - 1]);
@@ -261,7 +262,7 @@ export class BST extends BinaryTree {
261
262
  }
262
263
  };
263
264
  if (iterationType === IterationType.RECURSIVE) {
264
- _dfs(sortedKeysOrNodes, sortedData);
265
+ _dfs(sorted);
265
266
  }
266
267
  else {
267
268
  _iterate();
@@ -269,8 +270,8 @@ export class BST extends BinaryTree {
269
270
  return inserted;
270
271
  }
271
272
  /**
272
- * Time Complexity: O(log n) - Average case for a balanced tree.
273
- * Space Complexity: O(1) - Constant space is used.
273
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
274
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
274
275
  */
275
276
  /**
276
277
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -287,7 +288,7 @@ export class BST extends BinaryTree {
287
288
  * the key of the leftmost node if the comparison result is greater than, and the key of the
288
289
  * rightmost node otherwise. If no node is found, it returns 0.
289
290
  */
290
- lastKey(beginRoot = this.root, iterationType = this.options.iterationType) {
291
+ lastKey(beginRoot = this.root, iterationType = this.iterationType) {
291
292
  if (this._compare(0, 1) === CP.lt)
292
293
  return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
293
294
  else if (this._compare(0, 1) === CP.gt)
@@ -297,7 +298,7 @@ export class BST extends BinaryTree {
297
298
  }
298
299
  /**
299
300
  * Time Complexity: O(log n) - Average case for a balanced tree.
300
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
301
+ * Space Complexity: O(1) - Constant space is used.
301
302
  */
302
303
  /**
303
304
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -345,7 +346,11 @@ export class BST extends BinaryTree {
345
346
  }
346
347
  }
347
348
  /**
348
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
349
+ * Time Complexity: O(log n) - Average case for a balanced tree.
350
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
351
+ */
352
+ /**
353
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
349
354
  * otherwise it returns the key itself.
350
355
  * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
351
356
  * `undefined`.
@@ -353,13 +358,9 @@ export class BST extends BinaryTree {
353
358
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
354
359
  * @returns either a node object (N) or undefined.
355
360
  */
356
- ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
361
+ ensureNode(key, iterationType = IterationType.ITERATIVE) {
357
362
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
358
363
  }
359
- /**
360
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
361
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
362
- */
363
364
  /**
364
365
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
365
366
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -383,8 +384,8 @@ export class BST extends BinaryTree {
383
384
  * performed on the binary tree. It can have two possible values:
384
385
  * @returns The method returns an array of nodes (`N[]`).
385
386
  */
386
- getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
387
- beginRoot = this.ensureNotKey(beginRoot);
387
+ getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
388
+ beginRoot = this.ensureNode(beginRoot);
388
389
  if (!beginRoot)
389
390
  return [];
390
391
  const ans = [];
@@ -464,8 +465,8 @@ export class BST extends BinaryTree {
464
465
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
465
466
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
466
467
  */
467
- lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.options.iterationType) {
468
- targetNode = this.ensureNotKey(targetNode);
468
+ lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
469
+ targetNode = this.ensureNode(targetNode);
469
470
  const ans = [];
470
471
  if (!targetNode)
471
472
  return ans;
@@ -505,17 +506,8 @@ export class BST extends BinaryTree {
505
506
  }
506
507
  }
507
508
  /**
508
- * Balancing Adjustment:
509
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
510
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
511
- *
512
- * Use Cases and Efficiency:
513
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
514
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
515
- */
516
- /**
517
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
518
- * Space Complexity: O(n) - Additional space is required for the sorted array.
509
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
510
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
519
511
  */
520
512
  /**
521
513
  * Time Complexity: O(n) - Building a balanced tree from a sorted array.
@@ -528,7 +520,7 @@ export class BST extends BinaryTree {
528
520
  * values:
529
521
  * @returns The function `perfectlyBalance` returns a boolean value.
530
522
  */
531
- perfectlyBalance(iterationType = this.options.iterationType) {
523
+ perfectlyBalance(iterationType = this.iterationType) {
532
524
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
533
525
  this.clear();
534
526
  if (sorted.length < 1)
@@ -539,7 +531,7 @@ export class BST extends BinaryTree {
539
531
  return;
540
532
  const m = l + Math.floor((r - l) / 2);
541
533
  const midNode = sorted[m];
542
- this.add(midNode.key, midNode.value);
534
+ this.add([midNode.key, midNode.value]);
543
535
  buildBalanceBST(l, m - 1);
544
536
  buildBalanceBST(m + 1, r);
545
537
  };
@@ -556,7 +548,7 @@ export class BST extends BinaryTree {
556
548
  const m = l + Math.floor((r - l) / 2);
557
549
  const midNode = sorted[m];
558
550
  debugger;
559
- this.add(midNode.key, midNode.value);
551
+ this.add([midNode.key, midNode.value]);
560
552
  stack.push([m + 1, r]);
561
553
  stack.push([l, m - 1]);
562
554
  }
@@ -566,8 +558,17 @@ export class BST extends BinaryTree {
566
558
  }
567
559
  }
568
560
  /**
569
- * Time Complexity: O(n) - Visiting each node once.
570
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
561
+ * Balancing Adjustment:
562
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
563
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
564
+ *
565
+ * Use Cases and Efficiency:
566
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
567
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
568
+ */
569
+ /**
570
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
571
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
571
572
  */
572
573
  /**
573
574
  * Time Complexity: O(n) - Visiting each node once.
@@ -578,7 +579,7 @@ export class BST extends BinaryTree {
578
579
  * to check if the AVL tree is balanced. It can have two possible values:
579
580
  * @returns a boolean value.
580
581
  */
581
- isAVLBalanced(iterationType = this.options.iterationType) {
582
+ isAVLBalanced(iterationType = this.iterationType) {
582
583
  if (!this.root)
583
584
  return true;
584
585
  let balanced = true;
@@ -638,7 +639,7 @@ export class BST extends BinaryTree {
638
639
  * than), CP.lt (less than), or CP.eq (equal).
639
640
  */
640
641
  _compare(a, b) {
641
- const compared = this.options.comparator(a, b);
642
+ const compared = this.comparator(a, b);
642
643
  if (compared > 0)
643
644
  return CP.gt;
644
645
  else if (compared < 0)
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
8
+ import { BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
9
9
  import { BST, BSTNode } from './bst';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
@@ -21,35 +21,54 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
21
21
  */
22
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> {
23
23
  Sentinel: N;
24
- options: RBTreeOptions;
25
24
  /**
26
- * The constructor function initializes a Red-Black Tree with an optional set of options.
27
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
28
- * passed to the constructor. It is used to configure the RBTree object with specific options.
29
- */
30
- constructor(options?: RBTreeOptions);
25
+ * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
26
+ * initializes the tree with optional elements and options.
27
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
28
+ * objects. It represents the initial elements that will be added to the RBTree during its
29
+ * construction. If this parameter is provided, the `addMany` method is called to add all the
30
+ * elements to the
31
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
32
+ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
33
+ * only a subset of the properties defined in the `RBTreeOptions` interface.
34
+ */
35
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions>);
31
36
  protected _root: N;
32
37
  get root(): N;
33
38
  protected _size: number;
34
39
  get size(): number;
40
+ /**
41
+ * The function creates a new Red-Black Tree node with the specified key, value, and color.
42
+ * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
43
+ * identify and compare nodes in the Red-Black Tree.
44
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
45
+ * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
46
+ * specific type when using the `createNode` method.
47
+ * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
48
+ * Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
49
+ * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
50
+ * value, and color.
51
+ */
35
52
  createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
53
+ /**
54
+ * The function creates a Red-Black Tree with the specified options and returns it.
55
+ * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
56
+ * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
57
+ * class.
58
+ * @returns a new instance of a RedBlackTree object.
59
+ */
36
60
  createTree(options?: RBTreeOptions): TREE;
37
61
  /**
38
62
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
39
63
  * Space Complexity: O(1)
40
64
  */
41
65
  /**
42
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
43
- * Space Complexity: O(1)
44
- *
45
- * The `add` function adds a new node to a Red-Black Tree data structure.
46
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
47
- * following types:
48
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
49
- * key in the node being added to the Red-Black Tree.
50
- * @returns The method returns either a node (`N`) or `undefined`.
66
+ * The function adds a node to a Red-Black Tree data structure.
67
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
68
+ * @returns The method `add` returns either an instance of `N` (the node that was added) or
69
+ * `undefined`.
51
70
  */
52
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
71
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
53
72
  /**
54
73
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
55
74
  * Space Complexity: O(1)
@@ -70,6 +89,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
70
89
  * @returns an array of `BiTreeDeleteResult<N>`.
71
90
  */
72
91
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
92
+ /**
93
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
94
+ * Space Complexity: O(1)
95
+ */
73
96
  isRealNode(node: N | undefined): node is N;
74
97
  getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
75
98
  getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
@@ -101,6 +124,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
101
124
  * @returns the predecessor of the given RedBlackTreeNode 'x'.
102
125
  */
103
126
  getPredecessor(x: N): N;
127
+ /**
128
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
129
+ * Space Complexity: O(1)
130
+ */
104
131
  clear(): void;
105
132
  protected _setRoot(v: N): void;
106
133
  /**
@@ -166,4 +193,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
166
193
  * red-black tree.
167
194
  */
168
195
  protected _fixInsert(k: N): void;
196
+ /**
197
+ * The function replaces an old node with a new node while preserving the color of the old node.
198
+ * @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
199
+ * data structure. It is of type `N`, which is the type of the nodes in the data structure.
200
+ * @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
201
+ * data structure.
202
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
203
+ * superclass, passing in the `oldNode` and `newNode` as arguments.
204
+ */
205
+ protected _replaceNode(oldNode: N, newNode: N): N;
169
206
  }