red-black-tree-typed 1.47.3

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 (341) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +713 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/data-structures/binary-tree/avl-tree.d.ts +173 -0
  20. package/dist/data-structures/binary-tree/avl-tree.js +429 -0
  21. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  22. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  23. package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
  24. package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
  25. package/dist/data-structures/binary-tree/bst.d.ts +253 -0
  26. package/dist/data-structures/binary-tree/bst.js +651 -0
  27. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  28. package/dist/data-structures/binary-tree/index.js +23 -0
  29. package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
  30. package/dist/data-structures/binary-tree/rb-tree.js +524 -0
  31. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  32. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  33. package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
  34. package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
  35. package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
  36. package/dist/data-structures/graph/abstract-graph.js +1047 -0
  37. package/dist/data-structures/graph/directed-graph.d.ts +320 -0
  38. package/dist/data-structures/graph/directed-graph.js +530 -0
  39. package/dist/data-structures/graph/index.d.ts +4 -0
  40. package/dist/data-structures/graph/index.js +20 -0
  41. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  42. package/dist/data-structures/graph/map-graph.js +93 -0
  43. package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
  44. package/dist/data-structures/graph/undirected-graph.js +302 -0
  45. package/dist/data-structures/hash/hash-map.d.ts +186 -0
  46. package/dist/data-structures/hash/hash-map.js +367 -0
  47. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  48. package/dist/data-structures/hash/hash-table.js +236 -0
  49. package/dist/data-structures/hash/index.d.ts +2 -0
  50. package/dist/data-structures/hash/index.js +18 -0
  51. package/dist/data-structures/heap/heap.d.ts +410 -0
  52. package/dist/data-structures/heap/heap.js +697 -0
  53. package/dist/data-structures/heap/index.d.ts +3 -0
  54. package/dist/data-structures/heap/index.js +19 -0
  55. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  56. package/dist/data-structures/heap/max-heap.js +26 -0
  57. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  58. package/dist/data-structures/heap/min-heap.js +26 -0
  59. package/dist/data-structures/index.d.ts +11 -0
  60. package/dist/data-structures/index.js +27 -0
  61. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
  63. package/dist/data-structures/linked-list/index.d.ts +3 -0
  64. package/dist/data-structures/linked-list/index.js +19 -0
  65. package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
  67. package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
  69. package/dist/data-structures/matrix/index.d.ts +4 -0
  70. package/dist/data-structures/matrix/index.js +20 -0
  71. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  72. package/dist/data-structures/matrix/matrix.js +28 -0
  73. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  74. package/dist/data-structures/matrix/matrix2d.js +199 -0
  75. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  76. package/dist/data-structures/matrix/navigator.js +106 -0
  77. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  78. package/dist/data-structures/matrix/vector2d.js +290 -0
  79. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  80. package/dist/data-structures/priority-queue/index.js +19 -0
  81. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  83. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  85. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  86. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  87. package/dist/data-structures/queue/deque.d.ts +572 -0
  88. package/dist/data-structures/queue/deque.js +990 -0
  89. package/dist/data-structures/queue/index.d.ts +2 -0
  90. package/dist/data-structures/queue/index.js +18 -0
  91. package/dist/data-structures/queue/queue.d.ts +209 -0
  92. package/dist/data-structures/queue/queue.js +274 -0
  93. package/dist/data-structures/stack/index.d.ts +1 -0
  94. package/dist/data-structures/stack/index.js +17 -0
  95. package/dist/data-structures/stack/stack.d.ts +106 -0
  96. package/dist/data-structures/stack/stack.js +136 -0
  97. package/dist/data-structures/tree/index.d.ts +1 -0
  98. package/dist/data-structures/tree/index.js +17 -0
  99. package/dist/data-structures/tree/tree.d.ts +8 -0
  100. package/dist/data-structures/tree/tree.js +40 -0
  101. package/dist/data-structures/trie/index.d.ts +1 -0
  102. package/dist/data-structures/trie/index.js +17 -0
  103. package/dist/data-structures/trie/trie.d.ts +155 -0
  104. package/dist/data-structures/trie/trie.js +326 -0
  105. package/dist/index.d.ts +10 -0
  106. package/dist/index.js +27 -0
  107. package/dist/interfaces/binary-tree.d.ts +7 -0
  108. package/dist/interfaces/binary-tree.js +2 -0
  109. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  110. package/dist/interfaces/doubly-linked-list.js +2 -0
  111. package/dist/interfaces/graph.d.ts +5 -0
  112. package/dist/interfaces/graph.js +2 -0
  113. package/dist/interfaces/heap.d.ts +1 -0
  114. package/dist/interfaces/heap.js +2 -0
  115. package/dist/interfaces/index.d.ts +8 -0
  116. package/dist/interfaces/index.js +24 -0
  117. package/dist/interfaces/navigator.d.ts +1 -0
  118. package/dist/interfaces/navigator.js +2 -0
  119. package/dist/interfaces/priority-queue.d.ts +1 -0
  120. package/dist/interfaces/priority-queue.js +2 -0
  121. package/dist/interfaces/segment-tree.d.ts +1 -0
  122. package/dist/interfaces/segment-tree.js +2 -0
  123. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  124. package/dist/interfaces/singly-linked-list.js +2 -0
  125. package/dist/types/common.d.ts +20 -0
  126. package/dist/types/common.js +9 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
  128. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  130. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  132. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  133. package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
  134. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  135. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  136. package/dist/types/data-structures/binary-tree/index.js +22 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
  138. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  140. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  141. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
  142. package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
  143. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  144. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  145. package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
  146. package/dist/types/data-structures/graph/directed-graph.js +2 -0
  147. package/dist/types/data-structures/graph/index.d.ts +3 -0
  148. package/dist/types/data-structures/graph/index.js +19 -0
  149. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  150. package/dist/types/data-structures/graph/map-graph.js +2 -0
  151. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  152. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  153. package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
  154. package/dist/types/data-structures/hash/hash-map.js +2 -0
  155. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  156. package/dist/types/data-structures/hash/hash-table.js +2 -0
  157. package/dist/types/data-structures/hash/index.d.ts +3 -0
  158. package/dist/types/data-structures/hash/index.js +18 -0
  159. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/heap.js +2 -0
  161. package/dist/types/data-structures/heap/index.d.ts +1 -0
  162. package/dist/types/data-structures/heap/index.js +17 -0
  163. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  164. package/dist/types/data-structures/heap/max-heap.js +2 -0
  165. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  166. package/dist/types/data-structures/heap/min-heap.js +2 -0
  167. package/dist/types/data-structures/index.d.ts +11 -0
  168. package/dist/types/data-structures/index.js +27 -0
  169. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  172. package/dist/types/data-structures/linked-list/index.js +18 -0
  173. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  174. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  175. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  176. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  177. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/index.js +17 -0
  179. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  180. package/dist/types/data-structures/matrix/matrix.js +2 -0
  181. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  183. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  184. package/dist/types/data-structures/matrix/navigator.js +2 -0
  185. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  186. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  187. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  188. package/dist/types/data-structures/priority-queue/index.js +19 -0
  189. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  191. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  192. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  193. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  194. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  195. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  196. package/dist/types/data-structures/queue/deque.js +2 -0
  197. package/dist/types/data-structures/queue/index.d.ts +2 -0
  198. package/dist/types/data-structures/queue/index.js +18 -0
  199. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  200. package/dist/types/data-structures/queue/queue.js +2 -0
  201. package/dist/types/data-structures/stack/index.d.ts +1 -0
  202. package/dist/types/data-structures/stack/index.js +17 -0
  203. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  204. package/dist/types/data-structures/stack/stack.js +2 -0
  205. package/dist/types/data-structures/tree/index.d.ts +1 -0
  206. package/dist/types/data-structures/tree/index.js +17 -0
  207. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  208. package/dist/types/data-structures/tree/tree.js +2 -0
  209. package/dist/types/data-structures/trie/index.d.ts +1 -0
  210. package/dist/types/data-structures/trie/index.js +17 -0
  211. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  212. package/dist/types/data-structures/trie/trie.js +2 -0
  213. package/dist/types/index.d.ts +3 -0
  214. package/dist/types/index.js +19 -0
  215. package/dist/types/utils/index.d.ts +2 -0
  216. package/dist/types/utils/index.js +18 -0
  217. package/dist/types/utils/utils.d.ts +7 -0
  218. package/dist/types/utils/utils.js +2 -0
  219. package/dist/types/utils/validate-type.d.ts +19 -0
  220. package/dist/types/utils/validate-type.js +2 -0
  221. package/dist/utils/index.d.ts +1 -0
  222. package/dist/utils/index.js +17 -0
  223. package/dist/utils/utils.d.ts +24 -0
  224. package/dist/utils/utils.js +89 -0
  225. package/docs/.nojekyll +1 -0
  226. package/docs/assets/highlight.css +92 -0
  227. package/docs/assets/main.js +59 -0
  228. package/docs/assets/navigation.js +1 -0
  229. package/docs/assets/search.js +1 -0
  230. package/docs/assets/style.css +1383 -0
  231. package/docs/classes/AVLTree.html +2046 -0
  232. package/docs/classes/AVLTreeNode.html +263 -0
  233. package/docs/index.html +523 -0
  234. package/docs/modules.html +45 -0
  235. package/jest.config.js +8 -0
  236. package/package.json +147 -0
  237. package/src/data-structures/binary-tree/avl-tree.ts +443 -0
  238. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  239. package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
  240. package/src/data-structures/binary-tree/bst.ts +676 -0
  241. package/src/data-structures/binary-tree/index.ts +7 -0
  242. package/src/data-structures/binary-tree/rb-tree.ts +585 -0
  243. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  244. package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
  245. package/src/data-structures/graph/abstract-graph.ts +1181 -0
  246. package/src/data-structures/graph/directed-graph.ts +593 -0
  247. package/src/data-structures/graph/index.ts +4 -0
  248. package/src/data-structures/graph/map-graph.ts +106 -0
  249. package/src/data-structures/graph/undirected-graph.ts +331 -0
  250. package/src/data-structures/hash/hash-map.ts +401 -0
  251. package/src/data-structures/hash/hash-table.ts +268 -0
  252. package/src/data-structures/hash/index.ts +2 -0
  253. package/src/data-structures/heap/heap.ts +790 -0
  254. package/src/data-structures/heap/index.ts +3 -0
  255. package/src/data-structures/heap/max-heap.ts +26 -0
  256. package/src/data-structures/heap/min-heap.ts +26 -0
  257. package/src/data-structures/index.ts +11 -0
  258. package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
  259. package/src/data-structures/linked-list/index.ts +3 -0
  260. package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
  261. package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
  262. package/src/data-structures/matrix/index.ts +4 -0
  263. package/src/data-structures/matrix/matrix.ts +27 -0
  264. package/src/data-structures/matrix/matrix2d.ts +211 -0
  265. package/src/data-structures/matrix/navigator.ts +121 -0
  266. package/src/data-structures/matrix/vector2d.ts +315 -0
  267. package/src/data-structures/priority-queue/index.ts +3 -0
  268. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  269. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  270. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  271. package/src/data-structures/queue/deque.ts +1073 -0
  272. package/src/data-structures/queue/index.ts +2 -0
  273. package/src/data-structures/queue/queue.ts +308 -0
  274. package/src/data-structures/stack/index.ts +1 -0
  275. package/src/data-structures/stack/stack.ts +150 -0
  276. package/src/data-structures/tree/index.ts +1 -0
  277. package/src/data-structures/tree/tree.ts +41 -0
  278. package/src/data-structures/trie/index.ts +1 -0
  279. package/src/data-structures/trie/trie.ts +345 -0
  280. package/src/index.ts +11 -0
  281. package/src/interfaces/binary-tree.ts +10 -0
  282. package/src/interfaces/doubly-linked-list.ts +1 -0
  283. package/src/interfaces/graph.ts +7 -0
  284. package/src/interfaces/heap.ts +1 -0
  285. package/src/interfaces/index.ts +8 -0
  286. package/src/interfaces/navigator.ts +1 -0
  287. package/src/interfaces/priority-queue.ts +1 -0
  288. package/src/interfaces/segment-tree.ts +1 -0
  289. package/src/interfaces/singly-linked-list.ts +1 -0
  290. package/src/types/common.ts +23 -0
  291. package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
  292. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  293. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  294. package/src/types/data-structures/binary-tree/bst.ts +13 -0
  295. package/src/types/data-structures/binary-tree/index.ts +6 -0
  296. package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
  297. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  298. package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
  299. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  300. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  301. package/src/types/data-structures/graph/index.ts +3 -0
  302. package/src/types/data-structures/graph/map-graph.ts +1 -0
  303. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  304. package/src/types/data-structures/hash/hash-map.ts +12 -0
  305. package/src/types/data-structures/hash/hash-table.ts +1 -0
  306. package/src/types/data-structures/hash/index.ts +4 -0
  307. package/src/types/data-structures/heap/heap.ts +1 -0
  308. package/src/types/data-structures/heap/index.ts +1 -0
  309. package/src/types/data-structures/heap/max-heap.ts +1 -0
  310. package/src/types/data-structures/heap/min-heap.ts +1 -0
  311. package/src/types/data-structures/index.ts +11 -0
  312. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  313. package/src/types/data-structures/linked-list/index.ts +2 -0
  314. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  315. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  316. package/src/types/data-structures/matrix/index.ts +1 -0
  317. package/src/types/data-structures/matrix/matrix.ts +1 -0
  318. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  319. package/src/types/data-structures/matrix/navigator.ts +14 -0
  320. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  321. package/src/types/data-structures/priority-queue/index.ts +3 -0
  322. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  323. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  324. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  325. package/src/types/data-structures/queue/deque.ts +1 -0
  326. package/src/types/data-structures/queue/index.ts +2 -0
  327. package/src/types/data-structures/queue/queue.ts +1 -0
  328. package/src/types/data-structures/stack/index.ts +1 -0
  329. package/src/types/data-structures/stack/stack.ts +1 -0
  330. package/src/types/data-structures/tree/index.ts +1 -0
  331. package/src/types/data-structures/tree/tree.ts +1 -0
  332. package/src/types/data-structures/trie/index.ts +1 -0
  333. package/src/types/data-structures/trie/trie.ts +1 -0
  334. package/src/types/index.ts +3 -0
  335. package/src/types/utils/index.ts +2 -0
  336. package/src/types/utils/utils.ts +6 -0
  337. package/src/types/utils/validate-type.ts +35 -0
  338. package/src/utils/index.ts +1 -0
  339. package/src/utils/utils.ts +101 -0
  340. package/test/index.test.ts +111 -0
  341. package/tsconfig.json +38 -0
