data-structure-typed 1.15.2 → 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 (208) hide show
  1. package/README.md +24 -24
  2. package/dist/data-structures/heap/heap.d.ts +30 -15
  3. package/dist/data-structures/heap/heap.js +60 -15
  4. package/dist/data-structures/heap/max-heap.d.ts +3 -3
  5. package/dist/data-structures/heap/min-heap.d.ts +3 -3
  6. package/dist/data-structures/index.d.ts +1 -0
  7. package/dist/data-structures/index.js +1 -0
  8. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
  9. package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
  10. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
  11. package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
  12. package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
  13. package/dist/data-structures/priority-queue/priority-queue.js +28 -22
  14. package/dist/data-structures/tree/index.d.ts +1 -0
  15. package/dist/data-structures/tree/index.js +17 -0
  16. package/dist/data-structures/tree/tree.d.ts +9 -0
  17. package/dist/data-structures/tree/tree.js +52 -0
  18. package/dist/data-structures/types/heap.d.ts +0 -4
  19. package/dist/utils/types/utils.d.ts +1 -0
  20. package/docs/.nojekyll +1 -0
  21. package/docs/assets/highlight.css +92 -0
  22. package/docs/assets/main.js +58 -0
  23. package/docs/assets/search.js +1 -0
  24. package/docs/assets/style.css +1367 -0
  25. package/docs/classes/AVLTree.html +2192 -0
  26. package/docs/classes/AVLTreeNode.html +574 -0
  27. package/docs/classes/AaTree.html +150 -0
  28. package/docs/classes/AbstractEdge.html +269 -0
  29. package/docs/classes/AbstractGraph.html +927 -0
  30. package/docs/classes/AbstractVertex.html +215 -0
  31. package/docs/classes/ArrayDeque.html +417 -0
  32. package/docs/classes/BST.html +2038 -0
  33. package/docs/classes/BSTNode.html +570 -0
  34. package/docs/classes/BTree.html +150 -0
  35. package/docs/classes/BinaryIndexedTree.html +289 -0
  36. package/docs/classes/BinaryTree.html +1827 -0
  37. package/docs/classes/BinaryTreeNode.html +533 -0
  38. package/docs/classes/Character.html +198 -0
  39. package/docs/classes/CoordinateMap.html +469 -0
  40. package/docs/classes/CoordinateSet.html +430 -0
  41. package/docs/classes/Deque.html +767 -0
  42. package/docs/classes/DirectedEdge.html +354 -0
  43. package/docs/classes/DirectedGraph.html +1243 -0
  44. package/docs/classes/DirectedVertex.html +226 -0
  45. package/docs/classes/DoublyLinkedList.html +733 -0
  46. package/docs/classes/DoublyLinkedListNode.html +258 -0
  47. package/docs/classes/Heap.html +482 -0
  48. package/docs/classes/HeapItem.html +233 -0
  49. package/docs/classes/Matrix2D.html +480 -0
  50. package/docs/classes/MatrixNTI2D.html +218 -0
  51. package/docs/classes/MaxHeap.html +500 -0
  52. package/docs/classes/MaxPriorityQueue.html +809 -0
  53. package/docs/classes/MinHeap.html +501 -0
  54. package/docs/classes/MinPriorityQueue.html +811 -0
  55. package/docs/classes/Navigator.html +291 -0
  56. package/docs/classes/ObjectDeque.html +423 -0
  57. package/docs/classes/PriorityQueue.html +733 -0
  58. package/docs/classes/Queue.html +370 -0
  59. package/docs/classes/RBTree.html +150 -0
  60. package/docs/classes/SegmentTree.html +345 -0
  61. package/docs/classes/SegmentTreeNode.html +418 -0
  62. package/docs/classes/SinglyLinkedList.html +1105 -0
  63. package/docs/classes/SinglyLinkedListNode.html +375 -0
  64. package/docs/classes/SplayTree.html +150 -0
  65. package/docs/classes/Stack.html +346 -0
  66. package/docs/classes/TreeMultiSet.html +2036 -0
  67. package/docs/classes/TreeNode.html +236 -0
  68. package/docs/classes/Trie.html +350 -0
  69. package/docs/classes/TrieNode.html +258 -0
  70. package/docs/classes/TwoThreeTree.html +150 -0
  71. package/docs/classes/UndirectedEdge.html +313 -0
  72. package/docs/classes/UndirectedGraph.html +1080 -0
  73. package/docs/classes/UndirectedVertex.html +226 -0
  74. package/docs/classes/Vector2D.html +783 -0
  75. package/docs/enums/CP.html +159 -0
  76. package/docs/enums/FamilyPosition.html +159 -0
  77. package/docs/enums/LoopType.html +160 -0
  78. package/docs/index.html +494 -0
  79. package/docs/interfaces/AVLTreeDeleted.html +161 -0
  80. package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
  81. package/docs/interfaces/HeapOptions.html +167 -0
  82. package/docs/interfaces/IDirectedGraph.html +243 -0
  83. package/docs/interfaces/IGraph.html +427 -0
  84. package/docs/interfaces/NavigatorParams.html +197 -0
  85. package/docs/interfaces/PriorityQueueOptions.html +168 -0
  86. package/docs/modules.html +218 -0
  87. package/docs/types/BSTComparator.html +140 -0
  88. package/docs/types/BSTDeletedResult.html +137 -0
  89. package/docs/types/BinaryTreeDeleted.html +137 -0
  90. package/docs/types/BinaryTreeNodeId.html +125 -0
  91. package/docs/types/BinaryTreeNodePropertyName.html +125 -0
  92. package/docs/types/DFSOrderPattern.html +125 -0
  93. package/docs/types/DijkstraResult.html +145 -0
  94. package/docs/types/Direction.html +125 -0
  95. package/docs/types/DoublyLinkedListGetBy.html +125 -0
  96. package/docs/types/NodeOrPropertyName.html +125 -0
  97. package/docs/types/PriorityQueueComparator.html +145 -0
  98. package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
  99. package/docs/types/ResultByProperty.html +130 -0
  100. package/docs/types/ResultsByProperty.html +130 -0
  101. package/docs/types/SegmentTreeNodeVal.html +125 -0
  102. package/docs/types/SpecifyOptional.html +132 -0
  103. package/docs/types/Thunk.html +133 -0
  104. package/docs/types/ToThunkFn.html +133 -0
  105. package/docs/types/TopologicalStatus.html +125 -0
  106. package/docs/types/TreeMultiSetDeletedResult.html +137 -0
  107. package/docs/types/TrlAsyncFn.html +138 -0
  108. package/docs/types/TrlFn.html +138 -0
  109. package/docs/types/Turning.html +125 -0
  110. package/docs/types/VertexId.html +125 -0
  111. package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
  112. package/notes/note.md +23 -0
  113. package/package.json +1 -1
  114. package/.idea/data-structure-typed.iml +0 -14
  115. package/.idea/modules.xml +0 -8
  116. package/.idea/vcs.xml +0 -6
  117. package/src/assets/complexities-diff.jpg +0 -0
  118. package/src/assets/data-structure-complexities.jpg +0 -0
  119. package/src/assets/logo.png +0 -0
  120. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  121. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  122. package/src/data-structures/binary-tree/avl-tree.ts +0 -293
  123. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  124. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
  125. package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
  126. package/src/data-structures/binary-tree/bst.ts +0 -497
  127. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  128. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  129. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  130. package/src/data-structures/binary-tree/index.ts +0 -11
  131. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  132. package/src/data-structures/binary-tree/segment-tree.ts +0 -267
  133. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  134. package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
  135. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  136. package/src/data-structures/diagrams/README.md +0 -5
  137. package/src/data-structures/graph/abstract-graph.ts +0 -958
  138. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  139. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  140. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  141. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  142. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  143. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  148. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  149. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  150. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  151. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  152. package/src/data-structures/graph/directed-graph.ts +0 -429
  153. package/src/data-structures/graph/index.ts +0 -3
  154. package/src/data-structures/graph/undirected-graph.ts +0 -259
  155. package/src/data-structures/hash/coordinate-map.ts +0 -74
  156. package/src/data-structures/hash/coordinate-set.ts +0 -63
  157. package/src/data-structures/hash/hash-table.ts +0 -1
  158. package/src/data-structures/hash/index.ts +0 -6
  159. package/src/data-structures/hash/pair.ts +0 -1
  160. package/src/data-structures/hash/tree-map.ts +0 -1
  161. package/src/data-structures/hash/tree-set.ts +0 -1
  162. package/src/data-structures/heap/heap.ts +0 -162
  163. package/src/data-structures/heap/index.ts +0 -3
  164. package/src/data-structures/heap/max-heap.ts +0 -31
  165. package/src/data-structures/heap/min-heap.ts +0 -34
  166. package/src/data-structures/index.ts +0 -13
  167. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
  168. package/src/data-structures/linked-list/index.ts +0 -2
  169. package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
  170. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  171. package/src/data-structures/matrix/index.ts +0 -4
  172. package/src/data-structures/matrix/matrix.ts +0 -27
  173. package/src/data-structures/matrix/matrix2d.ts +0 -208
  174. package/src/data-structures/matrix/navigator.ts +0 -122
  175. package/src/data-structures/matrix/vector2d.ts +0 -316
  176. package/src/data-structures/priority-queue/index.ts +0 -3
  177. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
  178. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
  179. package/src/data-structures/priority-queue/priority-queue.ts +0 -349
  180. package/src/data-structures/queue/deque.ts +0 -251
  181. package/src/data-structures/queue/index.ts +0 -2
  182. package/src/data-structures/queue/queue.ts +0 -120
  183. package/src/data-structures/stack/index.ts +0 -1
  184. package/src/data-structures/stack/stack.ts +0 -98
  185. package/src/data-structures/trie/index.ts +0 -1
  186. package/src/data-structures/trie/trie.ts +0 -225
  187. package/src/data-structures/types/abstract-graph.ts +0 -51
  188. package/src/data-structures/types/avl-tree.ts +0 -6
  189. package/src/data-structures/types/binary-tree.ts +0 -15
  190. package/src/data-structures/types/bst.ts +0 -5
  191. package/src/data-structures/types/directed-graph.ts +0 -18
  192. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  193. package/src/data-structures/types/heap.ts +0 -8
  194. package/src/data-structures/types/index.ts +0 -13
  195. package/src/data-structures/types/navigator.ts +0 -13
  196. package/src/data-structures/types/priority-queue.ts +0 -9
  197. package/src/data-structures/types/segment-tree.ts +0 -1
  198. package/src/data-structures/types/singly-linked-list.ts +0 -1
  199. package/src/data-structures/types/tree-multiset.ts +0 -3
  200. package/src/index.ts +0 -1
  201. package/src/utils/index.ts +0 -2
  202. package/src/utils/types/index.ts +0 -1
  203. package/src/utils/types/utils.ts +0 -4
  204. package/src/utils/utils.ts +0 -78
  205. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  206. package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
  207. package/tests/unit/data-structures/graph/index.ts +0 -3
  208. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
