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,32 +1,122 @@
1
- import { BinaryTree, BinaryTreeNode, BinaryTreeNodeId, BinaryTreeNodePropertyName, LoopType } from './binary-tree';
2
- export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
3
- export type BSTDeletedResult<T> = {
4
- deleted: BSTNode<T> | null;
5
- needBalanced: BSTNode<T> | null;
6
- };
7
- export declare enum CP {
8
- lt = -1,
9
- eq = 0,
10
- gt = 1
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, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions } from '../../types';
9
+ import { CP } from '../../types';
10
+ import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
+ import { IBST, IBSTNode } from '../../interfaces';
12
+ export declare class BSTNode<T = any, NEIGHBOR extends BSTNode<T, NEIGHBOR> = BSTNodeNested<T>> extends BinaryTreeNode<T, NEIGHBOR> implements IBSTNode<T, NEIGHBOR> {
13
+ constructor(id: BinaryTreeNodeId, val?: T);
11
14
  }
12
- export declare class BSTNode<T> extends BinaryTreeNode<T> {
13
- clone(): BSTNode<T>;
14
- }
15
- export declare class BST<T> extends BinaryTree<T> {
15
+ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
16
+ /**
17
+ * The constructor function initializes a binary search tree object with an optional comparator function.
18
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
19
+ */
20
+ constructor(options?: BSTOptions);
21
+ /**
22
+ * The function creates a new binary search tree node with the given id and value.
23
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
24
+ * identify each node in the binary tree.
25
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
26
+ * that will be stored in the node.
27
+ * @returns a new instance of the BSTNode class with the specified id and value.
28
+ */
29
+ createNode(id: BinaryTreeNodeId, val?: N['val']): N;
30
+ /**
31
+ * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
32
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
33
+ * is of type `BinaryTreeNodeId`.
34
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
35
+ * the value associated with the node.
36
+ * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
37
+ * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
38
+ */
39
+ add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
40
+ /**
41
+ * The function returns the first node in a binary tree that matches the given property name and value.
42
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
43
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
44
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
45
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
46
+ * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
47
+ */
48
+ get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null;
49
+ /**
50
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
51
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
52
+ * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
53
+ * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
54
+ * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
55
+ */
56
+ lastKey(): BinaryTreeNodeId;
57
+ /**
58
+ * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
59
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
60
+ * `N` type. It represents the property of the binary tree node that you want to compare with.
61
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
62
+ * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
63
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
64
+ * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
65
+ * is set to `true`, the function will return an array with only one node (if
66
+ * @returns an array of nodes (type N).
67
+ */
68
+ getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
69
+ /**
70
+ * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
71
+ * less than a given node.
72
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
73
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
74
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
75
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
76
+ * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
77
+ */
78
+ lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number;
79
+ /**
80
+ * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
81
+ * have a greater value than a given node.
82
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
83
+ * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
84
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
85
+ * each greater node should be increased.
86
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
87
+ * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
88
+ * 'id'.
89
+ * @returns a boolean value.
90
+ */
91
+ allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
92
+ /**
93
+ * Balancing Adjustment:
94
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
95
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
96
+ *
97
+ * Use Cases and Efficiency:
98
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
99
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
100
+ */
101
+ /**
102
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
103
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
104
+ * @returns The function `perfectlyBalance()` returns a boolean value.
105
+ */
106
+ perfectlyBalance(): boolean;
107
+ /**
108
+ * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
109
+ * @returns a boolean value.
110
+ */
111
+ isAVLBalanced(): boolean;
16
112
  protected _comparator: BSTComparator;
113
+ /**
114
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
115
+ * greater than, less than, or equal to the second ID.
116
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
117
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
118
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
119
+ * than), or CP.eq (equal).
120
+ */
17
121
  protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
18
- constructor(options?: {
19
- comparator?: BSTComparator;
20
- loopType?: LoopType;
21
- });
22
- createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
23
- put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
24
- get(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): BSTNode<T> | null;
25
- lastKey(): number;
26
- remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[];
27
- getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): BSTNode<T>[];
28
- lesserSum(id: BinaryTreeNodeId, propertyName?: BinaryTreeNodePropertyName): number;
29
- allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
30
- balance(): boolean;
31
- isAVLBalanced(): boolean;
32
122
  }
@@ -1,29 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BST = exports.BSTNode = exports.CP = void 0;
3
+ exports.BST = exports.BSTNode = void 0;
4
+ const types_1 = require("../../types");
4
5
  const binary_tree_1 = require("./binary-tree");
