data-structure-typed 1.15.1 → 1.16.1

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 (224) hide show
  1. package/README.md +398 -27
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +30 -30
  3. package/dist/data-structures/binary-tree/binary-tree.js +55 -55
  4. package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -17
  5. package/dist/data-structures/binary-tree/segment-tree.js +30 -30
  6. package/dist/data-structures/graph/abstract-graph.d.ts +6 -6
  7. package/dist/data-structures/graph/abstract-graph.js +6 -6
  8. package/dist/data-structures/graph/directed-graph.d.ts +4 -4
  9. package/dist/data-structures/graph/directed-graph.js +6 -6
  10. package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
  11. package/dist/data-structures/hash/coordinate-map.d.ts +2 -2
  12. package/dist/data-structures/hash/coordinate-set.d.ts +2 -2
  13. package/dist/data-structures/heap/heap.d.ts +40 -25
  14. package/dist/data-structures/heap/heap.js +72 -27
  15. package/dist/data-structures/heap/max-heap.d.ts +3 -3
  16. package/dist/data-structures/heap/min-heap.d.ts +3 -3
  17. package/dist/data-structures/index.d.ts +1 -0
  18. package/dist/data-structures/index.js +1 -0
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +9 -9
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +7 -7
  22. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
  23. package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
  24. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
  25. package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
  26. package/dist/data-structures/priority-queue/priority-queue.d.ts +20 -9
  27. package/dist/data-structures/priority-queue/priority-queue.js +34 -28
  28. package/dist/data-structures/queue/deque.d.ts +1 -1
  29. package/dist/data-structures/tree/index.d.ts +1 -0
  30. package/dist/data-structures/tree/index.js +17 -0
  31. package/dist/data-structures/tree/tree.d.ts +9 -0
  32. package/dist/data-structures/tree/tree.js +52 -0
  33. package/dist/data-structures/types/heap.d.ts +0 -4
  34. package/dist/utils/types/utils.d.ts +1 -3
  35. package/dist/utils/types/utils.js +0 -14
  36. package/dist/utils/utils.js +0 -197
  37. package/docs/.nojekyll +1 -0
  38. package/docs/assets/highlight.css +92 -0
  39. package/docs/assets/main.js +58 -0
  40. package/docs/assets/search.js +1 -0
  41. package/docs/assets/style.css +1367 -0
  42. package/docs/classes/AVLTree.html +2192 -0
  43. package/docs/classes/AVLTreeNode.html +574 -0
  44. package/docs/classes/AaTree.html +150 -0
  45. package/docs/classes/AbstractEdge.html +269 -0
  46. package/docs/classes/AbstractGraph.html +927 -0
  47. package/docs/classes/AbstractVertex.html +215 -0
  48. package/docs/classes/ArrayDeque.html +417 -0
  49. package/docs/classes/BST.html +2038 -0
  50. package/docs/classes/BSTNode.html +570 -0
  51. package/docs/classes/BTree.html +150 -0
  52. package/docs/classes/BinaryIndexedTree.html +289 -0
  53. package/docs/classes/BinaryTree.html +1827 -0
  54. package/docs/classes/BinaryTreeNode.html +533 -0
  55. package/docs/classes/Character.html +198 -0
  56. package/docs/classes/CoordinateMap.html +469 -0
  57. package/docs/classes/CoordinateSet.html +430 -0
  58. package/docs/classes/Deque.html +767 -0
  59. package/docs/classes/DirectedEdge.html +354 -0
  60. package/docs/classes/DirectedGraph.html +1243 -0
  61. package/docs/classes/DirectedVertex.html +226 -0
  62. package/docs/classes/DoublyLinkedList.html +733 -0
  63. package/docs/classes/DoublyLinkedListNode.html +258 -0
  64. package/docs/classes/Heap.html +482 -0
  65. package/docs/classes/HeapItem.html +233 -0
  66. package/docs/classes/Matrix2D.html +480 -0
  67. package/docs/classes/MatrixNTI2D.html +218 -0
  68. package/docs/classes/MaxHeap.html +500 -0
  69. package/docs/classes/MaxPriorityQueue.html +809 -0
  70. package/docs/classes/MinHeap.html +501 -0
  71. package/docs/classes/MinPriorityQueue.html +811 -0
  72. package/docs/classes/Navigator.html +291 -0
  73. package/docs/classes/ObjectDeque.html +423 -0
  74. package/docs/classes/PriorityQueue.html +733 -0
  75. package/docs/classes/Queue.html +370 -0
  76. package/docs/classes/RBTree.html +150 -0
  77. package/docs/classes/SegmentTree.html +345 -0
  78. package/docs/classes/SegmentTreeNode.html +418 -0
  79. package/docs/classes/SinglyLinkedList.html +1105 -0
  80. package/docs/classes/SinglyLinkedListNode.html +375 -0
  81. package/docs/classes/SplayTree.html +150 -0
  82. package/docs/classes/Stack.html +346 -0
  83. package/docs/classes/TreeMultiSet.html +2036 -0
  84. package/docs/classes/TreeNode.html +236 -0
  85. package/docs/classes/Trie.html +350 -0
  86. package/docs/classes/TrieNode.html +258 -0
  87. package/docs/classes/TwoThreeTree.html +150 -0
  88. package/docs/classes/UndirectedEdge.html +313 -0
  89. package/docs/classes/UndirectedGraph.html +1080 -0
  90. package/docs/classes/UndirectedVertex.html +226 -0
  91. package/docs/classes/Vector2D.html +783 -0
  92. package/docs/enums/CP.html +159 -0
  93. package/docs/enums/FamilyPosition.html +159 -0
  94. package/docs/enums/LoopType.html +160 -0
  95. package/docs/index.html +494 -0
  96. package/docs/interfaces/AVLTreeDeleted.html +161 -0
  97. package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
  98. package/docs/interfaces/HeapOptions.html +167 -0
  99. package/docs/interfaces/IDirectedGraph.html +243 -0
  100. package/docs/interfaces/IGraph.html +427 -0
  101. package/docs/interfaces/NavigatorParams.html +197 -0
  102. package/docs/interfaces/PriorityQueueOptions.html +168 -0
  103. package/docs/modules.html +218 -0
  104. package/docs/types/BSTComparator.html +140 -0
  105. package/docs/types/BSTDeletedResult.html +137 -0
  106. package/docs/types/BinaryTreeDeleted.html +137 -0
  107. package/docs/types/BinaryTreeNodeId.html +125 -0
  108. package/docs/types/BinaryTreeNodePropertyName.html +125 -0
  109. package/docs/types/DFSOrderPattern.html +125 -0
  110. package/docs/types/DijkstraResult.html +145 -0
  111. package/docs/types/Direction.html +125 -0
  112. package/docs/types/DoublyLinkedListGetBy.html +125 -0
  113. package/docs/types/NodeOrPropertyName.html +125 -0
  114. package/docs/types/PriorityQueueComparator.html +145 -0
  115. package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
  116. package/docs/types/ResultByProperty.html +130 -0
  117. package/docs/types/ResultsByProperty.html +130 -0
  118. package/docs/types/SegmentTreeNodeVal.html +125 -0
  119. package/docs/types/SpecifyOptional.html +132 -0
  120. package/docs/types/Thunk.html +133 -0
  121. package/docs/types/ToThunkFn.html +133 -0
  122. package/docs/types/TopologicalStatus.html +125 -0
  123. package/docs/types/TreeMultiSetDeletedResult.html +137 -0
  124. package/docs/types/TrlAsyncFn.html +138 -0
  125. package/docs/types/TrlFn.html +138 -0
  126. package/docs/types/Turning.html +125 -0
  127. package/docs/types/VertexId.html +125 -0
  128. package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +11 -15
  129. package/notes/note.md +23 -0
  130. package/package.json +2 -4
  131. package/.idea/data-structure-typed.iml +0 -14
  132. package/.idea/modules.xml +0 -8
  133. package/.idea/vcs.xml +0 -6
  134. package/src/assets/complexities-diff.jpg +0 -0
  135. package/src/assets/data-structure-complexities.jpg +0 -0
  136. package/src/assets/logo.png +0 -0
  137. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  138. package/src/data-structures/binary-tree/avl-tree.ts +0 -293
  139. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  140. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
  141. package/src/data-structures/binary-tree/binary-tree.ts +0 -1485
  142. package/src/data-structures/binary-tree/bst.ts +0 -497
  143. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  144. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  145. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  146. package/src/data-structures/binary-tree/index.ts +0 -11
  147. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  148. package/src/data-structures/binary-tree/segment-tree.ts +0 -248
  149. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  150. package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
  151. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  152. package/src/data-structures/diagrams/README.md +0 -5
  153. package/src/data-structures/graph/abstract-graph.ts +0 -956
  154. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  155. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  156. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  157. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  158. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  159. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  160. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  161. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  162. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  163. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  164. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  165. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  166. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  167. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  168. package/src/data-structures/graph/directed-graph.ts +0 -424
  169. package/src/data-structures/graph/index.ts +0 -3
  170. package/src/data-structures/graph/undirected-graph.ts +0 -254
  171. package/src/data-structures/hash/coordinate-map.ts +0 -71
  172. package/src/data-structures/hash/coordinate-set.ts +0 -60
  173. package/src/data-structures/hash/hash-table.ts +0 -1
  174. package/src/data-structures/hash/index.ts +0 -6
  175. package/src/data-structures/hash/pair.ts +0 -1
  176. package/src/data-structures/hash/tree-map.ts +0 -1
  177. package/src/data-structures/hash/tree-set.ts +0 -1
  178. package/src/data-structures/heap/heap.ts +0 -156
  179. package/src/data-structures/heap/index.ts +0 -3
  180. package/src/data-structures/heap/max-heap.ts +0 -31
  181. package/src/data-structures/heap/min-heap.ts +0 -34
  182. package/src/data-structures/index.ts +0 -13
  183. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -351
  184. package/src/data-structures/linked-list/index.ts +0 -2
  185. package/src/data-structures/linked-list/singly-linked-list.ts +0 -748
  186. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  187. package/src/data-structures/matrix/index.ts +0 -4
  188. package/src/data-structures/matrix/matrix.ts +0 -27
  189. package/src/data-structures/matrix/matrix2d.ts +0 -208
  190. package/src/data-structures/matrix/navigator.ts +0 -122
  191. package/src/data-structures/matrix/vector2d.ts +0 -316
  192. package/src/data-structures/priority-queue/index.ts +0 -3
  193. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
  194. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
  195. package/src/data-structures/priority-queue/priority-queue.ts +0 -346
  196. package/src/data-structures/queue/deque.ts +0 -241
  197. package/src/data-structures/queue/index.ts +0 -2
  198. package/src/data-structures/queue/queue.ts +0 -120
  199. package/src/data-structures/stack/index.ts +0 -1
  200. package/src/data-structures/stack/stack.ts +0 -98
  201. package/src/data-structures/trie/index.ts +0 -1
  202. package/src/data-structures/trie/trie.ts +0 -225
  203. package/src/data-structures/types/abstract-graph.ts +0 -51
  204. package/src/data-structures/types/avl-tree.ts +0 -6
  205. package/src/data-structures/types/binary-tree.ts +0 -15
  206. package/src/data-structures/types/bst.ts +0 -5
  207. package/src/data-structures/types/directed-graph.ts +0 -18
  208. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  209. package/src/data-structures/types/heap.ts +0 -8
  210. package/src/data-structures/types/index.ts +0 -13
  211. package/src/data-structures/types/navigator.ts +0 -13
  212. package/src/data-structures/types/priority-queue.ts +0 -9
  213. package/src/data-structures/types/segment-tree.ts +0 -1
  214. package/src/data-structures/types/singly-linked-list.ts +0 -1
  215. package/src/data-structures/types/tree-multiset.ts +0 -3
  216. package/src/index.ts +0 -1
  217. package/src/utils/index.ts +0 -2
  218. package/src/utils/types/index.ts +0 -1
  219. package/src/utils/types/utils.ts +0 -176
  220. package/src/utils/utils.ts +0 -290
  221. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  222. package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -495
  223. package/tests/unit/data-structures/graph/index.ts +0 -3
  224. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