@@ -1,40 +1,32 @@
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
+ const clonedData = [...arr];
6
7
  const tree = new BST();
7
- expect(tree).toBeInstanceOf(BST);
8
-
9
- const values = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
10
- tree.addMany(values);
11
- expect(tree.root).toBeInstanceOf(BSTNode);
12
8
 
13
- if (tree.root) expect(tree.root.id).toBe(11);
14
-
15
- expect(tree.count).toBe(16);
9
+ for (const i of clonedData) {
10
+ tree.put(i, i);
11
+ }
16
12
 
17
13
  expect(tree.has(6)).toBe(true);
18
14
 
19
15
  const node6 = tree.get(6);
20
16
  expect(node6 && tree.getHeight(node6)).toBe(2);
21
17
  expect(node6 && tree.getDepth(node6)).toBe(3);
18
+ const getNodeById = tree.get(10, 'id');
19
+ expect(getNodeById?.id).toBe(10);
22
20
 
23
- const nodeId10 = tree.get(10, 'id');
24
- expect(nodeId10?.id).toBe(10);
21
+ const getNodesByCount = tree.getNodes(1, 'count');
22
+ expect(getNodesByCount.length).toBe(16);
25
23
 
26
- const nodeVal9 = tree.get(9, 'val');
27
- expect(nodeVal9?.id).toBe(9);
28
-
29
- const nodesByCount1 = tree.getNodes(1, 'count');
30
- expect(nodesByCount1.length).toBe(16);
31
-
32
- const leftMost = tree.getLeftMost();
33
- expect(leftMost?.id).toBe(1);
24
+ const getMinNodeByRoot = tree.getLeftMost();
25
+ expect(getMinNodeByRoot?.id).toBe(1);
34
26
 
