data-structure-typed 0.8.18 → 1.3.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 (272) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +690 -2
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
  8. package/dist/data-structures/binary-tree/avl-tree.js +110 -37
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
  12. package/dist/data-structures/binary-tree/binary-tree.js +27 -979
  13. package/dist/data-structures/binary-tree/bst.d.ts +118 -28
  14. package/dist/data-structures/binary-tree/bst.js +162 -124
  15. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  16. package/dist/data-structures/binary-tree/index.js +1 -0
  17. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  18. package/dist/data-structures/binary-tree/rb-tree.js +40 -2
  19. package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
  20. package/dist/data-structures/binary-tree/segment-tree.js +80 -17
  21. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
  22. package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
  23. package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
  24. package/dist/data-structures/graph/abstract-graph.js +365 -92
  25. package/dist/data-structures/graph/directed-graph.d.ts +175 -26
  26. package/dist/data-structures/graph/directed-graph.js +249 -95
  27. package/dist/data-structures/graph/index.d.ts +1 -0
  28. package/dist/data-structures/graph/index.js +1 -0
  29. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  30. package/dist/data-structures/graph/map-graph.js +111 -0
  31. package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
  32. package/dist/data-structures/graph/undirected-graph.js +154 -44
  33. package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
  34. package/dist/data-structures/hash/coordinate-map.js +44 -3
  35. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  36. package/dist/data-structures/hash/coordinate-set.js +34 -0
  37. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  38. package/dist/data-structures/hash/hash-table.js +4 -0
  39. package/dist/data-structures/hash/index.d.ts +5 -0
  40. package/dist/data-structures/hash/index.js +5 -0
  41. package/dist/data-structures/hash/pair.d.ts +2 -1
  42. package/dist/data-structures/hash/pair.js +4 -0
  43. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  44. package/dist/data-structures/hash/tree-map.js +4 -0
  45. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-set.js +4 -0
  47. package/dist/data-structures/heap/heap.d.ts +62 -51
  48. package/dist/data-structures/heap/heap.js +106 -63
  49. package/dist/data-structures/heap/max-heap.d.ts +13 -4
  50. package/dist/data-structures/heap/max-heap.js +10 -2
  51. package/dist/data-structures/heap/min-heap.d.ts +14 -4
  52. package/dist/data-structures/heap/min-heap.js +11 -2
  53. package/dist/data-structures/index.d.ts +1 -0
  54. package/dist/data-structures/index.js +1 -0
  55. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
  56. package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
  57. package/dist/data-structures/linked-list/index.d.ts +1 -0
  58. package/dist/data-structures/linked-list/index.js +1 -0
  59. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
  60. package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
  61. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  62. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  63. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  64. package/dist/data-structures/matrix/matrix.js +14 -0
  65. package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
  66. package/dist/data-structures/matrix/matrix2d.js +91 -8
  67. package/dist/data-structures/matrix/navigator.d.ts +37 -16
  68. package/dist/data-structures/matrix/navigator.js +28 -0
  69. package/dist/data-structures/matrix/vector2d.d.ts +156 -29
  70. package/dist/data-structures/matrix/vector2d.js +184 -55
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
  76. package/dist/data-structures/priority-queue/priority-queue.js +219 -75
  77. package/dist/data-structures/queue/deque.d.ts +141 -13
  78. package/dist/data-structures/queue/deque.js +151 -7
  79. package/dist/data-structures/queue/queue.d.ts +68 -42
  80. package/dist/data-structures/queue/queue.js +95 -51
  81. package/dist/data-structures/stack/stack.d.ts +30 -36
  82. package/dist/data-structures/stack/stack.js +31 -37
  83. package/dist/data-structures/tree/index.d.ts +1 -0
  84. package/dist/data-structures/tree/index.js +17 -0
  85. package/dist/data-structures/tree/tree.d.ts +14 -0
  86. package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
  87. package/dist/data-structures/trie/trie.d.ts +39 -6
  88. package/dist/data-structures/trie/trie.js +81 -12
  89. package/dist/index.d.ts +3 -0
  90. package/dist/index.js +3 -0
  91. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  92. package/dist/interfaces/abstract-binary-tree.js +2 -0
  93. package/dist/interfaces/abstract-graph.d.ts +17 -0
  94. package/dist/interfaces/abstract-graph.js +2 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/avl-tree.js +2 -0
  97. package/dist/interfaces/binary-tree.d.ts +6 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/bst.d.ts +17 -0
  100. package/dist/interfaces/bst.js +2 -0
  101. package/dist/interfaces/directed-graph.d.ts +12 -0
  102. package/dist/interfaces/directed-graph.js +2 -0
  103. package/dist/interfaces/doubly-linked-list.js +2 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +15 -0
  106. package/dist/interfaces/index.js +31 -0
  107. package/dist/interfaces/navigator.js +2 -0
  108. package/dist/interfaces/priority-queue.js +2 -0
  109. package/dist/interfaces/rb-tree.d.ts +8 -0
  110. package/dist/interfaces/rb-tree.js +2 -0
  111. package/dist/interfaces/segment-tree.js +2 -0
  112. package/dist/interfaces/singly-linked-list.js +2 -0
  113. package/dist/interfaces/tree-multiset.d.ts +7 -0
  114. package/dist/interfaces/tree-multiset.js +2 -0
  115. package/dist/interfaces/undirected-graph.d.ts +5 -0
  116. package/dist/interfaces/undirected-graph.js +2 -0
  117. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  118. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  119. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  120. package/dist/types/data-structures/abstract-graph.js +2 -0
  121. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  122. package/dist/types/data-structures/avl-tree.js +2 -0
  123. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  124. package/dist/types/data-structures/binary-tree.js +2 -0
  125. package/dist/types/data-structures/bst.d.ts +13 -0
  126. package/dist/types/data-structures/bst.js +9 -0
  127. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  128. package/dist/types/data-structures/directed-graph.js +9 -0
  129. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  130. package/dist/types/data-structures/heap.d.ts +3 -0
  131. package/dist/types/data-structures/heap.js +2 -0
  132. package/dist/types/data-structures/index.d.ts +13 -7
  133. package/dist/types/data-structures/index.js +31 -0
  134. package/dist/types/data-structures/map-graph.d.ts +1 -0
  135. package/dist/types/data-structures/map-graph.js +2 -0
  136. package/dist/types/data-structures/navigator.d.ts +14 -0
  137. package/dist/types/data-structures/navigator.js +2 -0
  138. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  139. package/dist/types/data-structures/priority-queue.js +2 -0
  140. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  141. package/dist/types/data-structures/rb-tree.js +8 -0
  142. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  143. package/dist/types/data-structures/segment-tree.js +2 -0
  144. package/dist/types/data-structures/singly-linked-list.js +2 -0
  145. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  146. package/dist/types/data-structures/tree-multiset.js +2 -0
  147. package/dist/types/helpers.d.ts +1 -0
  148. package/dist/types/helpers.js +2 -0
  149. package/dist/types/index.d.ts +2 -0
  150. package/dist/types/index.js +2 -0
  151. package/dist/types/utils/index.d.ts +2 -0
  152. package/dist/types/utils/index.js +18 -0
  153. package/dist/types/utils/utils.d.ts +7 -0
  154. package/dist/types/utils/utils.js +2 -0
  155. package/dist/types/utils/validate-type.d.ts +19 -0
  156. package/dist/types/utils/validate-type.js +2 -0
  157. package/dist/utils/index.js +17 -0
  158. package/dist/utils/utils.d.ts +19 -0
  159. package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
  160. package/package.json +106 -55
  161. package/.idea/data-structure-typed.iml +0 -12
  162. package/.idea/modules.xml +0 -8
  163. package/.idea/vcs.xml +0 -6
  164. package/dist/data-structures/trampoline.d.ts +0 -25
  165. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  166. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  167. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  168. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  169. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  170. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  171. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  172. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  173. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  174. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  175. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  176. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  177. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  178. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  179. package/dist/types/data-structures/graph/index.d.ts +0 -3
  180. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  181. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  182. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  185. package/dist/types/data-structures/heap/index.d.ts +0 -3
  186. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  187. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  188. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  189. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  190. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  191. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  192. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  193. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  194. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  195. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  196. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  199. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  200. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  201. package/dist/types/data-structures/queue/index.d.ts +0 -1
  202. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  203. package/dist/types/data-structures/stack/index.d.ts +0 -1
  204. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  205. package/dist/types/data-structures/trampoline.d.ts +0 -25
  206. package/dist/types/data-structures/trie/index.d.ts +0 -1
  207. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  208. package/dist/types/types/utils.d.ts +0 -46
  209. package/dist/types/utils.d.ts +0 -46
  210. package/dist/utils.d.ts +0 -122
  211. package/dist/utils.js +0 -569
  212. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  213. package/src/data-structures/binary-tree/avl-tree.ts +0 -232
  214. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  215. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  216. package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
  217. package/src/data-structures/binary-tree/bst.ts +0 -404
  218. package/src/data-structures/binary-tree/index.ts +0 -11
  219. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  220. package/src/data-structures/binary-tree/segment-tree.ts +0 -164
  221. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  222. package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
  223. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  224. package/src/data-structures/graph/abstract-graph.ts +0 -789
  225. package/src/data-structures/graph/directed-graph.ts +0 -322
  226. package/src/data-structures/graph/index.ts +0 -3
  227. package/src/data-structures/graph/undirected-graph.ts +0 -154
  228. package/src/data-structures/hash/coordinate-map.ts +0 -24
  229. package/src/data-structures/hash/coordinate-set.ts +0 -20
  230. package/src/data-structures/hash/hash-table.ts +0 -1
  231. package/src/data-structures/hash/index.ts +0 -1
  232. package/src/data-structures/heap/heap.ts +0 -136
  233. package/src/data-structures/heap/index.ts +0 -3
  234. package/src/data-structures/heap/max-heap.ts +0 -22
  235. package/src/data-structures/heap/min-heap.ts +0 -24
  236. package/src/data-structures/index.ts +0 -11
  237. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
  238. package/src/data-structures/linked-list/index.ts +0 -2
  239. package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
  240. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  241. package/src/data-structures/matrix/index.ts +0 -4
  242. package/src/data-structures/matrix/matrix.ts +0 -13
  243. package/src/data-structures/matrix/matrix2d.ts +0 -125
  244. package/src/data-structures/matrix/navigator.ts +0 -99
  245. package/src/data-structures/matrix/vector2d.ts +0 -189
  246. package/src/data-structures/priority-queue/index.ts +0 -3
  247. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
  248. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
  249. package/src/data-structures/priority-queue/priority-queue.ts +0 -208
  250. package/src/data-structures/queue/deque.ts +0 -139
  251. package/src/data-structures/queue/index.ts +0 -2
  252. package/src/data-structures/queue/queue.ts +0 -123
  253. package/src/data-structures/stack/index.ts +0 -1
  254. package/src/data-structures/stack/stack.ts +0 -104
  255. package/src/data-structures/trampoline.ts +0 -91
  256. package/src/data-structures/trie/index.ts +0 -1
  257. package/src/data-structures/trie/trie.ts +0 -153
  258. package/src/index.ts +0 -1
  259. package/src/types/index.ts +0 -1
  260. package/src/types/patches/index.d.ts +0 -0
  261. package/src/types/utils.ts +0 -158
  262. package/src/utils.ts +0 -605
  263. package/tsconfig.json +0 -53
  264. /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
  265. /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
  266. /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
  267. /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
  268. /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
  269. /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
  270. /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
  271. /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
  272. /package/dist/{types/types → utils}/index.d.ts +0 -0
