data-structure-typed 0.9.16 → 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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +96 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,395 +0,0 @@
1
- import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types';
2
- import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree';
3
-
4
- export enum CP {lt = -1, eq = 0, gt = 1}
5
-
6
- export class BSTNode<T> extends BinaryTreeNode<T> {
7
- override clone(): BSTNode<T> {
8
- return new BSTNode<T>(this.id, this.val, this.count);
9
- }
10
- }
11
-
12
- export class BST<T> extends BinaryTree<T> {
13
- constructor(options?: {
14
- comparator?: BSTComparator,
15
- loopType?: LoopType
16
- }) {
17
- super(options);
18
- if (options !== undefined) {
19
- const {comparator} = options;
20
- if (comparator !== undefined) {
21
- this._comparator = comparator;
22
- }
23
- }
24
- }
25
-
26
- override createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null {
27
- return val !== null ? new BSTNode<T>(id, val, count) : null;
28
- }
29
-
30
- override put(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode<T> | null {
31
- let inserted: BSTNode<T> | null = null;
32
- const newNode = this.createNode(id, val, count);
33
- if (this.root === null) {
34
- this.root = newNode;
35
- this.size++;
36
- this.count += newNode?.count ?? 1;
37
- inserted = (this.root);
38
- } else {
39
- let cur = this.root;
40
- let traversing = true;
41
- while (traversing) {
42
- if (cur !== null && newNode !== null) {
43
- if (this._compare(cur.id, id) === CP.eq) {
44
- if (newNode) {
45
- cur.count += newNode.count;
46
- this.count += newNode.count;
47
- cur.val = newNode.val;
48
- }
49
- //Duplicates are not accepted.
50
- traversing = false;
51
- inserted = cur;
52
- } else if (this._compare(cur.id, id) === CP.gt) {
53
- // Traverse left of the node
54
- if (cur.left === undefined) {
55
- if (newNode) {
56
- newNode.parent = cur;
57
- newNode.familyPosition = FamilyPosition.left;
58
- }
59
- //Add to the left of the current node
60
- cur.left = newNode;
61
- this.size++;
62
- this.count += newNode.count;
63
- traversing = false;
64
- inserted = cur.left;
65
- } else {
66
- //Traverse the left of the current node
67
- if (cur.left) cur = cur.left;
68
- }
69
- } else if (this._compare(cur.id, id) === CP.lt) {
70
- // Traverse right of the node
71
- if (cur.right === undefined) {
72
- if (newNode) {
73
- newNode.parent = cur;
74
- newNode.familyPosition = FamilyPosition.right;
75
- }
76
- //Add to the right of the current node
77
- cur.right = newNode;
78
- this.size++;
79
- this.count += newNode.count;
80
- traversing = false;
81
- inserted = (cur.right);
82
- } else {
83
- //Traverse the left of the current node
84
- if (cur.right) cur = cur.right;
85
- }
86
- }
87
- } else {
88
- traversing = false;
89
- }
90
- }
91
- }
92
- return inserted;
93
- }
94
-
95
- override get(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): BSTNode<T> | null {
96
- propertyName = propertyName ?? 'id';
97
- return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
98
- }
99
-
100
- lastKey() {
101
- if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.id ?? 0;
102
- else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.id ?? 0;
103
- else return this.getRightMost()?.id ?? 0;
104
- }
105
-
106
- override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[] {
107
- const bstDeletedResult: BSTDeletedResult<T>[] = [];
108
-
109
- if (!this.root) return bstDeletedResult;
110
-
111
- const curr: BSTNode<T> | null = this.get(id);
112
- if (!curr) return bstDeletedResult;
113
-
114
- const parent: BSTNode<T> | null = curr?.parent ? curr.parent : null;
115
- let needBalanced: BSTNode<T> | null = null, orgCurrent = curr;
116
-
117
- if (curr.count > 1 && !ignoreCount) {
118
- curr.count--;
119
- this.count--;
120
- } else {
121
- if (!curr.left) {
122
- if (!parent) {
123
- if (curr.right !== undefined) this.root = curr.right;
124
- } else {
125
- switch (curr.familyPosition) {
126
- case FamilyPosition.left:
127
- parent.left = curr.right;
128
- break;
129
- case FamilyPosition.right:
130
- parent.right = curr.right;
131
- break;
132
- }
133
- needBalanced = parent;
134
- }
135
- } else {
136
- const leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
137
- if (leftSubTreeMax) {
138
- const parentOfLeftSubTreeMax = leftSubTreeMax.parent;
139
- orgCurrent = curr.swapLocation(leftSubTreeMax);
140
- if (parentOfLeftSubTreeMax) {
141
- if (parentOfLeftSubTreeMax.right === leftSubTreeMax) parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
142
- else parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
143
- needBalanced = parentOfLeftSubTreeMax;
144
- }
145
- }
146
- }
147
- this.size--;
148
- this.count -= curr.count;
149
- }
150
-
151
- bstDeletedResult.push({deleted: orgCurrent, needBalanced});
152
- return bstDeletedResult;
153
- }
154
-
155
- override getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): BSTNode<T>[] {
156
- propertyName = propertyName ?? 'id';
157
- if (!this.root) return [];
158
- const result: BSTNode<T>[] = [];
159
-
160
- if (this._loopType === LoopType.recursive) {
161
- const _traverse = (cur: BSTNode<T>) => {
162
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
163
-
164
- if (!cur.left && !cur.right) return;
165
- if (propertyName === 'id') {
166
- if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
167
- if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
168
- } else {
169
- cur.left && _traverse(cur.left);
170
- cur.right && _traverse(cur.right);
171
- }
172
- }
173
-
174
- _traverse(this.root);
175
- } else {
176
- const queue: BSTNode<T>[] = [this.root];
177
- while (queue.length > 0) {
178
- const cur = queue.shift();
179
- if (cur) {
180
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
181
- if (propertyName === 'id') {
182
- if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
183
- if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
184
- } else {
185
- cur.left && queue.push(cur.left);
186
- cur.right && queue.push(cur.right);
187
- }
188
- }
189
- }
190
- }
191
-
192
- return result;
193
- }
194
-
195
- // --- start additional functions
196
- lesserSum(id: BinaryTreeNodeId, propertyName ?: BinaryTreeNodePropertyName): number {
197
- propertyName = propertyName ?? 'id';
198
- if (!this.root) return 0;
199
-
200
- const getSumByPropertyName = (cur: BSTNode<T>) => {
201
- let needSum: number;
202
- switch (propertyName) {
203
- case 'id':
204
- needSum = cur.id;
205
- break;
206
- case 'count':
207
- needSum = cur.count;
208
- break;
209
- default:
210
- needSum = cur.id;
211
- break;
212
- }
213
- return needSum;
214
- }
215
-
216
- let sum = 0;
217
-
218
- if (this._loopType === LoopType.recursive) {
219
- const _traverse = (cur: BSTNode<T>): void => {
220
- const compared = this._compare(cur.id, id);
221
- if (compared === CP.eq) {
222
- if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
223
- return;
224
- } else if (compared === CP.lt) {
225
- if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
226
- sum += getSumByPropertyName(cur);
227
- if (cur.right) _traverse(cur.right);
228
- else return;
229
- } else {
230
- if (cur.left) _traverse(cur.left);
231
- else return;
232
- }
233
- };
234
-
235
- _traverse(this.root);
236
- } else {
237
- const queue: BSTNode<T>[] = [this.root];
238
- while (queue.length > 0) {
239
- const cur = queue.shift();
240
- if (cur) {
241
- const compared = this._compare(cur.id, id);
242
- if (compared === CP.eq) {
243
- if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
244
- return sum;
245
- } else if (compared === CP.lt) { // todo maybe a bug
246
- if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
247
- sum += getSumByPropertyName(cur);
248
- if (cur.right) queue.push(cur.right);
249
- else return sum;
250
- } else {
251
- if (cur.left) queue.push(cur.left);
252
- else return sum;
253
- }
254
- }
255
- }
256
- }
257
-
258
- return sum;
259
- }
260
-
261
- allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
262
- propertyName = propertyName ?? 'id';
263
- if (!this.root) return false;
264
-
265
- const _sumByPropertyName = (cur: BSTNode<T>) => {
266
- switch (propertyName) {
267
- case 'id':
268
- cur.id += delta;
269
- break;
270
- case 'count':
271
- cur.count += delta;
272
- break;
273
- default:
274
- cur.id += delta;
275
- break;
276
- }
277
- }
278
-
279
- if (this._loopType === LoopType.recursive) {
280
- const _traverse = (cur: BSTNode<T>) => {
281
- const compared = this._compare(cur.id, node.id);
282
- _sumByPropertyName(cur);
283
-
284
- if (!cur.left && !cur.right) return;
285
- if (cur.left && compared === CP.gt) _traverse(cur.left);
286
- else if (cur.right && compared === CP.gt) _traverse(cur.right);
287
- };
288
-
289
- _traverse(this.root);
290
- return true;
291
- } else {
292
- const queue: BSTNode<T>[] = [this.root];
293
- while (queue.length > 0) {
294
- const cur = queue.shift();
295
- if (cur) {
296
- const compared = this._compare(cur.id, node.id);
297
- _sumByPropertyName(cur);
298
-
299
- if (cur.left && compared === CP.gt) queue.push(cur.left);
300
- else if (cur.right && compared === CP.gt) queue.push(cur.right);
301
- }
302
- }
303
- return true;
304
- }
305
- }
306
-
307
- balance(): boolean {
308
- const sorted = this.DFS('in', 'node'), n = sorted.length;
309
- this.clear();
310
-
311
- if (sorted.length < 1) return false;
312
- if (this._loopType === LoopType.recursive) {
313
- const buildBalanceBST = (l: number, r: number) => {
314
- if (l > r) return;
315
- const m = l + Math.floor((r - l) / 2);
316
- const midNode = sorted[m];
317
- this.put(midNode.id, midNode.val, midNode.count);
318
- buildBalanceBST(l, m - 1);
319
- buildBalanceBST(m + 1, r);
320
- };
321
-
322
- buildBalanceBST(0, n - 1);
323
- return true;
324
- } else {
325
- const stack: [[number, number]] = [[0, n - 1]];
326
- while (stack.length > 0) {
327
- const popped = stack.pop();
328
- if (popped) {
329
- const [l, r] = popped;
330
- if (l <= r) {
331
- const m = l + Math.floor((r - l) / 2);
332
- const midNode = sorted[m];
333
- this.put(midNode.id, midNode.val, midNode.count);
334
- stack.push([m + 1, r]);
335
- stack.push([l, m - 1]);
336
- }
337
- }
338
- }
339
- return true;
340
- }
341
- }
342
-
343
- isAVLBalanced(): boolean {
344
- if (!this.root) return true;
345
-
346
- let balanced = true;
347
-
348
- if (this._loopType === LoopType.recursive) {
349
- const _height = (cur: BSTNode<T> | null | undefined): number => {
350
- if (!cur) return 0;
351
- const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
352
- if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
353
- return Math.max(leftHeight, rightHeight) + 1;
354
- };
355
- _height(this.root);
356
- } else {
357
- const stack: BSTNode<T>[] = [];
358
- let node: BSTNode<T> | null | undefined = this.root, last: BSTNode<T> | null = null;
359
- const depths: Map<BSTNode<T>, number> = new Map();
360
-
361
- while (stack.length > 0 || node) {
362
- if (node) {
363
- stack.push(node);
364
- node = node.left;
365
- } else {
366
- node = stack[stack.length - 1]
367
- if (!node.right || last === node.right) {
368
- node = stack.pop();
369
- if (node) {
370
- const left = node.left ? depths.get(node.left) ?? -1 : -1;
371
- const right = node.right ? depths.get(node.right) ?? -1 : -1;
372
- if (Math.abs(left - right) > 1) return false;
373
- depths.set(node, 1 + Math.max(left, right));
374
- last = node;
375
- node = null;
376
- }
377
- } else node = node.right
378
- }
379
- }
380
- }
381
-
382
- return balanced;
383
- }
384
-
385
- protected _comparator: BSTComparator = (a, b) => a - b;
386
-
387
- protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
388
- const compared = this._comparator(a, b);
389
- if (compared > 0) return CP.gt;
390
- else if (compared < 0) return CP.lt;
391
- else return CP.eq;
392
- }
393
-
394
- // --- end additional functions
395
- }
@@ -1,11 +0,0 @@
1
- export * from './binary-tree';
2
- export * from './bst';
3
- export * from './binary-indexed-tree';
4
- export * from './segment-tree';
5
- export * from './avl-tree';
6
- export * from './b-tree';
7
- export * from './rb-tree';
8
- export * from './splay-tree';
9
- export * from './aa-tree';
10
- export * from './tree-multiset';
11
- export * from './two-three-tree';
@@ -1,3 +0,0 @@
1
- export class RBTree {
2
-
3
- }
@@ -1,172 +0,0 @@
1
- import type {SegmentTreeNodeVal} from '../types';
2
-
3
- export class SegmentTreeNode {
4
- constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
5
- this._start = start;
6
- this._end = end;
7
- this._sum = sum;
8
- this._val = val || null;
9
- }
10
-
11
- protected _start = 0;
12
-
13
- get start(): number {
14
- return this._start;
15
- }
16
-
17
- set start(v: number) {
18
- this._start = v;
19
- }
20
-
21
- protected _end = 0;
22
-
23
- get end(): number {
24
- return this._end;
25
- }
26
-
27
- set end(v: number) {
28
- this._end = v;
29
- }
30
-
31
- protected _val: SegmentTreeNodeVal | null = null;
32
-
33
- get val(): SegmentTreeNodeVal | null {
34
- return this._val;
35
- }
36
-
37
- set val(v: SegmentTreeNodeVal | null) {
38
- this._val = v;
39
- }
40
-
41
- protected _sum = 0;
42
-
43
- get sum(): number {
44
- return this._sum;
45
- }
46
-
47
- set sum(v: number) {
48
- this._sum = v;
49
- }
50
-
51
- protected _left: SegmentTreeNode | null = null;
52
-
53
- get left(): SegmentTreeNode | null {
54
- return this._left;
55
- }
56
-
57
- set left(v: SegmentTreeNode | null) {
58
- this._left = v;
59
- }
60
-
61
- protected _right: SegmentTreeNode | null = null;
62
-
63
- get right(): SegmentTreeNode | null {
64
- return this._right;
65
- }
66
-
67
- set right(v: SegmentTreeNode | null) {
68
- this._right = v;
69
- }
70
- }
71
-
72
- export class SegmentTree {
73
- protected _values: number[] = [];
74
- protected _start = 0;
75
- protected _end: number;
76
-
77
- constructor(values: number[], start?: number, end?: number) {
78
- start = start || 0;
79
- end = end || values.length - 1;
80
- this._values = values;
81
- this._start = start;
82
- this._end = end;
83
- this._root = this.build(start, end);
84
- }
85
-
86
- protected _root: SegmentTreeNode | null;
87
-
88
- get root(): SegmentTreeNode | null {
89
- return this._root;
90
- }
91
-
92
- build(start: number, end: number): SegmentTreeNode {
93
- if (start === end) {
94
- return new SegmentTreeNode(start, end, this._values[start]);
95
- }
96
- const mid = start + Math.floor((end - start) / 2);
97
- const left = this.build(start, mid);
98
- const right = this.build(mid + 1, end);
99
- const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
100
- cur.left = left;
101
- cur.right = right;
102
- return cur;
103
- }
104
-
105
- updateNode(index: number, sum: number, val?: SegmentTreeNodeVal) {
106
- const root = this.root || null;
107
- if (!root) {
108
- return;
109
- }
110
- const dfs = (cur: SegmentTreeNode, index: number, sum: number, val?: SegmentTreeNodeVal) => {
111
- if (cur.start === cur.end && cur.start === index) {
112
- cur.sum = sum;
113
- // cur.val = val;
114
- return;
115
- }
116
- const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
117
- if (index <= mid) {
118
- if (cur.left) {
119
- dfs(cur.left, index, sum, val);
120
- }
121
- } else {
122
- if (cur.right) {
123
- dfs(cur.right, index, sum, val);
124
- }
125
- }
126
- if (cur.left && cur.right) {
127
- cur.sum = cur.left.sum + cur.right.sum;
128
- }
129
- };
130
-
131
- dfs(root, index, sum);
132
- }
133
-
134
- querySumByRange(indexA: number, indexB: number): number {
135
- const root = this.root || null;
136
- if (!root) {
137
- return 0;
138
- }
139
-
140
- const dfs = (cur: SegmentTreeNode, i: number, j: number): number => {
141
- if (cur.start === i && cur.end === j) {
142
- return cur.sum;
143
- }
144
- const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
145
- if (j <= mid) {
146
- // TODO after no-non-null-assertion not ensure the logic
147
- if (cur.left) {
148
- return dfs(cur.left, i, j);
149
- } else {
150
- return NaN;
151
- }
152
- } else if (i > mid) {
153
- // TODO after no-non-null-assertion not ensure the logic
154
- if (cur.right) {
155
- // TODO after no-non-null-assertion not ensure the logic
156
- return dfs(cur.right, i, j);
157
-
158
- } else {
159
- return NaN;
160
- }
161
- } else {
162
- // TODO after no-non-null-assertion not ensure the logic
163
- if (cur.left && cur.right) {
164
- return dfs(cur.left, i, mid) + dfs(cur.right, mid + 1, j);
165
- } else {
166
- return NaN;
167
- }
168
- }
169
- };
170
- return dfs(root, indexA, indexB);
171
- }
172
- }
@@ -1,3 +0,0 @@
1
- export class SplayTree {
2
-
3
- }
@@ -1,18 +0,0 @@
1
- import {BST, BSTNode} from './bst';
2
- import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types';
3
-
4
- export class TreeMultiSet<T> extends BST<T> {
5
- override createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T> {
6
- return new BSTNode<T>(id, val, count);
7
- }
8
-
9
- override put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null {
10
- return super.put(id, val, count);
11
- }
12
-
13
- override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): TreeMultiSetDeletedResult<T>[] {
14
- return super.remove(id, isUpdateAllLeftSum);
15
- }
16
- }
17
-
18
-
@@ -1,3 +0,0 @@
1
- export class TwoThreeTree {
2
-
3
- }
@@ -1,7 +0,0 @@
1
- // 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
2
- // 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
3
- // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
4
- // 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
5
- // 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
6
- // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
7
- // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first