data-structure-typed 1.35.1 → 1.36.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 (214) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +21 -0
  4. package/dist/data-structures/binary-tree/abstract-binary-tree.js +13 -864
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
  7. package/dist/data-structures/binary-tree/avl-tree.js +90 -3
  8. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +536 -0
  14. package/dist/data-structures/binary-tree/binary-tree.js +1194 -2
  15. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  16. package/dist/data-structures/binary-tree/bst.d.ts +133 -0
  17. package/dist/data-structures/binary-tree/bst.js +114 -0
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/index.d.ts +12 -0
  20. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  21. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  22. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  23. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  24. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
  28. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  29. package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  30. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  31. package/dist/data-structures/graph/abstract-graph.js +270 -7
  32. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  33. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  34. package/dist/data-structures/graph/directed-graph.js +167 -0
  35. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  36. package/dist/data-structures/graph/index.d.ts +4 -0
  37. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  38. package/dist/data-structures/graph/map-graph.js +54 -0
  39. package/dist/data-structures/graph/map-graph.js.map +1 -1
  40. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  41. package/dist/data-structures/graph/undirected-graph.js +105 -0
  42. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  43. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  44. package/dist/data-structures/hash/coordinate-map.js +35 -0
  45. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  46. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  47. package/dist/data-structures/hash/coordinate-set.js +28 -0
  48. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  49. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  50. package/dist/data-structures/hash/hash-map.js +29 -1
  51. package/dist/data-structures/hash/hash-map.js.map +1 -1
  52. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  53. package/dist/data-structures/hash/hash-table.js +88 -6
  54. package/dist/data-structures/hash/hash-table.js.map +1 -1
  55. package/dist/data-structures/hash/index.d.ts +7 -0
  56. package/dist/data-structures/hash/pair.d.ts +2 -0
  57. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  58. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  59. package/dist/data-structures/heap/heap.d.ts +99 -0
  60. package/dist/data-structures/heap/heap.js +200 -78
  61. package/dist/data-structures/heap/heap.js.map +1 -1
  62. package/dist/data-structures/heap/index.d.ts +3 -0
  63. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  64. package/dist/data-structures/heap/max-heap.js +16 -6
  65. package/dist/data-structures/heap/max-heap.js.map +1 -1
  66. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  67. package/dist/data-structures/heap/min-heap.js +16 -6
  68. package/dist/data-structures/heap/min-heap.js.map +1 -1
  69. package/dist/data-structures/index.d.ts +11 -0
  70. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  71. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
  72. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  73. package/dist/data-structures/linked-list/index.d.ts +3 -0
  74. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  75. package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
  76. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  77. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  78. package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
  79. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  80. package/dist/data-structures/matrix/index.d.ts +4 -0
  81. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  82. package/dist/data-structures/matrix/matrix.js +15 -0
  83. package/dist/data-structures/matrix/matrix.js.map +1 -1
  84. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  85. package/dist/data-structures/matrix/matrix2d.js +91 -2
  86. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  87. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  88. package/dist/data-structures/matrix/navigator.js +28 -0
  89. package/dist/data-structures/matrix/navigator.js.map +1 -1
  90. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  91. package/dist/data-structures/matrix/vector2d.js +188 -1
  92. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  93. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  94. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  95. package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
  96. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  97. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  98. package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
  99. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  100. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  101. package/dist/data-structures/priority-queue/priority-queue.js +11 -174
  102. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  103. package/dist/data-structures/queue/deque.d.ts +165 -0
  104. package/dist/data-structures/queue/deque.js +124 -0
  105. package/dist/data-structures/queue/deque.js.map +1 -1
  106. package/dist/data-structures/queue/index.d.ts +2 -0
  107. package/dist/data-structures/queue/queue.d.ts +107 -0
  108. package/dist/data-structures/queue/queue.js +80 -0
  109. package/dist/data-structures/queue/queue.js.map +1 -1
  110. package/dist/data-structures/stack/index.d.ts +1 -0
  111. package/dist/data-structures/stack/stack.d.ts +63 -0
  112. package/dist/data-structures/stack/stack.js +50 -0
  113. package/dist/data-structures/stack/stack.js.map +1 -1
  114. package/dist/data-structures/tree/index.d.ts +1 -0
  115. package/dist/data-structures/tree/tree.d.ts +14 -0
  116. package/dist/data-structures/tree/tree.js +1 -0
  117. package/dist/data-structures/tree/tree.js.map +1 -1
  118. package/dist/data-structures/trie/index.d.ts +1 -0
  119. package/dist/data-structures/trie/trie.d.ts +61 -0
  120. package/dist/data-structures/trie/trie.js +36 -0
  121. package/dist/data-structures/trie/trie.js.map +1 -1
  122. package/dist/index.d.ts +4 -0
  123. package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
  124. package/dist/interfaces/abstract-graph.d.ts +5 -0
  125. package/dist/interfaces/avl-tree.d.ts +7 -0
  126. package/dist/interfaces/binary-tree.d.ts +6 -0
  127. package/dist/interfaces/bst.d.ts +6 -0
  128. package/dist/interfaces/directed-graph.d.ts +3 -0
  129. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  130. package/dist/interfaces/heap.d.ts +1 -0
  131. package/dist/interfaces/index.d.ts +15 -0
  132. package/dist/interfaces/navigator.d.ts +1 -0
  133. package/dist/interfaces/priority-queue.d.ts +1 -0
  134. package/dist/interfaces/rb-tree.d.ts +6 -0
  135. package/dist/interfaces/segment-tree.d.ts +1 -0
  136. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  137. package/dist/interfaces/tree-multiset.d.ts +6 -0
  138. package/dist/interfaces/undirected-graph.d.ts +3 -0
  139. package/dist/types/data-structures/abstract-binary-tree.d.ts +32 -0
  140. package/dist/types/data-structures/abstract-binary-tree.js +6 -0
  141. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  142. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  143. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  144. package/dist/types/data-structures/binary-tree.d.ts +6 -0
  145. package/dist/types/data-structures/bst.d.ts +13 -0
  146. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  147. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  148. package/dist/types/data-structures/hash.d.ts +1 -0
  149. package/dist/types/data-structures/heap.d.ts +1 -0
  150. package/dist/types/data-structures/index.d.ts +16 -0
  151. package/dist/types/data-structures/map-graph.d.ts +1 -0
  152. package/dist/types/data-structures/navigator.d.ts +14 -0
  153. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  154. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  155. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  156. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  157. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  158. package/dist/types/helpers.d.ts +1 -0
  159. package/dist/types/index.d.ts +3 -0
  160. package/dist/types/utils/index.d.ts +2 -0
  161. package/dist/types/utils/utils.d.ts +7 -0
  162. package/dist/types/utils/validate-type.d.ts +19 -0
  163. package/dist/utils/index.d.ts +1 -0
  164. package/dist/utils/utils.d.ts +19 -0
  165. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +3 -509
  166. package/lib/data-structures/binary-tree/abstract-binary-tree.js +1 -1175
  167. package/lib/data-structures/binary-tree/avl-tree.d.ts +1 -1
  168. package/lib/data-structures/binary-tree/binary-tree.d.ts +506 -1
  169. package/lib/data-structures/binary-tree/binary-tree.js +1173 -2
  170. package/lib/data-structures/binary-tree/bst.d.ts +1 -1
  171. package/lib/data-structures/binary-tree/rb-tree.d.ts +1 -1
  172. package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  173. package/lib/data-structures/graph/abstract-graph.js +3 -5
  174. package/lib/data-structures/heap/heap.d.ts +84 -68
  175. package/lib/data-structures/heap/heap.js +168 -107
  176. package/lib/data-structures/heap/max-heap.d.ts +6 -17
  177. package/lib/data-structures/heap/max-heap.js +11 -17
  178. package/lib/data-structures/heap/min-heap.d.ts +6 -18
  179. package/lib/data-structures/heap/min-heap.js +11 -18
  180. package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -7
  181. package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
  182. package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -7
  183. package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
  184. package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
  185. package/lib/data-structures/priority-queue/priority-queue.js +11 -315
  186. package/lib/types/data-structures/abstract-binary-tree.d.ts +1 -3
  187. package/lib/types/data-structures/binary-tree.d.ts +4 -2
  188. package/lib/types/data-structures/heap.d.ts +1 -3
  189. package/package.json +10 -6
  190. package/src/data-structures/binary-tree/abstract-binary-tree.ts +4 -1527
  191. package/src/data-structures/binary-tree/avl-tree.ts +3 -3
  192. package/src/data-structures/binary-tree/binary-tree.ts +1524 -5
  193. package/src/data-structures/binary-tree/bst.ts +3 -3
  194. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  195. package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
  196. package/src/data-structures/graph/abstract-graph.ts +3 -5
  197. package/src/data-structures/heap/heap.ts +167 -143
  198. package/src/data-structures/heap/max-heap.ts +15 -22
  199. package/src/data-structures/heap/min-heap.ts +15 -23
  200. package/src/data-structures/priority-queue/max-priority-queue.ts +13 -46
  201. package/src/data-structures/priority-queue/min-priority-queue.ts +13 -47
  202. package/src/data-structures/priority-queue/priority-queue.ts +7 -350
  203. package/src/types/data-structures/abstract-binary-tree.ts +1 -1
  204. package/src/types/data-structures/binary-tree.ts +2 -2
  205. package/src/types/data-structures/heap.ts +1 -5
  206. package/test/unit/data-structures/heap/heap.test.ts +26 -18
  207. package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
  208. package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
  209. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +9 -10
  210. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
  211. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +14 -30
  212. package/umd/bundle.min.js +1 -1
  213. package/umd/bundle.min.js.LICENSE.txt +15 -0
  214. package/umd/bundle.min.js.map +1 -1