@@ -1,3 +1,4 @@
1
+ export * from './abstract-binary-tree';
1
2
  export * from './binary-tree';
2
3
  export * from './bst';
3
4
  export * from './binary-indexed-tree';
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./abstract-binary-tree"), exports);
17
18
  __exportStar(require("./binary-tree"), exports);
18
19
  __exportStar(require("./bst"), exports);
19
20
  __exportStar(require("./binary-indexed-tree"), exports);
@@ -1,2 +1,19 @@
1
- export declare class RBTree {
1
+ import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
+ import { IRBTree, IRBTreeNode } from '../../interfaces/rb-tree';
3
+ import { BST, BSTNode } from './bst';
4
+ export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
5
+ constructor(id: BinaryTreeNodeId, val?: T, color?: RBColor);
6
+ private _color;
7
+ get color(): RBColor;
8
+ set color(value: RBColor);
9
+ }
10
+ export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
11
+ constructor(options?: RBTreeOptions);
12
+ createNode(id: BinaryTreeNodeId, val?: N['val']): N;
13
+ insert(id: number, val?: N | null): void;
14
+ private leftRotate;
15
+ private rightRotate;
16
+ private insertFixup;
17
+ private deleteFixup;
18
+ private transplant;
2
19
  }
@@ -1,6 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RBTree = void 0;
4
- class RBTree {
3
+ exports.RBTree = exports.RBTreeNode = void 0;
4
+ const types_1 = require("../../types");
5
+ const bst_1 = require("./bst");
6
+ class RBTreeNode extends bst_1.BSTNode {
7
+ constructor(id, val, color = types_1.RBColor.RED) {
8
+ super(id, val);
9
+ this._color = color;
10
+ }
11
+ get color() {
12
+ return this._color;
13
+ }
14
+ set color(value) {
15
+ this._color = value;
16
+ }
17
+ }
18
+ exports.RBTreeNode = RBTreeNode;
19
+ class RBTree extends bst_1.BST {
20
+ constructor(options) {
21
+ super(options);
22
+ }
23
+ createNode(id, val) {
24
+ return new RBTreeNode(id, val, types_1.RBColor.RED);
25
+ }
26
+ // private override _root: BinaryTreeNode<N> | null = null;
27
+ //
28
+ // override get root(): BinaryTreeNode<N> | null {
29
+ // return this._root;
30
+ // }
31
+ insert(id, val) {
32
+ }
33
+ leftRotate(node) {
34
+ }
35
+ rightRotate(node) {
36
+ }
37
+ insertFixup(node) {
38
+ }
39
+ deleteFixup(node) {
40
+ }
41
+ transplant(u, v) {
42
+ }
5
43
  }
6
44
  exports.RBTree = RBTree;
@@ -1,33 +1,83 @@
1
- export type SegmentTreeNodeVal = number;
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { SegmentTreeNodeVal } from '../../types';
2
9
  export declare class SegmentTreeNode {
3
- protected _start: number;
10
+ constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
11
+ private _start;
4
12
  get start(): number;
5
13
  set start(v: number);
6
- protected _end: number;
14
+ private _end;
7
15
  get end(): number;
8
16
  set end(v: number);
9
- protected _val: SegmentTreeNodeVal | null;
17
+ private _val;
10
18
  get val(): SegmentTreeNodeVal | null;
11
19
  set val(v: SegmentTreeNodeVal | null);
12
- protected _sum: number;
20
+ private _sum;
13
21
  get sum(): number;
14
22
  set sum(v: number);
15
- protected _left: SegmentTreeNode | null;
23
+ private _left;
16
24
  get left(): SegmentTreeNode | null;
17
25
  set left(v: SegmentTreeNode | null);
18
- protected _right: SegmentTreeNode | null;
26
+ private _right;
19
27
  get right(): SegmentTreeNode | null;
20
28
  set right(v: SegmentTreeNode | null);
21
- constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
22
29
  }
23
30
  export declare class SegmentTree {
24
- protected _values: number[];
25
- protected _start: number;
26
- protected _end: number;
27
- protected _root: SegmentTreeNode | null;
28
- get root(): SegmentTreeNode | null;
31
+ /**
32
+ * The constructor initializes the values, start, end, and root properties of an object.
33
+ * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
34
+ * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
35
+ * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
36
+ * the beginning of the array.
37
+ * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
38
+ * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
39
+ */
29
40
  constructor(values: number[], start?: number, end?: number);
41
+ private _values;
42
+ get values(): number[];
43
+ private _start;
44
+ get start(): number;
45
+ private _end;
46
+ get end(): number;
47
+ private _root;
48
+ get root(): SegmentTreeNode | null;
49
+ /**
50
+ * The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
51
+ * for each segment.
52
+ * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
53
+ * building the segment tree.
54
+ * @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
55
+ * building the segment tree.
56
+ * @returns a SegmentTreeNode object.
57
+ */
30
58
  build(start: number, end: number): SegmentTreeNode;
59
+ /**
60
+ * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
61
+ * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
62
+ * updated.
63
+ * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
64
+ * the `SegmentTreeNode` at the specified `index`.
65
+ * @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
66
+ * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
67
+ * cur.val = val;` and pass a value for `val` in the
68
+ * @returns The function does not return anything.
69
+ */
31
70
  updateNode(index: number, sum: number, val?: SegmentTreeNodeVal): void;
71
+ /**
72
+ * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
73
+ * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
74
+ * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
75
+ * calculate the sum.
76
+ * @returns The function `querySumByRange` returns a number.
77
+ */
32
78
  querySumByRange(indexA: number, indexB: number): number;
79
+ protected _setValues(value: number[]): void;
80
+ protected _setStart(value: number): void;
81
+ protected _setEnd(value: number): void;
82
+ protected _setRoot(v: SegmentTreeNode | null): void;
33
83
  }
@@ -1,7 +1,26 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.SegmentTree = exports.SegmentTreeNode = void 0;
4
11
  class SegmentTreeNode {
12
+ constructor(start, end, sum, val) {
13
+ this._start = 0;
14
+ this._end = 0;
15
+ this._val = null;
16
+ this._sum = 0;
17
+ this._left = null;
18
+ this._right = null;
19
+ this._start = start;
20
+ this._end = end;
21
+ this._sum = sum;
22
+ this._val = val || null;
23
+ }
5
24
  get start() {
6
25
  return this._start;
7
26
  }
@@ -38,24 +57,18 @@ class SegmentTreeNode {
38
57
  set right(v) {
39
58
  this._right = v;
40
59
  }
41
- constructor(start, end, sum, val) {
42
- this._start = 0;
43
- this._end = 0;
44
- this._val = null;
45
- this._sum = 0;
46
- this._left = null;
47
- this._right = null;
48
- this._start = start;
49
- this._end = end;
50
- this._sum = sum;
51
- this._val = val || null;
52
- }
53
60
  }
54
61
  exports.SegmentTreeNode = SegmentTreeNode;
55
62
  class SegmentTree {
56
- get root() {
57
- return this._root;
58
- }
63
+ /**
64
+ * The constructor initializes the values, start, end, and root properties of an object.
65
+ * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
66
+ * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
67
+ * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
68
+ * the beginning of the array.
69
+ * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
70
+ * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
71
+ */
59
72
  constructor(values, start, end) {
60
73
  this._values = [];
61
74
  this._start = 0;
@@ -66,10 +79,30 @@ class SegmentTree {
66
79
  this._end = end;
67
80
  this._root = this.build(start, end);
68
81
  }
82
+ get values() {
83
+ return this._values;
84
+ }
85
+ get start() {
86
+ return this._start;
87
+ }
88
+ get end() {
89
+ return this._end;
90
+ }
91
+ get root() {
92
+ return this._root;
93
+ }
94
+ /**
95
+ * The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
96
+ * for each segment.
97
+ * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
98
+ * building the segment tree.
99
+ * @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
100
+ * building the segment tree.
101
+ * @returns a SegmentTreeNode object.
102
+ */
69
103
  build(start, end) {
70
- if (start === end) {
104
+ if (start === end)
71
105
  return new SegmentTreeNode(start, end, this._values[start]);
72
- }
73
106
  const mid = start + Math.floor((end - start) / 2);
74
107
  const left = this.build(start, mid);
75
108
  const right = this.build(mid + 1, end);
@@ -78,6 +111,17 @@ class SegmentTree {
78
111
  cur.right = right;
79
112
  return cur;
80
113
  }
114
+ /**
115
+ * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
116
+ * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
117
+ * updated.
118
+ * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
119
+ * the `SegmentTreeNode` at the specified `index`.
120
+ * @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
121
+ * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
122
+ * cur.val = val;` and pass a value for `val` in the
123
+ * @returns The function does not return anything.
124
+ */
81
125
  updateNode(index, sum, val) {
82
126
  const root = this.root || null;
83
127
  if (!root) {
@@ -106,6 +150,13 @@ class SegmentTree {
106
150
  };
107
151
  dfs(root, index, sum);
108
152
  }
153
+ /**
154
+ * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
155
+ * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
156
+ * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
157
+ * calculate the sum.
158
+ * @returns The function `querySumByRange` returns a number.
159
+ */
109
160
  querySumByRange(indexA, indexB) {
110
161
  const root = this.root || null;
111
162
  if (!root) {
@@ -147,5 +198,17 @@ class SegmentTree {
147
198
  };
148
199
  return dfs(root, indexA, indexB);
149
200
  }
201
+ _setValues(value) {
202
+ this._values = value;
203
+ }
204
+ _setStart(value) {
205
+ this._start = value;
206
+ }
207
+ _setEnd(value) {
208
+ this._end = value;
209
+ }
210
+ _setRoot(v) {
211
+ this._root = v;
212
+ }
150
213
  }
151
214
  exports.SegmentTree = SegmentTree;
@@ -1,11 +1,214 @@
1
- import { BST, BSTNode } from './bst';
2
- import { BinaryTreeNodeId } from './binary-tree';
3
- export type TreeMultiSetDeletedResult<T> = {
4
- deleted: BSTNode<T> | null;
5
- needBalanced: BSTNode<T> | null;
6
- };
7
- export declare class TreeMultiSet<T> extends BST<T> {
8
- createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T>;
9
- put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
10
- remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): TreeMultiSetDeletedResult<T>[];
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { BinaryTreeNodeId, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
9
+ import { BinaryTreeDeletedResult, DFSOrderPattern, NodeOrPropertyName } from '../../types';
10
+ import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
11
+ import { AVLTree, AVLTreeNode } from './avl-tree';
12
+ export declare class TreeMultisetNode<T = any, NEIGHBOR extends TreeMultisetNode<T, NEIGHBOR> = TreeMultisetNodeNested<T>> extends AVLTreeNode<T, NEIGHBOR> implements ITreeMultisetNode<T, NEIGHBOR> {
13
+ /**
14
+ * The constructor function initializes a BinaryTreeNode object with an id, value, and count.
15
+ * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
16
+ * of the binary tree node.
17
+ * @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
18
+ * tree node. If no value is provided, it will be `undefined`.
19
+ * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
20
+ * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
21
+ * parameter when creating a new instance of the `BinaryTreeNode` class,
22
+ */
23
+ constructor(id: BinaryTreeNodeId, val?: T, count?: number);
24
+ private _count;
25
+ get count(): number;
26
+ set count(v: number);
27
+ }
28
+ /**
29
+ * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
30
+ */
31
+ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements ITreeMultiset<N> {
32
+ /**
33
+ * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
34
+ * merge duplicated values.
35
+ * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
36
+ * TreeMultiset.
37
+ */
38
+ constructor(options?: TreeMultisetOptions);
39
+ private _count;
40
+ get count(): number;
41
+ /**
42
+ * The function creates a new BSTNode with the given id, value, and count.
43
+ * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
44
+ * distinguish one node from another in the tree.
45
+ * @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
46
+ * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
47
+ * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
48
+ * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
49
+ */
50
+ createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
51
+ /**
52
+ * The function swaps the location of two nodes in a tree data structure.
53
+ * @param {N} srcNode - The source node that we want to swap with the destination node.
54
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
55
+ * be swapped with.
56
+ * @returns the `destNode` after swapping its values with the `srcNode`.
57
+ */
58
+ swapLocation(srcNode: N, destNode: N): N;
59
+ /**
60
+ * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
61
+ * necessary.
62
+ * @param {BinaryTreeNodeId | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N` (which
63
+ * represents a `BinaryTreeNode`).
64
+ * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
65
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
66
+ * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
67
+ * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
68
+ */
69
+ add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined;
70
+ /**
71
+ * The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
72
+ * node.
73
+ * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
74
+ * be either a node object (`N`) or `null`.
75
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
76
+ * child.
77
+ * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
78
+ */
79
+ _addTo(newNode: N | null, parent: N): N | null | undefined;
80
+ /**
81
+ * The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes.
82
+ * @param {BinaryTreeNodeId[] | N[]} idsOrNodes - An array of BinaryTreeNodeId objects or N objects. These objects
83
+ * represent the IDs or nodes of the binary tree where the values will be added.
84
+ * @param {N['val'][]} [data] - Optional array of values to be associated with each node being added. If provided, the
85
+ * length of the `data` array should be equal to the length of the `idsOrNodes` array.
86
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
87
+ */
88
+ addMany(idsOrNodes: (BinaryTreeNodeId | N)[], data?: N['val'][]): (N | null | undefined)[];
89
+ /**
90
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
91
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
92
+ * @returns The function `perfectlyBalance()` returns a boolean value.
93
+ */
94
+ perfectlyBalance(): boolean;
95
+ /**
96
+ * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
97
+ * node that needs to be balanced.
98
+ * @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
99
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
100
+ * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
101
+ * not be taken into account when removing it. If `ignoreCount` is set to `false
102
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
+ */
104
+ remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
+ /**
106
+ * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
107
+ * recursive or iterative traversal.
108
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
109
+ * binary tree.
110
+ * @returns The function `getSubTreeCount` returns an array `[number, number]`.
111
+ */
112
+ getSubTreeCount(subTreeRoot: N | null | undefined): [number, number];
113
+ /**
114
+ * The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
115
+ * recursively or iteratively.
116
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
117
+ * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree) or
118
+ * `null` if the subtree is empty.
119
+ * @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
120
+ */
121
+ subTreeSumCount(subTreeRoot: N | BinaryTreeNodeId | null): number;
122
+ /**
123
+ * The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
124
+ * the `count` property of each node.
125
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
126
+ * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree), a
127
+ * `BinaryTreeNode` object, or `null` if the subtree is empty.
128
+ * @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
129
+ * in the subtree should be increased or decreased.
130
+ * @returns a boolean value.
131
+ */
132
+ subTreeAddCount(subTreeRoot: N | BinaryTreeNodeId | null, delta: number): boolean;
133
+ /**
134
+ * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
135
+ * using a queue.
136
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
137
+ * `N`. It represents the property of the nodes that you want to search for.
138
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
139
+ * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
140
+ * to `true`, the function will return only one node. If `onlyOne`
141
+ * @returns an array of nodes that match the given nodeProperty.
142
+ */
143
+ getNodesByCount(nodeProperty: BinaryTreeNodeId | N, onlyOne?: boolean): N[];
144
+ /**
145
+ * The BFSCount function returns an array of counts from a breadth-first search of nodes.
146
+ * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
147
+ * BFS traversal.
148
+ */
149
+ BFSCount(): number[];
150
+ /**
151
+ * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
152
+ * count property of each node at that level.
153
+ * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
154
+ * the class `N` or `null`.
155
+ * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
156
+ * array represents the count property of a node in that level.
157
+ */
158
+ listLevelsCount(node: N | null): number[][];
159
+ /**
160
+ * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
161
+ * pattern.
162
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
163
+ * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
164
+ * @returns The function `morrisCount` returns an array of numbers.
165
+ */
166
+ morrisCount(pattern?: 'in' | 'pre' | 'post'): number[];
167
+ /**
168
+ * The function DFSIterativeCount performs a depth-first search iteratively and returns an array of count values for
169
+ * each node.
170
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is a string that specifies the traversal order
171
+ * for the Depth-First Search (DFS) algorithm. It can have one of three values: 'in', 'pre', or 'post'.
172
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
173
+ * specifies whether to return the nodes or the property names during the depth-first search traversal. If it is set to
174
+ * `'node'`, the function will return the nodes. If it is set to `'property'`, the function will return the property
175
+ * @returns The DFSIterativeCount method returns an array of numbers.
176
+ */
177
+ DFSIterativeCount(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): number[];
178
+ /**
179
+ * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
180
+ * @param {DFSOrderPattern} [pattern] - The `pattern` parameter is an optional parameter that specifies the order in
181
+ * which the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
182
+ * @param [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is used to specify whether you want to retrieve the
183
+ * nodes themselves or a specific property of the nodes. If you pass `'count'` as the value for `nodeOrPropertyName`,
184
+ * the function will return an array of the `count` property of each node.
185
+ * @returns The DFSCount method returns an array of numbers representing the count property of each node in the DFS
186
+ * traversal.
187
+ */
188
+ DFSCount(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
189
+ /**
190
+ * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
191
+ * value than a given node.
192
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
193
+ * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
194
+ */
195
+ lesserSumCount(beginNode: N | BinaryTreeNodeId | null): number;
196
+ /**
197
+ * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
198
+ * greater than a given ID by a specified delta value.
199
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be one of the following:
200
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
201
+ * of each node should be increased.
202
+ * @returns a boolean value.
203
+ */
204
+ allGreaterNodesAddCount(node: N | BinaryTreeNodeId | null, delta: number): boolean;
205
+ /**
206
+ * The clear() function clears the data and sets the count to 0.
207
+ */
208
+ clear(): void;
209
+ /**
210
+ * The function "_setCount" is used to set the value of the "_count" property.
211
+ * @param {number} v - number
212
+ */
213
+ protected _setCount(v: number): void;
11
214
  }