35
27
  const node15 = tree.get(15);
36
- const minNodeBySpecificNode = node15 && tree.getLeftMost(node15);
37
- expect(minNodeBySpecificNode?.id).toBe(12);
28
+ const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
29
+ expect(getMinNodeBySpecificNode?.id).toBe(12);
38
30
 
39
31
  const subTreeSum = node15 && tree.subTreeSum(node15);
40
32
  expect(subTreeSum).toBe(70);
@@ -42,39 +34,34 @@ describe('BST Case6', () => {
42
34
  const lesserSum = tree.lesserSum(10);
43
35
  expect(lesserSum).toBe(45);
44
36
 
45
- expect(node15).toBeInstanceOf(BSTNode);
46
- if (node15 instanceof BSTNode) {
37
+ if (node15) {
47
38
  const subTreeAdd = tree.subTreeAdd(node15, 1, 'count');
48
39
  expect(subTreeAdd).toBeDefined();
49
40
  }
41
+ expect(node15).toBeDefined();
50
42
 
51
43
  const node11 = tree.get(11);
52
- expect(node11).toBeInstanceOf(BSTNode);
53
44
  if (node11 instanceof BSTNode) {
54
- const allGreaterNodesAdded = tree.allGreaterNodesAdd(node11, 2, 'count');
55
- expect(allGreaterNodesAdded).toBeDefined();
45
+ const allGreaterNodesAdd = tree.allGreaterNodesAdd(node11, 2, 'count');
46
+ expect(allGreaterNodesAdd).toBeDefined();
56
47
  }
57
48
 
58
- const dfsInorderNodes = tree.DFS('in', 'node');
59
- expect(dfsInorderNodes[0].id).toBe(1);
60
- expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
49
+ const dfs = tree.DFS('in', 'node');
50
+ expect(dfs[0].id).toBe(1);
51
+ expect(dfs[dfs.length - 1].id).toBe(16);
61
52
 
62
53
  tree.balance();
54
+ const bfs = tree.BFS('node');
63
55
  expect(tree.isBalanced()).toBe(true);
56
+ expect(bfs[0].id).toBe(8);
57
+ expect(bfs[bfs.length - 1].id).toBe(16);
64
58
 
65
- const bfsNodesAfterBalanced = tree.BFS('node');
66
- expect(bfsNodesAfterBalanced[0].id).toBe(8);
67
- expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
68
-
69
- const removed11 = tree.remove(11, true);
59
+ const removed11 = tree.remove(1, true);
70
60
  expect(removed11).toBeInstanceOf(Array);
71
61
  expect(removed11[0]).toBeDefined();
72
62
  expect(removed11[0].deleted).toBeDefined();
73
-
74
- if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
75
-
63
+ if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(1);
76
64
  expect(tree.isAVLBalanced()).toBe(true);
77
-
78
65
  expect(node15 && tree.getHeight(node15)).toBe(2);
79
66
 
80
67
  const removed1 = tree.remove(1, true);
@@ -84,7 +71,6 @@ describe('BST Case6', () => {
84
71
  if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
85
72
 
86
73
  expect(tree.isAVLBalanced()).toBe(true);
87
-
88
74
  expect(tree.getHeight()).toBe(4);
89
75
 
90
76
  const removed4 = tree.remove(4, true);
@@ -112,6 +98,7 @@ describe('BST Case6', () => {
112
98
  expect(tree.isAVLBalanced()).toBe(true);
113
99
  expect(tree.getHeight()).toBe(3);
114
100
 
101
+
115
102
  const removed5 = tree.remove(5, true);
116
103
  expect(removed5).toBeInstanceOf(Array);
117
104
  expect(removed5[0]).toBeDefined();
@@ -179,16 +166,16 @@ describe('BST Case6', () => {
179
166
  expect(tree.getHeight()).toBe(2);
180
167
 
181
168
 
182
- expect(tree.isAVLBalanced()).toBe(false);
169
+ expect(!tree.isAVLBalanced()).toBe(true);
183
170
 
184
- const bfsIDs = tree.BFS();
185
- expect(bfsIDs[0]).toBe(2);
186
- expect(bfsIDs[1]).toBe(12);
187
- expect(bfsIDs[2]).toBe(16);
171
+ const lastBFSIds = tree.BFS();
172
+ expect(lastBFSIds[0]).toBe(2);
173
+ expect(lastBFSIds[1]).toBe(12);
174
+ expect(lastBFSIds[2]).toBe(16);
188
175
 
189
- const bfsNodes = tree.BFS('node');
190
- expect(bfsNodes[0].id).toBe(2);
191
- expect(bfsNodes[1].id).toBe(12);
192
- expect(bfsNodes[2].id).toBe(16);
176
+ const lastBFSNodes = tree.BFS('node');
177
+ expect(lastBFSNodes[0].id).toBe(2);
178
+ expect(lastBFSNodes[1].id).toBe(12);
179
+ expect(lastBFSNodes[2].id).toBe(16);
193
180
  });
194
181
  });
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.2",
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": {
@@ -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
- }