@@ -1,876 +1,25 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.AbstractBinaryTree = exports.AbstractBinaryTreeNode = void 0;
4
- const utils_1 = require("../../utils");
5
- const types_1 = require("../../types");
6
11
  class AbstractBinaryTreeNode {
7
- constructor(key, val) {
8
- this.key = key;
12
+ /**
13
+ * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
14
+ * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
15
+ * stored in the binary tree node. If no value is provided, it will be set to undefined.
16
+ */
17
+ constructor(val) {
9
18
  this.val = val;
10
19
  }
11
- get left() {
12
- return this._left;
13
- }
14
- set left(v) {
15
- if (v) {
16
- v.parent = this;
17
- }
18
- this._left = v;
19
- }
20
- get right() {
21
- return this._right;
22
- }
23
- set right(v) {
24
- if (v) {
25
- v.parent = this;
26
- }
27
- this._right = v;
28
- }
29
- get familyPosition() {
30
- const that = this;
31
- if (that.parent) {
32
- if (that.parent.left === that) {
33
- if (that.left || that.right) {
34
- return types_1.FamilyPosition.ROOT_LEFT;
35
- }
36
- else {
37
- return types_1.FamilyPosition.LEFT;
38
- }
39
- }
40
- else if (that.parent.right === that) {
41
- if (that.left || that.right) {
42
- return types_1.FamilyPosition.ROOT_RIGHT;
43
- }
44
- else {
45
- return types_1.FamilyPosition.RIGHT;
46
- }
47
- }
48
- else {
49
- return types_1.FamilyPosition.MAL_NODE;
50
- }
51
- }
52
- else {
53
- if (that.left || that.right) {
54
- return types_1.FamilyPosition.ROOT;
55
- }
56
- else {
57
- return types_1.FamilyPosition.ISOLATED;
58
- }
59
- }
60
- }
61
20
  }
62
21
  exports.AbstractBinaryTreeNode = AbstractBinaryTreeNode;
63
22
  class AbstractBinaryTree {
64
- constructor(options) {
65
- this._root = null;
66
- this._size = 0;
67
- this._loopType = types_1.LoopType.ITERATIVE;
68
- this.visitedKey = [];
69
- this.visitedVal = [];
70
- this.visitedNode = [];
71
- if (options !== undefined) {
72
- const { loopType = types_1.LoopType.ITERATIVE } = options;
73
- this._loopType = loopType;
74
- }
75
- this.clear();
76
- }
77
- get root() {
78
- return this._root;
79
- }
80
- get size() {
81
- return this._size;
82
- }
83
- get loopType() {
84
- return this._loopType;
85
- }
86
- swapLocation(srcNode, destNode) {
87
- const { key, val } = destNode;
88
- const tempNode = this.createNode(key, val);
89
- if (tempNode) {
90
- destNode.key = srcNode.key;
91
- destNode.val = srcNode.val;
92
- srcNode.key = tempNode.key;
93
- srcNode.val = tempNode.val;
94
- }
95
- return destNode;
96
- }
97
- clear() {
98
- this._root = null;
99
- this._size = 0;
100
- this._clearResults();
101
- }
102
- isEmpty() {
103
- return this.size === 0;
104
- }
105
- add(keyOrNode, val) {
106
- const _bfs = (root, newNode) => {
107
- const queue = [root];
108
- while (queue.length > 0) {
109
- const cur = queue.shift();
110
- if (cur) {
111
- if (newNode && cur.key === newNode.key)
112
- return;
113
- const inserted = this._addTo(newNode, cur);
114
- if (inserted !== undefined)
115
- return inserted;
116
- if (cur.left)
117
- queue.push(cur.left);
118
- if (cur.right)
119
- queue.push(cur.right);
120
- }
121
- else
122
- return;
123
- }
124
- return;
125
- };
126
- let inserted, needInsert;
127
- if (keyOrNode === null) {
128
- needInsert = null;
129
- }
130
- else if (typeof keyOrNode === 'number') {
131
- needInsert = this.createNode(keyOrNode, val);
132
- }
133
- else if (keyOrNode instanceof AbstractBinaryTreeNode) {
134
- needInsert = keyOrNode;
135
- }
136
- else {
137
- return;
138
- }
139
- const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
140
- if (this.root) {
141
- if (existNode) {
142
- existNode.val = val;
143
- inserted = existNode;
144
- }
145
- else {
146
- inserted = _bfs(this.root, needInsert);
147
- }
148
- }
149
- else {
150
- this._setRoot(needInsert);
151
- if (needInsert !== null) {
152
- this._setSize(1);
153
- }
154
- else {
155
- this._setSize(0);
156
- }
157
- inserted = this.root;
158
- }
159
- return inserted;
160
- }
161
- addMany(keysOrNodes, data) {
162
- const inserted = [];
163
- for (let i = 0; i < keysOrNodes.length; i++) {
164
- const keyOrNode = keysOrNodes[i];
165
- if (keyOrNode instanceof AbstractBinaryTreeNode) {
166
- inserted.push(this.add(keyOrNode.key, keyOrNode.val));
167
- continue;
168
- }
169
- if (keyOrNode === null) {
170
- inserted.push(this.add(null));
171
- continue;
172
- }
173
- const val = data === null || data === void 0 ? void 0 : data[i];
174
- inserted.push(this.add(keyOrNode, val));
175
- }
176
- return inserted;
177
- }
178
- refill(keysOrNodes, data) {
179
- this.clear();
180
- return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
181
- }
182
- remove(nodeOrKey) {
183
- const bstDeletedResult = [];
184
- if (!this.root)
185
- return bstDeletedResult;
186
- const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
187
- if (!curr)
188
- return bstDeletedResult;
189
- const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
190
- let needBalanced = null, orgCurrent = curr;
191
- if (!curr.left) {
192
- if (!parent) {
193
- if (curr.right !== undefined)
194
- this._setRoot(curr.right);
195
- }
196
- else {
197
- const { familyPosition: fp } = curr;
198
- if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
199
- parent.left = curr.right;
200
- }
201
- else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
202
- parent.right = curr.right;
203
- }
204
- needBalanced = parent;
205
- }
206
- }
207
- else {
208
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
209
- if (leftSubTreeRightMost) {
210
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
211
- orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
212
- if (parentOfLeftSubTreeMax) {
213
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
214
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
215
- else
216
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
217
- needBalanced = parentOfLeftSubTreeMax;
218
- }
219
- }
220
- }
221
- this._setSize(this.size - 1);
222
- bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
223
- return bstDeletedResult;
224
- }
225
- getDepth(beginRoot = this.root) {
226
- if (typeof beginRoot === 'number')
227
- beginRoot = this.get(beginRoot, 'key');
228
- let depth = 0;
229
- while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
230
- depth++;
231
- beginRoot = beginRoot.parent;
232
- }
233
- return depth;
234
- }
235
- getHeight(beginRoot = this.root) {
236
- if (typeof beginRoot === 'number')
237
- beginRoot = this.get(beginRoot, 'key');
238
- if (!beginRoot)
239
- return -1;
240
- if (this._loopType === types_1.LoopType.RECURSIVE) {
241
- const _getMaxHeight = (cur) => {
242
- if (!cur)
243
- return -1;
244
- const leftHeight = _getMaxHeight(cur.left);
245
- const rightHeight = _getMaxHeight(cur.right);
246
- return Math.max(leftHeight, rightHeight) + 1;
247
- };
248
- return _getMaxHeight(beginRoot);
249
- }
250
- else {
251
- if (!beginRoot) {
252
- return -1;
253
- }
254
- const stack = [{ node: beginRoot, depth: 0 }];
255
- let maxHeight = 0;
256
- while (stack.length > 0) {
257
- const { node, depth } = stack.pop();
258
- if (node.left) {
259
- stack.push({ node: node.left, depth: depth + 1 });
260
- }
261
- if (node.right) {
262
- stack.push({ node: node.right, depth: depth + 1 });
263
- }
264
- maxHeight = Math.max(maxHeight, depth);
265
- }
266
- return maxHeight;
267
- }
268
- }
269
- getMinHeight(beginRoot = this.root) {
270
- var _a, _b, _c;
271
- if (!beginRoot)
272
- return -1;
273
- if (this._loopType === types_1.LoopType.RECURSIVE) {
274
- const _getMinHeight = (cur) => {
275
- if (!cur)
276
- return 0;
277
- if (!cur.left && !cur.right)
278
- return 0;
279
- const leftMinHeight = _getMinHeight(cur.left);
280
- const rightMinHeight = _getMinHeight(cur.right);
281
- return Math.min(leftMinHeight, rightMinHeight) + 1;
282
- };
283
- return _getMinHeight(beginRoot);
284
- }
285
- else {
286
- const stack = [];
287
- let node = beginRoot, last = null;
288
- const depths = new Map();
289
- while (stack.length > 0 || node) {
290
- if (node) {
291
- stack.push(node);
292
- node = node.left;
293
- }
294
- else {
295
- node = stack[stack.length - 1];
296
- if (!node.right || last === node.right) {
297
- node = stack.pop();
298
- if (node) {
299
- const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
300
- const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
301
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
302
- last = node;
303
- node = null;
304
- }
305
- }
306
- else
307
- node = node.right;
308
- }
309
- }
310
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
311
- }
312
- }
313
- isPerfectlyBalanced(beginRoot = this.root) {
314
- return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
315
- }
316
- getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
317
- if (!this.root)
318
- return [];
319
- const result = [];
320
- if (this.loopType === types_1.LoopType.RECURSIVE) {
321
- const _traverse = (cur) => {
322
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
323
- return;
324
- if (!cur.left && !cur.right)
325
- return;
326
- cur.left && _traverse(cur.left);
327
- cur.right && _traverse(cur.right);
328
- };
329
- _traverse(this.root);
330
- }
331
- else {
332
- const queue = [this.root];
333
- while (queue.length > 0) {
334
- const cur = queue.shift();
335
- if (cur) {
336
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
337
- return result;
338
- cur.left && queue.push(cur.left);
339
- cur.right && queue.push(cur.right);
340
- }
341
- }
342
- }
343
- return result;
344
- }
345
- has(nodeProperty, propertyName = 'key') {
346
- return this.getNodes(nodeProperty, propertyName).length > 0;
347
- }
348
- get(nodeProperty, propertyName = 'key') {
349
- var _a;
350
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
351
- }
352
- getPathToRoot(node, isReverse = true) {
353
- const result = [];
354
- while (node.parent) {
355
- result.push(node);
356
- node = node.parent;
357
- }
358
- result.push(node);
359
- return isReverse ? result.reverse() : result;
360
- }
361
- getLeftMost(beginRoot = this.root) {
362
- if (typeof beginRoot === 'number')
363
- beginRoot = this.get(beginRoot, 'key');
364
- if (!beginRoot)
365
- return beginRoot;
366
- if (this._loopType === types_1.LoopType.RECURSIVE) {
367
- const _traverse = (cur) => {
368
- if (!cur.left)
369
- return cur;
370
- return _traverse(cur.left);
371
- };
372
- return _traverse(beginRoot);
373
- }
374
- else {
375
- const _traverse = (0, utils_1.trampoline)((cur) => {
376
- if (!cur.left)
377
- return cur;
378
- return _traverse.cont(cur.left);
379
- });
380
- return _traverse(beginRoot);
381
- }
382
- }
383
- getRightMost(beginRoot = this.root) {
384
- if (!beginRoot)
385
- return beginRoot;
386
- if (this._loopType === types_1.LoopType.RECURSIVE) {
387
- const _traverse = (cur) => {
388
- if (!cur.right)
389
- return cur;
390
- return _traverse(cur.right);
391
- };
392
- return _traverse(beginRoot);
393
- }
394
- else {
395
- const _traverse = (0, utils_1.trampoline)((cur) => {
396
- if (!cur.right)
397
- return cur;
398
- return _traverse.cont(cur.right);
399
- });
400
- return _traverse(beginRoot);
401
- }
402
- }
403
- isSubtreeBST(node) {
404
- if (!node)
405
- return true;
406
- if (this._loopType === types_1.LoopType.RECURSIVE) {
407
- const dfs = (cur, min, max) => {
408
- if (!cur)
409
- return true;
410
- if (cur.key <= min || cur.key >= max)
411
- return false;
412
- return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
413
- };
414
- return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
415
- }
416
- else {
417
- const stack = [];
418
- let prev = Number.MIN_SAFE_INTEGER, curr = node;
419
- while (curr || stack.length > 0) {
420
- while (curr) {
421
- stack.push(curr);
422
- curr = curr.left;
423
- }
424
- curr = stack.pop();
425
- if (!curr || prev >= curr.key)
426
- return false;
427
- prev = curr.key;
428
- curr = curr.right;
429
- }
430
- return true;
431
- }
432
- }
433
- isBST() {
434
- return this.isSubtreeBST(this.root);
435
- }
436
- getSubTreeSize(subTreeRoot) {
437
- let size = 0;
438
- if (!subTreeRoot)
439
- return size;
440
- if (this._loopType === types_1.LoopType.RECURSIVE) {
441
- const _traverse = (cur) => {
442
- size++;
443
- cur.left && _traverse(cur.left);
444
- cur.right && _traverse(cur.right);
445
- };
446
- _traverse(subTreeRoot);
447
- return size;
448
- }
449
- else {
450
- const stack = [subTreeRoot];
451
- while (stack.length > 0) {
452
- const cur = stack.pop();
453
- size++;
454
- cur.right && stack.push(cur.right);
455
- cur.left && stack.push(cur.left);
456
- }
457
- return size;
458
- }
459
- }
460
- subTreeSum(subTreeRoot, propertyName = 'key') {
461
- if (typeof subTreeRoot === 'number')
462
- subTreeRoot = this.get(subTreeRoot, 'key');
463
- if (!subTreeRoot)
464
- return 0;
465
- let sum = 0;
466
- const _sumByProperty = (cur) => {
467
- let needSum;
468
- switch (propertyName) {
469
- case 'key':
470
- needSum = cur.key;
471
- break;
472
- case 'val':
473
- needSum = typeof cur.val === 'number' ? cur.val : 0;
474
- break;
475
- default:
476
- needSum = cur.key;
477
- break;
478
- }
479
- return needSum;
480
- };
481
- if (this._loopType === types_1.LoopType.RECURSIVE) {
482
- const _traverse = (cur) => {
483
- sum += _sumByProperty(cur);
484
- cur.left && _traverse(cur.left);
485
- cur.right && _traverse(cur.right);
486
- };
487
- _traverse(subTreeRoot);
488
- }
489
- else {
490
- const stack = [subTreeRoot];
491
- while (stack.length > 0) {
492
- const cur = stack.pop();
493
- sum += _sumByProperty(cur);
494
- cur.right && stack.push(cur.right);
495
- cur.left && stack.push(cur.left);
496
- }
497
- }
498
- return sum;
499
- }
500
- subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
501
- if (typeof subTreeRoot === 'number')
502
- subTreeRoot = this.get(subTreeRoot, 'key');
503
- if (!subTreeRoot)
504
- return false;
505
- const _addByProperty = (cur) => {
506
- switch (propertyName) {
507
- case 'key':
508
- cur.key += delta;
509
- break;
510
- default:
511
- cur.key += delta;
512
- break;
513
- }
514
- };
515
- if (this._loopType === types_1.LoopType.RECURSIVE) {
516
- const _traverse = (cur) => {
517
- _addByProperty(cur);
518
- cur.left && _traverse(cur.left);
519
- cur.right && _traverse(cur.right);
520
- };
521
- _traverse(subTreeRoot);
522
- }
523
- else {
524
- const stack = [subTreeRoot];
525
- while (stack.length > 0) {
526
- const cur = stack.pop();
527
- _addByProperty(cur);
528
- cur.right && stack.push(cur.right);
529
- cur.left && stack.push(cur.left);
530
- }
531
- }
532
- return true;
533
- }
534
- bfs(nodeOrPropertyName = 'key') {
535
- this._clearResults();
536
- const queue = [this.root];
537
- while (queue.length !== 0) {
538
- const cur = queue.shift();
539
- if (cur) {
540
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
541
- if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
542
- queue.push(cur.left);
543
- if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
544
- queue.push(cur.right);
545
- }
546
- }
547
- return this._getResultByPropertyName(nodeOrPropertyName);
548
- }
549
- dfs(pattern = 'in', nodeOrPropertyName = 'key') {
550
- this._clearResults();
551
- const _traverse = (node) => {
552
- switch (pattern) {
553
- case 'in':
554
- if (node.left)
555
- _traverse(node.left);
556
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
557
- if (node.right)
558
- _traverse(node.right);
559
- break;
560
- case 'pre':
561
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
562
- if (node.left)
563
- _traverse(node.left);
564
- if (node.right)
565
- _traverse(node.right);
566
- break;
567
- case 'post':
568
- if (node.left)
569
- _traverse(node.left);
570
- if (node.right)
571
- _traverse(node.right);
572
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
573
- break;
574
- }
575
- };
576
- this.root && _traverse(this.root);
577
- return this._getResultByPropertyName(nodeOrPropertyName);
578
- }
579
- dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
580
- this._clearResults();
581
- if (!this.root)
582
- return this._getResultByPropertyName(nodeOrPropertyName);
583
- const stack = [{ opt: 0, node: this.root }];
584
- while (stack.length > 0) {
585
- const cur = stack.pop();
586
- if (!cur || !cur.node)
587
- continue;
588
- if (cur.opt === 1) {
589
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
590
- }
591
- else {
592
- switch (pattern) {
593
- case 'in':
594
- stack.push({ opt: 0, node: cur.node.right });
595
- stack.push({ opt: 1, node: cur.node });
596
- stack.push({ opt: 0, node: cur.node.left });
597
- break;
598
- case 'pre':
599
- stack.push({ opt: 0, node: cur.node.right });
600
- stack.push({ opt: 0, node: cur.node.left });
601
- stack.push({ opt: 1, node: cur.node });
602
- break;
603
- case 'post':
604
- stack.push({ opt: 1, node: cur.node });
605
- stack.push({ opt: 0, node: cur.node.right });
606
- stack.push({ opt: 0, node: cur.node.left });
607
- break;
608
- default:
609
- stack.push({ opt: 0, node: cur.node.right });
610
- stack.push({ opt: 1, node: cur.node });
611
- stack.push({ opt: 0, node: cur.node.left });
612
- break;
613
- }
614
- }
615
- }
616
- return this._getResultByPropertyName(nodeOrPropertyName);
617
- }
618
- levelIterative(node = this.root, nodeOrPropertyName = 'key') {
619
- if (!node)
620
- return [];
621
- this._clearResults();
622
- const queue = [node];
623
- while (queue.length > 0) {
624
- const cur = queue.shift();
625
- if (cur) {
626
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
627
- if (cur.left) {
628
- queue.push(cur.left);
629
- }
630
- if (cur.right) {
631
- queue.push(cur.right);
632
- }
633
- }
634
- }
635
- return this._getResultByPropertyName(nodeOrPropertyName);
636
- }
637
- listLevels(node = this.root, nodeOrPropertyName = 'key') {
638
- if (!node)
639
- return [];
640
- const levelsNodes = [];
641
- const collectByProperty = (node, level) => {
642
- switch (nodeOrPropertyName) {
643
- case 'key':
644
- levelsNodes[level].push(node.key);
645
- break;
646
- case 'val':
647
- levelsNodes[level].push(node.val);
648
- break;
649
- case 'node':
650
- levelsNodes[level].push(node);
651
- break;
652
- default:
653
- levelsNodes[level].push(node.key);
654
- break;
655
- }
656
- };
657
- if (this.loopType === types_1.LoopType.RECURSIVE) {
658
- const _recursive = (node, level) => {
659
- if (!levelsNodes[level])
660
- levelsNodes[level] = [];
661
- collectByProperty(node, level);
662
- if (node.left)
663
- _recursive(node.left, level + 1);
664
- if (node.right)
665
- _recursive(node.right, level + 1);
666
- };
667
- _recursive(node, 0);
668
- }
669
- else {
670
- const stack = [[node, 0]];
671
- while (stack.length > 0) {
672
- const head = stack.pop();
673
- const [node, level] = head;
674
- if (!levelsNodes[level])
675
- levelsNodes[level] = [];
676
- collectByProperty(node, level);
677
- if (node.right)
678
- stack.push([node.right, level + 1]);
679
- if (node.left)
680
- stack.push([node.left, level + 1]);
681
- }
682
- }
683
- return levelsNodes;
684
- }
685
- getPredecessor(node) {
686
- if (node.left) {
687
- let predecessor = node.left;
688
- while (!predecessor || (predecessor.right && predecessor.right !== node)) {
689
- if (predecessor) {
690
- predecessor = predecessor.right;
691
- }
692
- }
693
- return predecessor;
694
- }
695
- else {
696
- return node;
697
- }
698
- }
699
- morris(pattern = 'in', nodeOrPropertyName = 'key') {
700
- if (this.root === null)
701
- return [];
702
- this._clearResults();
703
- let cur = this.root;
704
- const _reverseEdge = (node) => {
705
- let pre = null;
706
- let next = null;
707
- while (node) {
708
- next = node.right;
709
- node.right = pre;
710
- pre = node;
711
- node = next;
712
- }
713
- return pre;
714
- };
715
- const _printEdge = (node) => {
716
- const tail = _reverseEdge(node);
717
- let cur = tail;
718
- while (cur) {
719
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
720
- cur = cur.right;
721
- }
722
- _reverseEdge(tail);
723
- };
724
- switch (pattern) {
725
- case 'in':
726
- while (cur) {
727
- if (cur.left) {
728
- const predecessor = this.getPredecessor(cur);
729
- if (!predecessor.right) {
730
- predecessor.right = cur;
731
- cur = cur.left;
732
- continue;
733
- }
734
- else {
735
- predecessor.right = null;
736
- }
737
- }
738
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
739
- cur = cur.right;
740
- }
741
- break;
742
- case 'pre':
743
- while (cur) {
744
- if (cur.left) {
745
- const predecessor = this.getPredecessor(cur);
746
- if (!predecessor.right) {
747
- predecessor.right = cur;
748
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
749
- cur = cur.left;
750
- continue;
751
- }
752
- else {
753
- predecessor.right = null;
754
- }
755
- }
756
- else {
757
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
758
- }
759
- cur = cur.right;
760
- }
761
- break;
762
- case 'post':
763
- while (cur) {
764
- if (cur.left) {
765
- const predecessor = this.getPredecessor(cur);
766
- if (predecessor.right === null) {
767
- predecessor.right = cur;
768
- cur = cur.left;
769
- continue;
770
- }
771
- else {
772
- predecessor.right = null;
773
- _printEdge(cur.left);
774
- }
775
- }
776
- cur = cur.right;
777
- }
778
- _printEdge(this.root);
779
- break;
780
- }
781
- return this._getResultByPropertyName(nodeOrPropertyName);
782
- }
783
- _addTo(newNode, parent) {
784
- if (parent) {
785
- if (parent.left === undefined) {
786
- parent.left = newNode;
787
- if (newNode) {
788
- this._setSize(this.size + 1);
789
- }
790
- return parent.left;
791
- }
792
- else if (parent.right === undefined) {
793
- parent.right = newNode;
794
- if (newNode) {
795
- this._setSize(this.size + 1);
796
- }
797
- return parent.right;
798
- }
799
- else {
800
- return;
801
- }
802
- }
803
- else {
804
- return;
805
- }
806
- }
807
- _setLoopType(value) {
808
- this._loopType = value;
809
- }
810
- _setRoot(v) {
811
- if (v) {
812
- v.parent = undefined;
813
- }
814
- this._root = v;
815
- }
816
- _setSize(v) {
817
- this._size = v;
818
- }
819
- _clearResults() {
820
- this.visitedKey = [];
821
- this.visitedVal = [];
822
- this.visitedNode = [];
823
- }
824
- _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
825
- switch (propertyName) {
826
- case 'key':
827
- if (cur.key === nodeProperty) {
828
- result.push(cur);
829
- return onlyOne;
830
- }
831
- break;
832
- case 'val':
833
- if (cur.val === nodeProperty) {
834
- result.push(cur);
835
- return onlyOne;
836
- }
837
- break;
838
- default:
839
- if (cur.key === nodeProperty) {
840
- result.push(cur);
841
- return onlyOne;
842
- }
843
- break;
844
- }
845
- }
846
- _accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
847
- switch (nodeOrPropertyName) {
848
- case 'key':
849
- this.visitedKey.push(node.key);
850
- break;
851
- case 'val':
852
- this.visitedVal.push(node.val);
853
- break;
854
- case 'node':
855
- this.visitedNode.push(node);
856
- break;
857
- default:
858
- this.visitedKey.push(node.key);
859
- break;
860
- }
861
- }
862
- _getResultByPropertyName(nodeOrPropertyName = 'key') {
863
- switch (nodeOrPropertyName) {
864
- case 'key':
865
- return this.visitedKey;
866
- case 'val':
867
- return this.visitedVal;
868
- case 'node':
869
- return this.visitedNode;
870
- default:
871
- return this.visitedKey;
872
- }
873
- }
874
23
  }
875
24
  exports.AbstractBinaryTree = AbstractBinaryTree;
876
25
  //# sourceMappingURL=abstract-binary-tree.js.map