5
- var CP;
6
- (function (CP) {
7
- CP[CP["lt"] = -1] = "lt";
8
- CP[CP["eq"] = 0] = "eq";
9
- CP[CP["gt"] = 1] = "gt";
10
- })(CP = exports.CP || (exports.CP = {}));
11
6
  class BSTNode extends binary_tree_1.BinaryTreeNode {
12
- clone() {
13
- return new BSTNode(this.id, this.val, this.count);
7
+ constructor(id, val) {
8
+ super(id, val);
14
9
  }
15
10
  }
16
11
  exports.BSTNode = BSTNode;
17
12
  class BST extends binary_tree_1.BinaryTree {
18
- _compare(a, b) {
19
- const compared = this._comparator(a, b);
20
- if (compared > 0)
21
- return CP.gt;
22
- else if (compared < 0)
23
- return CP.lt;
24
- else
25
- return CP.eq;
26
- }
13
+ /**
14
+ * The constructor function initializes a binary search tree object with an optional comparator function.
15
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
16
+ */
27
17
  constructor(options) {
28
18
  super(options);
29
19
  this._comparator = (a, b) => a - b;
@@ -34,17 +24,33 @@ class BST extends binary_tree_1.BinaryTree {
34
24
  }
35
25
  }
36
26
  }
37
- createNode(id, val, count) {
38
- return val !== null ? new BSTNode(id, val, count) : null;
27
+ /**
28
+ * The function creates a new binary search tree node with the given id and value.
29
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
30
+ * identify each node in the binary tree.
31
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
32
+ * that will be stored in the node.
33
+ * @returns a new instance of the BSTNode class with the specified id and value.
34
+ */
35
+ createNode(id, val) {
36
+ return new BSTNode(id, val);
39
37
  }
40
- put(id, val, count = 1) {
41
- var _a;
38
+ /**
39
+ * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
40
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
41
+ * is of type `BinaryTreeNodeId`.
42
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
43
+ * the value associated with the node.
44
+ * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
45
+ * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
46
+ */
47
+ add(id, val) {
48
+ // TODO support node as a param
42
49
  let inserted = null;
43
- const newNode = this.createNode(id, val, count);
50
+ const newNode = this.createNode(id, val);
44
51
  if (this.root === null) {
45
- this.root = newNode;
46
- this.size++;
47
- this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 1;
52
+ this._setRoot(newNode);
53
+ this._setSize(this.size + 1);
48
54
  inserted = (this.root);
49
55
  }
50
56
  else {
@@ -52,27 +58,23 @@ class BST extends binary_tree_1.BinaryTree {
52
58
  let traversing = true;
53
59
  while (traversing) {
54
60
  if (cur !== null && newNode !== null) {
55
- if (this._compare(cur.id, id) === CP.eq) {
61
+ if (this._compare(cur.id, id) === types_1.CP.eq) {
56
62
  if (newNode) {
57
- cur.count += newNode.count;
58
- this.count += newNode.count;
59
63
  cur.val = newNode.val;
60
64
  }
61
65
  //Duplicates are not accepted.
62
66
  traversing = false;
63
67
  inserted = cur;
64
68
  }
65
- else if (this._compare(cur.id, id) === CP.gt) {
69
+ else if (this._compare(cur.id, id) === types_1.CP.gt) {
66
70
  // Traverse left of the node
67
71
  if (cur.left === undefined) {
68
72
  if (newNode) {
69
73
  newNode.parent = cur;
70
- newNode.familyPosition = binary_tree_1.FamilyPosition.left;
71
74
  }
72
75
  //Add to the left of the current node
73
76
  cur.left = newNode;
74
- this.size++;
75
- this.count += newNode.count;
77
+ this._setSize(this.size + 1);
76
78
  traversing = false;
77
79
  inserted = cur.left;
78
80
  }
@@ -82,17 +84,15 @@ class BST extends binary_tree_1.BinaryTree {
82
84
  cur = cur.left;
83
85
  }
84
86
  }
85
- else if (this._compare(cur.id, id) === CP.lt) {
87
+ else if (this._compare(cur.id, id) === types_1.CP.lt) {
86
88
  // Traverse right of the node
87
89
  if (cur.right === undefined) {
88
90
  if (newNode) {
89
91
  newNode.parent = cur;
90
- newNode.familyPosition = binary_tree_1.FamilyPosition.right;
91
92
  }
92
93
  //Add to the right of the current node
93
94
  cur.right = newNode;
94
- this.size++;
95
- this.count += newNode.count;
95
+ this._setSize(this.size + 1);
96
96
  traversing = false;
97
97
  inserted = (cur.right);
98
98
  }
@@ -110,86 +110,61 @@ class BST extends binary_tree_1.BinaryTree {
110
110
  }
111
111
  return inserted;
112
112
  }
113
+ /**
114
+ * The function returns the first node in a binary tree that matches the given property name and value.
115
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
116
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
117
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
118
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
119
+ * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
120
+ */
113
121
  get(nodeProperty, propertyName) {
114
122
  var _a;
115
123
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
116
124
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
117
125
  }
126
+ /**
127
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
128
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
129
+ * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
130
+ * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
131
+ * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
132
+ */
118
133
  lastKey() {
119
134
  var _a, _b, _c, _d, _e, _f;
120
- if (this._compare(0, 1) === CP.lt)
135
+ if (this._compare(0, 1) === types_1.CP.lt)
121
136
  return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
122
- else if (this._compare(0, 1) === CP.gt)
137
+ else if (this._compare(0, 1) === types_1.CP.gt)
123
138
  return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
124
139
  else
125
140
  return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
126
141
  }
127
- remove(id, ignoreCount) {
128
- const bstDeletedResult = [];
129
- if (!this.root)
130
- return bstDeletedResult;
131
- const curr = this.get(id);
132
- if (!curr)
133
- return bstDeletedResult;
134
- const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
135
- let needBalanced = null, orgCurrent = curr;
136
- if (curr.count > 1 && !ignoreCount) {
137
- curr.count--;
138
- this.count--;
139
- }
140
- else {
141
- if (!curr.left) {
142
- if (!parent) {
143
- if (curr.right !== undefined)
144
- this.root = curr.right;
145
- }
146
- else {
147
- switch (curr.familyPosition) {
148
- case binary_tree_1.FamilyPosition.left:
149
- parent.left = curr.right;
150
- break;
151
- case binary_tree_1.FamilyPosition.right:
152
- parent.right = curr.right;
153
- break;
154
- }
155
- needBalanced = parent;
156
- }
157
- }
158
- else {
159
- const leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
160
- if (leftSubTreeMax) {
161
- const parentOfLeftSubTreeMax = leftSubTreeMax.parent;
162
- orgCurrent = curr.swapLocation(leftSubTreeMax);
163
- if (parentOfLeftSubTreeMax) {
164
- if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
165
- parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
166
- else
167
- parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
168
- needBalanced = parentOfLeftSubTreeMax;
169
- }
170
- }
171
- }
172
- this.size--;
173
- this.count -= curr.count;
174
- }
175
- bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
176
- return bstDeletedResult;
177
- }
142
+ /**
143
+ * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
144
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
145
+ * `N` type. It represents the property of the binary tree node that you want to compare with.
146
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
147
+ * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
148
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
149
+ * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
150
+ * is set to `true`, the function will return an array with only one node (if
151
+ * @returns an array of nodes (type N).
152
+ */
178
153
  getNodes(nodeProperty, propertyName, onlyOne) {
179
154
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
180
155
  if (!this.root)
181
156
  return [];
182
157
  const result = [];
183
- if (this._loopType === binary_tree_1.LoopType.recursive) {
158
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
184
159
  const _traverse = (cur) => {
185
160
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
186
161
  return;
187
162
  if (!cur.left && !cur.right)
188
163
  return;
189
164
  if (propertyName === 'id') {
190
- if (this._compare(cur.id, nodeProperty) === CP.gt)
165
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
191
166
  cur.left && _traverse(cur.left);
192
- if (this._compare(cur.id, nodeProperty) === CP.lt)
167
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
193
168
  cur.right && _traverse(cur.right);
194
169
  }
195
170
  else {
@@ -207,9 +182,9 @@ class BST extends binary_tree_1.BinaryTree {
207
182
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
208
183
  return result;
209
184
  if (propertyName === 'id') {
210
- if (this._compare(cur.id, nodeProperty) === CP.gt)
185
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
211
186
  cur.left && queue.push(cur.left);
212
- if (this._compare(cur.id, nodeProperty) === CP.lt)
187
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
213
188
  cur.right && queue.push(cur.right);
214
189
  }
215
190
  else {
@@ -222,19 +197,30 @@ class BST extends binary_tree_1.BinaryTree {
222
197
  return result;
223
198
  }
224
199
  // --- start additional functions
225
- lesserSum(id, propertyName) {
200
+ /**
201
+ * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
202
+ * less than a given node.
203
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
204
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
205
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
206
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
207
+ * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
208
+ */
209
+ lesserSum(beginNode, propertyName) {
226
210
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
211
+ if (typeof beginNode === 'number')
212
+ beginNode = this.get(beginNode, 'id');
213
+ if (!beginNode)
214
+ return 0;
227
215
  if (!this.root)
228
216
  return 0;
217
+ const id = beginNode.id;
229
218
  const getSumByPropertyName = (cur) => {
230
219
  let needSum;
231
220
  switch (propertyName) {
232
221
  case 'id':
233
222
  needSum = cur.id;
234
223
  break;
235
- case 'count':
236
- needSum = cur.count;
237
- break;
238
224
  default:
239
225
  needSum = cur.id;
240
226
  break;
@@ -242,15 +228,15 @@ class BST extends binary_tree_1.BinaryTree {
242
228
  return needSum;
243
229
  };
244
230
  let sum = 0;
245
- if (this._loopType === binary_tree_1.LoopType.recursive) {
231
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
246
232
  const _traverse = (cur) => {
247
233
  const compared = this._compare(cur.id, id);
248
- if (compared === CP.eq) {
234
+ if (compared === types_1.CP.eq) {
249
235
  if (cur.right)
250
236
  sum += this.subTreeSum(cur.right, propertyName);
251
237
  return;
252
238
  }
253
- else if (compared === CP.lt) {
239
+ else if (compared === types_1.CP.lt) {
254
240
  if (cur.left)
255
241
  sum += this.subTreeSum(cur.left, propertyName);
256
242
  sum += getSumByPropertyName(cur);
@@ -274,12 +260,12 @@ class BST extends binary_tree_1.BinaryTree {
274
260
  const cur = queue.shift();
275
261
  if (cur) {
276
262
  const compared = this._compare(cur.id, id);
277
- if (compared === CP.eq) {
263
+ if (compared === types_1.CP.eq) {
278
264
  if (cur.right)
279
265
  sum += this.subTreeSum(cur.right, propertyName);
280
266
  return sum;
281
267
  }
282
- else if (compared === CP.lt) { // todo maybe a bug
268
+ else if (compared === types_1.CP.lt) { // todo maybe a bug
283
269
  if (cur.left)
284
270
  sum += this.subTreeSum(cur.left, propertyName);
285
271
  sum += getSumByPropertyName(cur);
@@ -299,8 +285,25 @@ class BST extends binary_tree_1.BinaryTree {
299
285
  }
300
286
  return sum;
301
287
  }
288
+ /**
289
+ * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
290
+ * have a greater value than a given node.
291
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
292
+ * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
293
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
294
+ * each greater node should be increased.
295
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
296
+ * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
297
+ * 'id'.
298
+ * @returns a boolean value.
299
+ */
302
300
  allGreaterNodesAdd(node, delta, propertyName) {
303
301
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
302
+ if (typeof node === 'number')
303
+ node = this.get(node, 'id');
304
+ if (!node)
305
+ return false;
306
+ const id = node.id;
304
307
  if (!this.root)
305
308
  return false;
306
309
  const _sumByPropertyName = (cur) => {
@@ -308,23 +311,21 @@ class BST extends binary_tree_1.BinaryTree {
308
311
  case 'id':
309
312
  cur.id += delta;
310
313
  break;
311
- case 'count':
312
- cur.count += delta;
313
- break;
314
314
  default:
315
315
  cur.id += delta;
316
316
  break;
317
317
  }
318
318
  };
319
- if (this._loopType === binary_tree_1.LoopType.recursive) {
319
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
320
320
  const _traverse = (cur) => {
321
- const compared = this._compare(cur.id, node.id);
322
- _sumByPropertyName(cur);
321
+ const compared = this._compare(cur.id, id);
322
+ if (compared === types_1.CP.gt)
323
+ _sumByPropertyName(cur);
323
324
  if (!cur.left && !cur.right)
324
325
  return;
325
- if (cur.left && compared === CP.gt)
326
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
326
327
  _traverse(cur.left);
327
- else if (cur.right && compared === CP.gt)
328
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
328
329
  _traverse(cur.right);
329
330
  };
330
331
  _traverse(this.root);
@@ -335,29 +336,44 @@ class BST extends binary_tree_1.BinaryTree {
335
336
  while (queue.length > 0) {
336
337
  const cur = queue.shift();
337
338
  if (cur) {
338
- const compared = this._compare(cur.id, node.id);
339
- _sumByPropertyName(cur);
340
- if (cur.left && compared === CP.gt)
339
+ const compared = this._compare(cur.id, id);
340
+ if (compared === types_1.CP.gt)
341
+ _sumByPropertyName(cur);
342
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
341
343
  queue.push(cur.left);
342
- else if (cur.right && compared === CP.gt)
344
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
343
345
  queue.push(cur.right);
344
346
  }
345
347
  }
346
348
  return true;
347
349
  }
348
350
  }
349
- balance() {
351
+ /**
352
+ * Balancing Adjustment:
353
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
354
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
355
+ *
356
+ * Use Cases and Efficiency:
357
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
358
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
359
+ */
360
+ /**
361
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
362
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
363
+ * @returns The function `perfectlyBalance()` returns a boolean value.
364
+ */
365
+ perfectlyBalance() {
350
366
  const sorted = this.DFS('in', 'node'), n = sorted.length;
351
367
  this.clear();
352
368
  if (sorted.length < 1)
353
369
  return false;
354
- if (this._loopType === binary_tree_1.LoopType.recursive) {
370
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
355
371
  const buildBalanceBST = (l, r) => {
356
372
  if (l > r)
357
373
  return;
358
374
  const m = l + Math.floor((r - l) / 2);
359
375
  const midNode = sorted[m];
360
- this.put(midNode.id, midNode.val, midNode.count);
376
+ this.add(midNode.id, midNode.val);
361
377
  buildBalanceBST(l, m - 1);
362
378
  buildBalanceBST(m + 1, r);
363
379
  };
@@ -373,7 +389,7 @@ class BST extends binary_tree_1.BinaryTree {
373
389
  if (l <= r) {
374
390
  const m = l + Math.floor((r - l) / 2);
375
391
  const midNode = sorted[m];
376
- this.put(midNode.id, midNode.val, midNode.count);
392
+ this.add(midNode.id, midNode.val);
377
393
  stack.push([m + 1, r]);
378
394
  stack.push([l, m - 1]);
379
395
  }
@@ -382,12 +398,16 @@ class BST extends binary_tree_1.BinaryTree {
382
398
  return true;
383
399
  }
384
400
  }
401
+ /**
402
+ * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
403
+ * @returns a boolean value.
404
+ */
385
405
  isAVLBalanced() {
386
406
  var _a, _b;
387
407
  if (!this.root)
388
408
  return true;
389
409
  let balanced = true;
390
- if (this._loopType === binary_tree_1.LoopType.recursive) {
410
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
391
411
  const _height = (cur) => {
392
412
  if (!cur)
393
413
  return 0;
@@ -400,7 +420,8 @@ class BST extends binary_tree_1.BinaryTree {
400
420
  }
401
421
  else {
402
422
  const stack = [];
403
- let node = this.root, last = null, depths = new Map();
423
+ let node = this.root, last = null;
424
+ const depths = new Map();
404
425
  while (stack.length > 0 || node) {
405
426
  if (node) {
406
427
  stack.push(node);
@@ -411,8 +432,8 @@ class BST extends binary_tree_1.BinaryTree {
411
432
  if (!node.right || last === node.right) {
412
433
  node = stack.pop();
413
434
  if (node) {
414
- let left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
415
- let right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
435
+ const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
436
+ const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
416
437
  if (Math.abs(left - right) > 1)
417
438
  return false;
418
439
  depths.set(node, 1 + Math.max(left, right));
@@ -427,5 +448,22 @@ class BST extends binary_tree_1.BinaryTree {
427
448
  }
428
449
  return balanced;
429
450
  }
451
+ /**
452
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
453
+ * greater than, less than, or equal to the second ID.
454
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
455
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
456
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
457
+ * than), or CP.eq (equal).
458
+ */
459
+ _compare(a, b) {
460
+ const compared = this._comparator(a, b);
461
+ if (compared > 0)
462
+ return types_1.CP.gt;
463
+ else if (compared < 0)
464
+ return types_1.CP.lt;
465
+ else
466
+ return types_1.CP.eq;
467
+ }
430
468
  }
431
469
  exports.BST = BST;