@@ -1,18 +1,15 @@
1
- import {BST, BSTNode} from '../../../../src';
1
+ import {BST, BSTNode} from '../../../src';
2
2
 
3
- describe('bst-case6', () => {
3
+ describe('testBSTCase6', () => {
4
4
  it('should perform various operations on a Binary Search Tree', () => {
5
5
  const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
6
-
6
+ const clonedData = [...arr];
7
7
  const tree = new BST();
8
8
 
9
- expect(tree).toBeInstanceOf(BST);
10
-
11
- tree.addMany(arr);
9
+ for (const i of clonedData) {
10
+ tree.put(i, i);
11
+ }
12
12
 
13
- expect(tree.root).toBeInstanceOf(BSTNode);
14
- if (tree.root) expect(tree.root.id).toBe(11);
15
- expect(tree.count).toBe(16);
16
13
  expect(tree.has(6)).toBe(true);
17
14
 
18
15
  const node6 = tree.get(6);
@@ -37,15 +34,13 @@ describe('bst-case6', () => {
37
34
  const lesserSum = tree.lesserSum(10);
38
35
  expect(lesserSum).toBe(45);
39
36
 
40
- expect(node15).toBeInstanceOf(BSTNode);
41
- if (node15 instanceof BSTNode) {
37
+ if (node15) {
42
38
  const subTreeAdd = tree.subTreeAdd(node15, 1, 'count');
43
39
  expect(subTreeAdd).toBeDefined();
44
40
  }
45
-
41
+ expect(node15).toBeDefined();
46
42
 
47
43
  const node11 = tree.get(11);
48
- expect(node11).toBeInstanceOf(BSTNode);
49
44
  if (node11 instanceof BSTNode) {
50
45
  const allGreaterNodesAdd = tree.allGreaterNodesAdd(node11, 2, 'count');
51
46
  expect(allGreaterNodesAdd).toBeDefined();
@@ -61,11 +56,11 @@ describe('bst-case6', () => {
61
56
  expect(bfs[0].id).toBe(8);
62
57
  expect(bfs[bfs.length - 1].id).toBe(16);
63
58
 
64
- const removed11 = tree.remove(11, true);
59
+ const removed11 = tree.remove(1, true);
65
60
  expect(removed11).toBeInstanceOf(Array);
66
61
  expect(removed11[0]).toBeDefined();
67
62
  expect(removed11[0].deleted).toBeDefined();
68
- if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
63
+ if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(1);
69
64
  expect(tree.isAVLBalanced()).toBe(true);
70
65
  expect(node15 && tree.getHeight(node15)).toBe(2);
71
66
 
@@ -103,6 +98,7 @@ describe('bst-case6', () => {
103
98
  expect(tree.isAVLBalanced()).toBe(true);
104
99
  expect(tree.getHeight()).toBe(3);
105
100
 
101
+
106
102
  const removed5 = tree.remove(5, true);
107
103
  expect(removed5).toBeInstanceOf(Array);
108
104
  expect(removed5[0]).toBeDefined();
package/notes/note.md ADDED
@@ -0,0 +1,23 @@
1
+ <table>
2
+ <tbody><tr>
3
+ <th><p>S.No</p></th>
4
+ <th><p>add() function</p></th>
5
+ <th><p>offer() method</p></th>
6
+ </tr>
7
+ <tr>
8
+ <td><p>1</p></td>
9
+ <td><p>add() function throws an IllegalState exception when you try to insert an element in a full queue.</p></td>
10
+ <td><p>It does not throw any exception but returns false when the queue is full or reaches its maximum size.</p></td>
11
+ </tr>
12
+ <tr>
13
+ <td><p>2</p></td>
14
+ <td><p>On successful inserting a queue element, add() method returns true. It does not return False</p></td>
15
+ <td><p>offer() method returns True on successfully inserting an element and returns False when it fails to insert a Queue element.</p></td>
16
+ </tr>
17
+ <tr>
18
+ <td><p>3</p></td>
19
+ <td><p>It belongs to the Collection framework.</p></td>
20
+ <td><p>It is a queue method.</p></td>
21
+ </tr>
22
+ </tbody>
23
+ </table>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.15.1",
3
+ "version": "1.16.1",
4
4
  "description": "Explore our comprehensive Javascript Data Structure / TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  "test": "jest",
9
9
  "build:docs": "typedoc --out docs ./src",
10
10
  "deps:check": "dependency-cruiser src",
11
- "build:publish": "npm run test && npm run build && npm run build:docs && npm run publish"
11
+ "build:publish": "npm run test && npm run build && npm run build:docs && npm publish"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -49,7 +49,6 @@
49
49
  "types": "dist/index.d.ts",
50
50
  "devDependencies": {
51
51
  "@types/jest": "^29.5.3",
52
- "@types/lodash": "^4.14.197",
53
52
  "@types/node": "^20.4.9",
54
53
  "dependency-cruiser": "^13.1.2",
55
54
  "jest": "^29.6.2",
@@ -58,6 +57,5 @@
58
57
  "typescript": "^4.9.5"
59
58
  },
60
59
  "dependencies": {
61
- "lodash": "^4.17.21"
62
60
  }
63
61
  }
@@ -1,14 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/temp" />
6
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- <excludeFolder url="file://$MODULE_DIR$/dist" />
9
- <excludeFolder url="file://$MODULE_DIR$/notes" />
10
- </content>
11
- <orderEntry type="inheritedJdk" />
12
- <orderEntry type="sourceFolder" forTests="false" />
13
- </component>
14
- </module>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/data-structure-typed.iml" filepath="$PROJECT_DIR$/.idea/data-structure-typed.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>
Binary file
Binary file
@@ -1,3 +0,0 @@
1
- export class AaTree {
2
-
3
- }
@@ -1,293 +0,0 @@
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 {AVLTreeDeleted, BinaryTreeNodeId} from '../types';
10
-
11
- export class AVLTreeNode<T> extends BSTNode<T> {
12
- /**
13
- * The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
14
- * id, value, and count.
15
- * @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
16
- * the current instance.
17
- */
18
- override clone(): AVLTreeNode<T> {
19
- return new AVLTreeNode<T>(this.id, this.val, this.count);
20
- }
21
- }
22
-
23
- export class AVLTree<T> extends BST<T> {
24
-
25
- override createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode<T> {
26
- return new AVLTreeNode<T>(id, val, count);
27
- }
28
-
29
- /**
30
- * The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
31
- * balances the tree.
32
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
33
- * update in the AVL tree.
34
- * @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
35
- * `id`. It can be of type `T` (the generic type) or `null`.
36
- * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
37
- * of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
38
- * to `1`, indicating that the value should be inserted once.
39
- * @returns The method is returning either an AVLTreeNode<T> object or null.
40
- */
41
- override add(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null {
42
- const inserted = super.add(id, val, count);
43
- if (inserted) this.balancePath(inserted);
44
- return inserted;
45
- }
46
-
47
- /**
48
- * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
49
- * then balances the tree if necessary.
50
- * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
51
- * removed from the AVL tree.
52
- * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
53
- * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
54
- * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
55
- * @returns The method is returning an array of `AVLTreeDeleted<T>` objects.
56
- */
57
- override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<T>[] {
58
- const deletedResults = super.remove(id, isUpdateAllLeftSum);
59
- for (const {needBalanced} of deletedResults) {
60
- if (needBalanced) {
61
- this.balancePath(needBalanced);
62
- }
63
- }
64
- return deletedResults;
65
- }
66
-
67
- /**
68
- * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
69
- * height of its right subtree.
70
- * @param node - The parameter "node" is of type AVLTreeNode<T>, which represents a node in an AVL tree.
71
- * @returns The balance factor of the given AVL tree node.
72
- */
73
- balanceFactor(node: AVLTreeNode<T>): number {
74
- if (!node.right) // node has no right subtree
75
- return -node.height;
76
- else if (!node.left) // node has no left subtree
77
- return +node.height;
78
- else
79
- return node.right.height - node.left.height;
80
- }
81
-
82
- /**
83
- * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
84
- * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
85
- */
86
- updateHeight(node: AVLTreeNode<T>): void {
87
- if (!node.left && !node.right) // node is a leaf
88
- node.height = 0;
89
- else if (!node.left) {
90
- // node has no left subtree
91
- const rightHeight = node.right ? node.right.height : 0;
92
- node.height = 1 + rightHeight;
93
- } else if (!node.right) // node has no right subtree
94
- node.height = 1 + node.left.height;
95
- else
96
- node.height = 1 + Math.max(node.right.height, node.left.height);
97
- }
98
-
99
- /**
100
- * The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
101
- * each node in the path from the given node to the root.
102
- * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
103
- */
104
- balancePath(node: AVLTreeNode<T>): void {
105
- const path = this.getPathToRoot(node);
106
- for (let i = path.length - 1; i >= 0; i--) {
107
- const A = path[i];
108
- this.updateHeight(A);
109
- switch (this.balanceFactor(A)) {
110
- case -2:
111
- if (A && A.left) {
112
- if (this.balanceFactor(A.left) <= 0) {
113
- this.balanceLL(A); // Perform LL rotation
114
- } else {
115
- this.balanceLR(A); // Perform LR rotation
116
- }
117
- }
118
- break;
119
- case +2:
120
- if (A && A.right) {
121
- if (this.balanceFactor(A.right) >= 0) {
122
- this.balanceRR(A); // Perform RR rotation
123
- } else {
124
- this.balanceRL(A); // Perform RL rotation
125
- }
126
- }
127
- }
128
- }
129
- }
130
-
131
- /**
132
- * The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
133
- * @param A - The parameter A is an AVLTreeNode object.
134
- */
135
- balanceLL(A: AVLTreeNode<T>): void {
136
- const parentOfA = A.parent;
137
- const B = A.left; // A is left-heavy and B is left-heavy
138
- A.parent = B;
139
- if (B && B.right) {
140
- B.right.parent = A;
141
- }
142
- if (B) B.parent = parentOfA;
143
- if (A === this.root) {
144
- if (B) this.root = B;
145
- } else {
146
- if (parentOfA?.left === A) {
147
- parentOfA.left = B;
148
- } else {
149
- if (parentOfA) parentOfA.right = B;
150
- }
151
- }
152
-
153
- if (B) {
154
- A.left = B.right; // Make T2 the left subtree of A
155
- B.right = A; // Make A the left child of B
156
- }
157
- this.updateHeight(A);
158
- if (B) this.updateHeight(B);
159
- }
160
-
161
- /**
162
- * The `balanceLR` function performs a left-right rotation to balance an AVL tree.
163
- * @param A - A is an AVLTreeNode object.
164
- */
165
- balanceLR(A: AVLTreeNode<T>): void {
166
- const parentOfA = A.parent;
167
- const B = A.left; // A is left-heavy
168
- let C = null;
169
- if (B) {
170
- C = B.right;// B is right-heavy
171
- }
172
- if (A) A.parent = C;
173
- if (B) B.parent = C;
174
-
175
- if (C) {
176
- if (C.left) {
177
- C.left.parent = B;
178
- }
179
- if (C.right) {
180
- C.right.parent = A;
181
- }
182
- C.parent = parentOfA;
183
- }
184
-
185
- if (A === this.root) {
186
- if (C) this.root = C;
187
- } else {
188
- if (parentOfA) {
189
- if (parentOfA.left === A) {
190
- parentOfA.left = C;
191
- } else {
192
- parentOfA.right = C;
193
- }
194
- }
195
- }
196
-
197
- if (C) {
198
- A.left = C.right; // Make T3 the left subtree of A
199
- if (B) B.right = C.left; // Make T2 the right subtree of B
200
- C.left = B;
201
- C.right = A;
202
- }
203
-
204
- this.updateHeight(A); // Adjust heights
205
- B && this.updateHeight(B);
206
- C && this.updateHeight(C);
207
- }
208
-
209
- /**
210
- * The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
211
- * @param A - The parameter A is an AVLTreeNode object.
212
- */
213
- balanceRR(A: AVLTreeNode<T>): void {
214
- const parentOfA = A.parent;
215
- const B = A.right; // A is right-heavy and B is right-heavy
216
- A.parent = B;
217
- if (B) {
218
- if (B.left) {
219
- B.left.parent = A;
220
- }
221
- B.parent = parentOfA;
222
- }
223
-
224
- if (A === this.root) {
225
- if (B) this.root = B;
226
- } else {
227
- if (parentOfA) {
228
- if (parentOfA.left === A) {
229
- parentOfA.left = B;
230
- } else {
231
- parentOfA.right = B;
232
- }
233
- }
234
- }
235
-
236
- if (B) {
237
- A.right = B.left; // Make T2 the right subtree of A
238
- B.left = A;
239
- }
240
- this.updateHeight(A);
241
- B && this.updateHeight(B);
242
- }
243
-
244
- /**
245
- * The `balanceRL` function performs a right-left rotation to balance an AVL tree.
246
- * @param A - A is an AVLTreeNode object.
247
- */
248
- balanceRL(A: AVLTreeNode<T>): void {
249
- const parentOfA = A.parent;
250
- const B = A.right; // A is right-heavy
251
- let C = null;
252
- if (B) {
253
- C = B.left; // B is left-heavy
254
- }
255
-
256
- A.parent = C;
257
- if (B) B.parent = C;
258
-
259
- if (C) {
260
- if (C.left) {
261
- C.left.parent = A;
262
- }
263
- if (C.right) {
264
- C.right.parent = B;
265
- }
266
- C.parent = parentOfA;
267
- }
268
-
269
-
270
- if (A === this.root) {
271
- if (C) this.root = C;
272
- } else {
273
- if (parentOfA) {
274
- if (parentOfA.left === A) {
275
- parentOfA.left = C;
276
- } else {
277
- parentOfA.right = C;
278
- }
279
- }
280
- }
281
-
282
- if (C) A.right = C.left; // Make T2 the right subtree of A
283
- if (B && C) B.left = C.right; // Make T3 the left subtree of B
284
- if (C) C.left = A;
285
- if (C) C.right = B;
286
-
287
- this.updateHeight(A); // Adjust heights
288
- B && this.updateHeight(B);
289
- C && this.updateHeight(C);
290
- }
291
- }
292
-
293
-
@@ -1,3 +0,0 @@
1
- export class BTree {
2
-
3
- }
@@ -1,69 +0,0 @@
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
- export class BinaryIndexedTree {
9
- private readonly _sumTree: number[];
10
-
11
- /**
12
- * The constructor initializes an array with a specified length and fills it with zeros.
13
- * @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
14
- * sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
15
- * The size of the sum tree array is `n + 1` because
16
- */
17
- constructor(n: number) {
18
- this._sumTree = new Array<number>(n + 1).fill(0);
19
- }
20
-
21
- static lowBit(x: number) {
22
- return x & (-x);
23
- }
24
-
25
- /**
26
- * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
27
- * its ancestors.
28
- * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
29
- * updated.
30
- * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
31
- * at index "i" in the "_sumTree" array.
32
- */
33
- update(i: number, delta: number) {
34
- while (i < this._sumTree.length) {
35
- this._sumTree[i] += delta;
36
- i += BinaryIndexedTree.lowBit(i);
37
- }
38
- }
39
-
40
- /**
41
- * The function calculates the prefix sum of an array using a binary indexed tree.
42
- * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
43
- * array for which we want to calculate the prefix sum.
44
- * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
45
- * `i`.
46
- */
47
- getPrefixSum(i: number) {
48
- let sum = 0;
49
- while (i > 0) {
50
- sum += this._sumTree[i];
51
- i -= BinaryIndexedTree.lowBit(i);
52
- }
53
- return sum;
54
- }
55
-
56
- /**
57
- * The function `getRangeSum` calculates the sum of a range of numbers in an array.
58
- * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
59
- * sum.
60
- * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
61
- * the sum.
62
- * @returns the sum of the elements in the range specified by the start and end indices.
63
- */
64
- public getRangeSum(start: number, end: number): number {
65
- if (!(0 <= start && start <= end && end <= this._sumTree.length))
66
- throw 'Index out of bounds';
67
- return this.getPrefixSum(end) - this.getPrefixSum(start);
68
- }
69
- }