@@ -0,0 +1,173 @@
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 { BST, BSTNode } from './bst';
9
+ import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
10
+ import { BTNCallback } from '../../types';
11
+ import { IBinaryTree } from '../../interfaces';
12
+ export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
13
+ height: number;
14
+ constructor(key: BTNKey, value?: V);
15
+ }
16
+ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
17
+ options: AVLTreeOptions;
18
+ /**
19
+ * This is a constructor function for an AVL tree data structure in TypeScript.
20
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
21
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
22
+ * options.
23
+ */
24
+ constructor(options?: AVLTreeOptions);
25
+ /**
26
+ * The function creates a new AVL tree node with the specified key and value.
27
+ * @param {BTNKey} key - The key parameter is the key value that will be associated with
28
+ * the new node. It is used to determine the position of the node in the binary search tree.
29
+ * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
30
+ * type `V`, which means it can be any value that is assignable to the `value` property of the
31
+ * node type `N`.
32
+ * @returns a new AVLTreeNode object with the specified key and value.
33
+ */
34
+ createNode(key: BTNKey, value?: V): N;
35
+ createTree(options?: AVLTreeOptions): TREE;
36
+ /**
37
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
38
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
39
+ */
40
+ /**
41
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
42
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
43
+ *
44
+ * The function overrides the add method of a class, adds a key-value pair to a data structure, and
45
+ * balances the structure if necessary.
46
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
47
+ * `BTNKey`, `N`, `null`, or `undefined`.
48
+ * @param {V} [value] - The `value` parameter is the value associated with the key that is being
49
+ * added to the binary search tree.
50
+ * @returns The method is returning either a node (N) or undefined.
51
+ */
52
+ add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
53
+ /**
54
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
55
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
56
+ */
57
+ /**
58
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
59
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
60
+ *
61
+ * The function overrides the delete method of a binary tree, performs the deletion, and then
62
+ * balances the tree if necessary.
63
+ * @param identifier - The `identifier` parameter is the value or condition used to identify the
64
+ * node(s) to be deleted from the binary tree. It can be of any type and is the return type of the
65
+ * `callback` function.
66
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
67
+ * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
68
+ * default to the `_defaultOneParamCallback` function. The `callback` function should have a single
69
+ * parameter of type `N
70
+ * @returns The method is returning an array of `BiTreeDeleteResult<N>`.
71
+ */
72
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BiTreeDeleteResult<N>[];
73
+ /**
74
+ * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
75
+ * tree.
76
+ * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
77
+ * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
78
+ * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
79
+ * node where the values from the source node will be swapped to.
80
+ * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
81
+ * if either `srcNode` or `destNode` is undefined.
82
+ */
83
+ protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined;
84
+ /**
85
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
86
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
87
+ */
88
+ /**
89
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
90
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
91
+ *
92
+ * The function calculates the balance factor of a node in a binary tree.
93
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
94
+ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
95
+ * height of the left subtree from the height of the right subtree.
96
+ */
97
+ protected _balanceFactor(node: N): number;
98
+ /**
99
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
100
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
101
+ */
102
+ /**
103
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
104
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
105
+ *
106
+ * The function updates the height of a node in a binary tree based on the heights of its left and
107
+ * right children.
108
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
109
+ */
110
+ protected _updateHeight(node: N): void;
111
+ /**
112
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
113
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
114
+ */
115
+ /**
116
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
117
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
118
+ *
119
+ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
120
+ * to restore balance in an AVL tree after inserting a node.
121
+ * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
122
+ * AVL tree that needs to be balanced.
123
+ */
124
+ protected _balancePath(node: N): void;
125
+ /**
126
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
127
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
128
+ */
129
+ /**
130
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
131
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
132
+ *
133
+ * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
134
+ * @param {N} A - A is a node in a binary tree.
135
+ */
136
+ protected _balanceLL(A: N): void;
137
+ /**
138
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
139
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
140
+ */
141
+ /**
142
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
143
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
144
+ *
145
+ * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
146
+ * @param {N} A - A is a node in a binary tree.
147
+ */
148
+ protected _balanceLR(A: N): void;
149
+ /**
150
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
151
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
152
+ */
153
+ /**
154
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
155
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
156
+ *
157
+ * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
158
+ * @param {N} A - A is a node in a binary tree.
159
+ */
160
+ protected _balanceRR(A: N): void;
161
+ /**
162
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
163
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
164
+ */
165
+ /**
166
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
167
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
168
+ *
169
+ * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
170
+ * @param {N} A - A is a node in a binary tree.
171
+ */
172
+ protected _balanceRL(A: N): void;
173
+ }
@@ -0,0 +1,429 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AVLTree = exports.AVLTreeNode = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ const bst_1 = require("./bst");
12
+ const types_1 = require("../../types");
13
+ class AVLTreeNode extends bst_1.BSTNode {
14
+ constructor(key, value) {
15
+ super(key, value);
16
+ this.height = 0;
17
+ }
18
+ }
19
+ exports.AVLTreeNode = AVLTreeNode;
20
+ class AVLTree extends bst_1.BST {
21
+ /**
22
+ * This is a constructor function for an AVL tree data structure in TypeScript.
23
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
24
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
25
+ * options.
26
+ */
27
+ constructor(options) {
28
+ super(options);
29
+ if (options) {
30
+ this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
31
+ }
32
+ else {
33
+ this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
34
+ }
35
+ }
36
+ /**
37
+ * The function creates a new AVL tree node with the specified key and value.
38
+ * @param {BTNKey} key - The key parameter is the key value that will be associated with
39
+ * the new node. It is used to determine the position of the node in the binary search tree.
40
+ * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
41
+ * type `V`, which means it can be any value that is assignable to the `value` property of the
42
+ * node type `N`.
43
+ * @returns a new AVLTreeNode object with the specified key and value.
44
+ */
45
+ createNode(key, value) {
46
+ return new AVLTreeNode(key, value);
47
+ }
48
+ createTree(options) {
49
+ return new AVLTree(Object.assign(Object.assign({}, this.options), options));
50
+ }
51
+ /**
52
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
53
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
54
+ */
55
+ /**
56
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
57
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
58
+ *
59
+ * The function overrides the add method of a class, adds a key-value pair to a data structure, and
60
+ * balances the structure if necessary.
61
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
62
+ * `BTNKey`, `N`, `null`, or `undefined`.
63
+ * @param {V} [value] - The `value` parameter is the value associated with the key that is being
64
+ * added to the binary search tree.
65
+ * @returns The method is returning either a node (N) or undefined.
66
+ */
67
+ add(keyOrNode, value) {
68
+ if (keyOrNode === null)
69
+ return undefined;
70
+ const inserted = super.add(keyOrNode, value);
71
+ if (inserted)
72
+ this._balancePath(inserted);
73
+ return inserted;
74
+ }
75
+ /**
76
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
77
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
78
+ */
79
+ /**
80
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
81
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
82
+ *
83
+ * The function overrides the delete method of a binary tree, performs the deletion, and then
84
+ * balances the tree if necessary.
85
+ * @param identifier - The `identifier` parameter is the value or condition used to identify the
86
+ * node(s) to be deleted from the binary tree. It can be of any type and is the return type of the
87
+ * `callback` function.
88
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
89
+ * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
90
+ * default to the `_defaultOneParamCallback` function. The `callback` function should have a single
91
+ * parameter of type `N
92
+ * @returns The method is returning an array of `BiTreeDeleteResult<N>`.
93
+ */
94
+ delete(identifier, callback = this._defaultOneParamCallback) {
95
+ if (identifier instanceof AVLTreeNode)
96
+ callback = (node => node);
97
+ const deletedResults = super.delete(identifier, callback);
98
+ for (const { needBalanced } of deletedResults) {
99
+ if (needBalanced) {
100
+ this._balancePath(needBalanced);
101
+ }
102
+ }
103
+ return deletedResults;
104
+ }
105
+ /**
106
+ * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
107
+ * tree.
108
+ * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
109
+ * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
110
+ * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
111
+ * node where the values from the source node will be swapped to.
112
+ * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
113
+ * if either `srcNode` or `destNode` is undefined.
114
+ */
115
+ _swap(srcNode, destNode) {
116
+ srcNode = this.ensureNotKey(srcNode);
117
+ destNode = this.ensureNotKey(destNode);
118
+ if (srcNode && destNode) {
119
+ const { key, value, height } = destNode;
120
+ const tempNode = this.createNode(key, value);
121
+ if (tempNode) {
122
+ tempNode.height = height;
123
+ destNode.key = srcNode.key;
124
+ destNode.value = srcNode.value;
125
+ destNode.height = srcNode.height;
126
+ srcNode.key = tempNode.key;
127
+ srcNode.value = tempNode.value;
128
+ srcNode.height = tempNode.height;
129
+ }
130
+ return destNode;
131
+ }
132
+ return undefined;
133
+ }
134
+ /**
135
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
136
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
137
+ */
138
+ /**
139
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
140
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
141
+ *
142
+ * The function calculates the balance factor of a node in a binary tree.
143
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
144
+ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
145
+ * height of the left subtree from the height of the right subtree.
146
+ */
147
+ _balanceFactor(node) {
148
+ if (!node.right)
149
+ // node has no right subtree
150
+ return -node.height;
151
+ else if (!node.left)
152
+ // node has no left subtree
153
+ return +node.height;
154
+ else
155
+ return node.right.height - node.left.height;
156
+ }
157
+ /**
158
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
159
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
160
+ */
161
+ /**
162
+ * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
163
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
164
+ *
165
+ * The function updates the height of a node in a binary tree based on the heights of its left and
166
+ * right children.
167
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
168
+ */
169
+ _updateHeight(node) {
170
+ if (!node.left && !node.right)
171
+ node.height = 0;
172
+ else if (!node.left) {
173
+ const rightHeight = node.right ? node.right.height : 0;
174
+ node.height = 1 + rightHeight;
175
+ }
176
+ else if (!node.right)
177
+ node.height = 1 + node.left.height;
178
+ else
179
+ node.height = 1 + Math.max(node.right.height, node.left.height);
180
+ }
181
+ /**
182
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
183
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
184
+ */
185
+ /**
186
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
187
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
188
+ *
189
+ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
190
+ * to restore balance in an AVL tree after inserting a node.
191
+ * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
192
+ * AVL tree that needs to be balanced.
193
+ */
194
+ _balancePath(node) {
195
+ const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
196
+ for (let i = 0; i < path.length; i++) {
197
+ // second O(log n)
198
+ const A = path[i];
199
+ // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
200
+ this._updateHeight(A); // first O(1)
201
+ // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
202
+ // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
203
+ switch (this._balanceFactor(A) // second O(1)
204
+ ) {
205
+ case -2:
206
+ if (A && A.left) {
207
+ if (this._balanceFactor(A.left) <= 0) {
208
+ // second O(1)
209
+ // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
210
+ this._balanceLL(A);
211
+ }
212
+ else {
213
+ // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
214
+ this._balanceLR(A);
215
+ }
216
+ }
217
+ break;
218
+ case +2:
219
+ if (A && A.right) {
220
+ if (this._balanceFactor(A.right) >= 0) {
221
+ // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
222
+ this._balanceRR(A);
223
+ }
224
+ else {
225
+ // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
226
+ this._balanceRL(A);
227
+ }
228
+ }
229
+ }
230
+ // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
231
+ }
232
+ }
233
+ /**
234
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
235
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
236
+ */
237
+ /**
238
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
239
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
240
+ *
241
+ * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
242
+ * @param {N} A - A is a node in a binary tree.
243
+ */
244
+ _balanceLL(A) {
245
+ const parentOfA = A.parent;
246
+ const B = A.left;
247
+ A.parent = B;
248
+ if (B && B.right) {
249
+ B.right.parent = A;
250
+ }
251
+ if (B)
252
+ B.parent = parentOfA;
253
+ if (A === this.root) {
254
+ if (B)
255
+ this._setRoot(B);
256
+ }
257
+ else {
258
+ if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
259
+ parentOfA.left = B;
260
+ }
261
+ else {
262
+ if (parentOfA)
263
+ parentOfA.right = B;
264
+ }
265
+ }
266
+ if (B) {
267
+ A.left = B.right;
268
+ B.right = A;
269
+ }
270
+ this._updateHeight(A);
271
+ if (B)
272
+ this._updateHeight(B);
273
+ }
274
+ /**
275
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
276
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
277
+ */
278
+ /**
279
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
280
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
281
+ *
282
+ * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
283
+ * @param {N} A - A is a node in a binary tree.
284
+ */
285
+ _balanceLR(A) {
286
+ const parentOfA = A.parent;
287
+ const B = A.left;
288
+ let C = undefined;
289
+ if (B) {
290
+ C = B.right;
291
+ }
292
+ if (A)
293
+ A.parent = C;
294
+ if (B)
295
+ B.parent = C;
296
+ if (C) {
297
+ if (C.left) {
298
+ C.left.parent = B;
299
+ }
300
+ if (C.right) {
301
+ C.right.parent = A;
302
+ }
303
+ C.parent = parentOfA;
304
+ }
305
+ if (A === this.root) {
306
+ if (C)
307
+ this._setRoot(C);
308
+ }
309
+ else {
310
+ if (parentOfA) {
311
+ if (parentOfA.left === A) {
312
+ parentOfA.left = C;
313
+ }
314
+ else {
315
+ parentOfA.right = C;
316
+ }
317
+ }
318
+ }
319
+ if (C) {
320
+ A.left = C.right;
321
+ if (B)
322
+ B.right = C.left;
323
+ C.left = B;
324
+ C.right = A;
325
+ }
326
+ this._updateHeight(A);
327
+ B && this._updateHeight(B);
328
+ C && this._updateHeight(C);
329
+ }
330
+ /**
331
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
332
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
333
+ */
334
+ /**
335
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
336
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
337
+ *
338
+ * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
339
+ * @param {N} A - A is a node in a binary tree.
340
+ */
341
+ _balanceRR(A) {
342
+ const parentOfA = A.parent;
343
+ const B = A.right;
344
+ A.parent = B;
345
+ if (B) {
346
+ if (B.left) {
347
+ B.left.parent = A;
348
+ }
349
+ B.parent = parentOfA;
350
+ }
351
+ if (A === this.root) {
352
+ if (B)
353
+ this._setRoot(B);
354
+ }
355
+ else {
356
+ if (parentOfA) {
357
+ if (parentOfA.left === A) {
358
+ parentOfA.left = B;
359
+ }
360
+ else {
361
+ parentOfA.right = B;
362
+ }
363
+ }
364
+ }
365
+ if (B) {
366
+ A.right = B.left;
367
+ B.left = A;
368
+ }
369
+ this._updateHeight(A);
370
+ B && this._updateHeight(B);
371
+ }
372
+ /**
373
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
374
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
375
+ */
376
+ /**
377
+ * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
378
+ * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
379
+ *
380
+ * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
381
+ * @param {N} A - A is a node in a binary tree.
382
+ */
383
+ _balanceRL(A) {
384
+ const parentOfA = A.parent;
385
+ const B = A.right;
386
+ let C = undefined;
387
+ if (B) {
388
+ C = B.left;
389
+ }
390
+ A.parent = C;
391
+ if (B)
392
+ B.parent = C;
393
+ if (C) {
394
+ if (C.left) {
395
+ C.left.parent = A;
396
+ }
397
+ if (C.right) {
398
+ C.right.parent = B;
399
+ }
400
+ C.parent = parentOfA;
401
+ }
402
+ if (A === this.root) {
403
+ if (C)
404
+ this._setRoot(C);
405
+ }
406
+ else {
407
+ if (parentOfA) {
408
+ if (parentOfA.left === A) {
409
+ parentOfA.left = C;
410
+ }
411
+ else {
412
+ parentOfA.right = C;
413
+ }
414
+ }
415
+ }
416
+ if (C)
417
+ A.right = C.left;
418
+ if (B && C)
419
+ B.left = C.right;
420
+ if (C)
421
+ C.left = A;
422
+ if (C)
423
+ C.right = B;
424
+ this._updateHeight(A);
425
+ B && this._updateHeight(B);
426
+ C && this._updateHeight(C);
427
+ }
428
+ }
429
+ exports.AVLTree = AVLTree;