binary-tree-typed 2.4.4 → 2.5.0

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 (85) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +965 -420
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +962 -417
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +965 -421
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +962 -418
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/binary-tree-typed.js +959 -414
  46. package/dist/umd/binary-tree-typed.js.map +1 -1
  47. package/dist/umd/binary-tree-typed.min.js +3 -3
  48. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -5,156 +5,222 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { SegmentTreeNodeVal } from '../../types';
9
- export declare class SegmentTreeNode {
10
- /**
11
- * The constructor initializes the properties of a SegmentTreeNode object.
12
- * @param {number} start - The `start` parameter represents the starting index of the segment covered
13
- * by this node in a segment tree.
14
- * @param {number} end - The `end` parameter represents the end index of the segment covered by this
15
- * node in a segment tree.
16
- * @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
17
- * the segment tree node.
18
- * @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
19
- * of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
20
- */
21
- constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
22
- protected _start: number;
23
- /**
24
- * The function returns the value of the protected variable _start.
25
- * @returns The start value, which is of type number.
26
- */
27
- get start(): number;
28
- /**
29
- * The above function sets the value of the "start" property.
30
- * @param {number} value - The value parameter is of type number.
31
- */
32
- set start(value: number);
33
- protected _end: number;
34
- /**
35
- * The function returns the value of the protected variable `_end`.
36
- * @returns The value of the protected property `_end`.
37
- */
38
- get end(): number;
39
- /**
40
- * The above function sets the value of the "end" property.
41
- * @param {number} value - The value parameter is a number that represents the new value for the end
42
- * property.
43
- */
44
- set end(value: number);
45
- protected _value: SegmentTreeNodeVal | undefined;
46
- /**
47
- * The function returns the value of a segment tree node.
48
- * @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
49
- */
50
- get value(): SegmentTreeNodeVal | undefined;
51
- /**
52
- * The function sets the value of a segment tree node.
53
- * @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
54
- * `SegmentTreeNodeVal` or `undefined`.
55
- */
56
- set value(value: SegmentTreeNodeVal | undefined);
57
- protected _sum: number;
58
- /**
59
- * The function returns the value of the sum property.
60
- * @returns The method is returning the value of the variable `_sum`.
61
- */
62
- get sum(): number;
63
- /**
64
- * The above function sets the value of the sum property.
65
- * @param {number} value - The parameter "value" is of type "number".
66
- */
67
- set sum(value: number);
68
- protected _left: SegmentTreeNode | undefined;
69
- /**
70
- * The function returns the left child of a segment tree node.
71
- * @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
72
- * `SegmentTreeNode` or `undefined`.
73
- */
74
- get left(): SegmentTreeNode | undefined;
75
- /**
76
- * The function sets the value of the left property of a SegmentTreeNode object.
77
- * @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
78
- * undefined.
79
- */
80
- set left(value: SegmentTreeNode | undefined);
81
- protected _right: SegmentTreeNode | undefined;
82
- /**
83
- * The function returns the right child of a segment tree node.
84
- * @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
85
- */
86
- get right(): SegmentTreeNode | undefined;
87
- /**
88
- * The function sets the right child of a segment tree node.
89
- * @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
90
- * undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
91
- * value.
92
- */
93
- set right(value: SegmentTreeNode | undefined);
94
- }
95
- export declare class SegmentTree {
96
- /**
97
- * The constructor initializes the values, start, end, and root properties of an object.
98
- * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
99
- * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
100
- * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
101
- * the beginning of the array.
102
- * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
103
- * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
104
- */
105
- constructor(values: number[], start?: number, end?: number);
106
- protected _values: number[];
107
- /**
108
- * The function returns an array of numbers.
109
- * @returns An array of numbers is being returned.
110
- */
111
- get values(): number[];
112
- protected _start: number;
113
- /**
114
- * The function returns the value of the protected variable _start.
115
- * @returns The start value, which is of type number.
116
- */
117
- get start(): number;
118
- protected _end: number;
119
- /**
120
- * The function returns the value of the protected variable `_end`.
121
- * @returns The value of the protected property `_end`.
122
- */
123
- get end(): number;
124
- protected _root: SegmentTreeNode | undefined;
125
- /**
126
- * The function returns the root node of a segment tree.
127
- * @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
128
- */
129
- get root(): SegmentTreeNode | undefined;
130
- /**
131
- * The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
132
- * the sum of values to each segment.
133
- * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
134
- * building the segment tree.
135
- * @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
136
- * build a segment tree.
137
- * @returns a SegmentTreeNode object.
138
- */
139
- build(start: number, end: number): SegmentTreeNode;
140
- /**
141
- * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
142
- * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
143
- * updated.
144
- * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
145
- * the `SegmentTreeNode` at the specified `index`.
146
- * @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
147
- * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
148
- * cur.value = value;` and pass a value for `value` in the
149
- * @returns The function does not return anything.
150
- */
151
- updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
152
- /**
153
- * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
154
- * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
155
- * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
156
- * calculate the sum.
157
- * @returns The function `querySumByRange` returns a number.
158
- */
159
- querySumByRange(indexA: number, indexB: number): number;
8
+ export type SegmentTreeOptions<E> = {
9
+ merger: (a: E, b: E) => E;
10
+ identity: E;
11
+ };
12
+ /**
13
+ * Generic Segment Tree with flat array internals.
14
+ *
15
+ * Supports any associative merge operation (sum, min, max, gcd, etc.).
16
+ * Reference: AtCoder Library segtree<S, op, e>.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const sumTree = SegmentTree.sum([1, 2, 3, 4, 5]);
21
+ * sumTree.query(1, 3); // 9 (2+3+4)
22
+ * sumTree.update(2, 10); // [1, 2, 10, 4, 5]
23
+ * sumTree.query(1, 3); // 16 (2+10+4)
24
+ *
25
+ * const minTree = SegmentTree.min([5, 2, 8, 1, 9]);
26
+ * minTree.query(0, 4); // 1
27
+ * ```
28
+ */
29
+ export declare class SegmentTree<E = number> implements Iterable<E> {
30
+ protected readonly _merger: (a: E, b: E) => E;
31
+ protected readonly _identity: E;
32
+ protected _n: number;
33
+ protected _tree: E[];
34
+ protected _treeSize: number;
35
+ constructor(elements: E[], options: SegmentTreeOptions<E>);
36
+ /**
37
+ * Create a sum segment tree.
38
+ * @example
39
+ * ```ts
40
+ * const st = SegmentTree.sum([1, 2, 3, 4, 5]);
41
+ * st.query(0, 2); // 6 (1+2+3)
42
+ * st.update(1, 10);
43
+ * st.query(0, 2); // 14 (1+10+3)
44
+ * ```
45
+ */
46
+ static sum(elements: number[]): SegmentTree<number>;
47
+ /**
48
+ * Create a min segment tree.
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ * @example
61
+ * // Temperature monitoring with range queries
62
+ * // Hourly temperatures for a day (24 readings)
63
+ * const temps = [18, 17, 16, 15, 16, 18, 21, 24, 27, 29, 31, 32, 33, 32, 31, 29, 27, 25, 23, 21, 20, 19, 18, 17];
64
+ * const tree = SegmentTree.sum(temps);
65
+ *
66
+ * // Average temperature during work hours (9-17)
67
+ * const workSum = tree.query(9, 17);
68
+ * console.log(workSum / 9); // toBeCloseTo;
69
+ *
70
+ * // Sum of morning temps (6-11)
71
+ * console.log(tree.query(6, 11)); // 164;
72
+ */
73
+ static min(elements: number[]): SegmentTree<number>;
74
+ /**
75
+ * Create a max segment tree.
76
+ * @example
77
+ * ```ts
78
+ * const st = SegmentTree.max([3, 1, 4, 1, 5]);
79
+ * st.query(1, 4); // 5
80
+ * ```
81
+ */
82
+ static max(elements: number[]): SegmentTree<number>;
83
+ /**
84
+ * Point update: set element at index to value.
85
+ * Time: O(log n)
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+ * @example
98
+ * // Dynamic range sum with updates
99
+ * // Monthly revenue data (in thousands)
100
+ * const revenue = [120, 95, 140, 110, 85, 130, 150, 100, 160, 125, 90, 175];
101
+ * const tree = SegmentTree.sum(revenue);
102
+ *
103
+ * // Q1 revenue (Jan-Mar)
104
+ * console.log(tree.query(0, 2)); // 355;
105
+ *
106
+ * // Update March revenue from 140 to 200
107
+ * tree.update(2, 200);
108
+ *
109
+ * // Q1 revenue after update
110
+ * console.log(tree.query(0, 2)); // 415;
111
+ *
112
+ * // H1 revenue (Jan-Jun)
113
+ * console.log(tree.query(0, 5)); // 740;
114
+ */
115
+ update(index: number, value: E): void;
116
+ /**
117
+ * Range query: returns merger result over [start, end] (inclusive).
118
+ * Time: O(log n)
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+ * @example
131
+ * // Range sum query on an array
132
+ * const tree = SegmentTree.sum([1, 3, 5, 7, 9, 11]);
133
+ *
134
+ * // Query sum of range [1, 3] → 3 + 5 + 7 = 15
135
+ * console.log(tree.query(1, 3)); // 15;
136
+ *
137
+ * // Query entire range
138
+ * console.log(tree.query(0, 5)); // 36;
139
+ *
140
+ * // Query single element
141
+ * console.log(tree.query(2, 2)); // 5;
142
+ */
143
+ query(start: number, end: number): E;
144
+ /**
145
+ * Get element at index.
146
+ * Time: O(1)
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+ * @example
159
+ * // Point access on segment tree
160
+ * const st = SegmentTree.sum([10, 20, 30, 40]);
161
+ * console.log(st.get(0)); // 10;
162
+ * console.log(st.get(2)); // 30;
163
+ */
164
+ get(index: number): E;
165
+ /**
166
+ * Find the largest r such that predicate(query(left, r)) is true.
167
+ * Returns left-1 if predicate(identity) is false.
168
+ * Returns n-1 if predicate holds for the entire range [left, n-1].
169
+ * Time: O(log n)
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ * @example
182
+ * // Find rightmost position where predicate holds
183
+ * // Prefix sums: find the rightmost index where prefix sum < 10
184
+ * const st = SegmentTree.sum([3, 1, 4, 1, 5]);
185
+ * // maxRight(0, sum => sum < 10) — prefix [3,4,8,9,14]
186
+ * // sum < 10 holds through index 3 (prefix=9), fails at 4 (prefix=14)
187
+ * const result = st.maxRight(0, sum => sum < 10);
188
+ * console.log(result); // 3;
189
+ */
190
+ maxRight(left: number, predicate: (segValue: E) => boolean): number;
191
+ /**
192
+ * Find the smallest l such that predicate(query(l, right)) is true.
193
+ * Returns right+1 if predicate(identity) is false.
194
+ * Returns 0 if predicate holds for the entire range [0, right].
195
+ * Time: O(log n)
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+ * @example
208
+ * // Find leftmost position where predicate holds
209
+ * const st = SegmentTree.sum([3, 1, 4, 1, 5]);
210
+ * // minLeft(5, sum => sum < 7) — suffix sums from right
211
+ * // From right: [5]=5 < 7, [1,5]=6 < 7, [4,1,5]=10 ≥ 7
212
+ * const result = st.minLeft(5, sum => sum < 7);
213
+ * console.log(result); // 3;
214
+ */
215
+ minLeft(right: number, predicate: (segValue: E) => boolean): number;
216
+ get size(): number;
217
+ isEmpty(): boolean;
218
+ clone(): SegmentTree<E>;
219
+ toArray(): E[];
220
+ /**
221
+ * Iterates over leaf values in index order.
222
+ */
223
+ [Symbol.iterator](): IterableIterator<E>;
224
+ forEach(callback: (value: E, index: number) => void): void;
225
+ print(): void;
160
226
  }