data-structure-typed 1.19.3 → 1.19.5

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +3 -2
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -1,63 +1,25 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
2
  Object.defineProperty(exports, "__esModule", { value: true });
34
3
  exports.BST = exports.BSTNode = void 0;
35
- var types_1 = require("../types");
36
- var binary_tree_1 = require("./binary-tree");
37
- var BSTNode = /** @class */ (function (_super) {
38
- __extends(BSTNode, _super);
39
- function BSTNode() {
40
- return _super !== null && _super.apply(this, arguments) || this;
41
- }
42
- return BSTNode;
43
- }(binary_tree_1.BinaryTreeNode));
4
+ const types_1 = require("../types");
5
+ const binary_tree_1 = require("./binary-tree");
6
+ class BSTNode extends binary_tree_1.BinaryTreeNode {
7
+ }
44
8
  exports.BSTNode = BSTNode;
45
- var BST = /** @class */ (function (_super) {
46
- __extends(BST, _super);
9
+ class BST extends binary_tree_1.BinaryTree {
47
10
  /**
48
11
  * The constructor function initializes a binary search tree object with an optional comparator function.
49
12
  * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
50
13
  */
51
- function BST(options) {
52
- var _this = _super.call(this, options) || this;
53
- _this._comparator = function (a, b) { return a - b; };
14
+ constructor(options) {
15
+ super(options);
16
+ this._comparator = (a, b) => a - b;
54
17
  if (options !== undefined) {
55
- var comparator = options.comparator;
18
+ const { comparator } = options;
56
19
  if (comparator !== undefined) {
57
- _this._comparator = comparator;
20
+ this._comparator = comparator;
58
21
  }
59
22
  }
60
- return _this;
61
23
  }
62
24
  /**
63
25
  * The function creates a new binary search tree node with the given id and value.
@@ -67,9 +29,9 @@ var BST = /** @class */ (function (_super) {
67
29
  * that will be stored in the node.
68
30
  * @returns a new instance of the BSTNode class with the specified id and value.
69
31
  */
70
- BST.prototype.createNode = function (id, val) {
32
+ createNode(id, val) {
71
33
  return new BSTNode(id, val);
72
- };
34
+ }
73
35
  /**
74
36
  * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
75
37
  * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
@@ -79,17 +41,17 @@ var BST = /** @class */ (function (_super) {
79
41
  * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
80
42
  * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
81
43
  */
82
- BST.prototype.add = function (id, val) {
83
- var inserted = null;
84
- var newNode = this.createNode(id, val);
44
+ add(id, val) {
45
+ let inserted = null;
46
+ const newNode = this.createNode(id, val);
85
47
  if (this.root === null) {
86
48
  this._setRoot(newNode);
87
49
  this._setSize(this.size + 1);
88
50
  inserted = (this.root);
89
51
  }
90
52
  else {
91
- var cur = this.root;
92
- var traversing = true;
53
+ let cur = this.root;
54
+ let traversing = true;
93
55
  while (traversing) {
94
56
  if (cur !== null && newNode !== null) {
95
57
  if (this._compare(cur.id, id) === types_1.CP.eq) {
@@ -143,7 +105,7 @@ var BST = /** @class */ (function (_super) {
143
105
  }
144
106
  }
145
107
  return inserted;
146
- };
108
+ }
147
109
  /**
148
110
  * The function returns the first node in a binary tree that matches the given property name and value.
149
111
  * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
@@ -152,11 +114,11 @@ var BST = /** @class */ (function (_super) {
152
114
  * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
153
115
  * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
154
116
  */
155
- BST.prototype.get = function (nodeProperty, propertyName) {
117
+ get(nodeProperty, propertyName) {
156
118
  var _a;
157
119
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
158
120
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
159
- };
121
+ }
160
122
  /**
161
123
  * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
162
124
  * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
@@ -164,7 +126,7 @@ var BST = /** @class */ (function (_super) {
164
126
  * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
165
127
  * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
166
128
  */
167
- BST.prototype.lastKey = function () {
129
+ lastKey() {
168
130
  var _a, _b, _c, _d, _e, _f;
169
131
  if (this._compare(0, 1) === types_1.CP.lt)
170
132
  return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
@@ -172,7 +134,7 @@ var BST = /** @class */ (function (_super) {
172
134
  return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
173
135
  else
174
136
  return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
175
- };
137
+ }
176
138
  /**
177
139
  * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
178
140
  * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
@@ -184,35 +146,34 @@ var BST = /** @class */ (function (_super) {
184
146
  * is set to `true`, the function will return an array with only one node (if
185
147
  * @returns an array of nodes (type N).
186
148
  */
187
- BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
188
- var _this = this;
149
+ getNodes(nodeProperty, propertyName, onlyOne) {
189
150
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
190
151
  if (!this.root)
191
152
  return [];
192
- var result = [];
153
+ const result = [];
193
154
  if (this.loopType === types_1.LoopType.RECURSIVE) {
194
- var _traverse_1 = function (cur) {
195
- if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
155
+ const _traverse = (cur) => {
156
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
196
157
  return;
197
158
  if (!cur.left && !cur.right)
198
159
  return;
199
160
  if (propertyName === 'id') {
200
- if (_this._compare(cur.id, nodeProperty) === types_1.CP.gt)
201
- cur.left && _traverse_1(cur.left);
202
- if (_this._compare(cur.id, nodeProperty) === types_1.CP.lt)
203
- cur.right && _traverse_1(cur.right);
161
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
162
+ cur.left && _traverse(cur.left);
163
+ if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
164
+ cur.right && _traverse(cur.right);
204
165
  }
205
166
  else {
206
- cur.left && _traverse_1(cur.left);
207
- cur.right && _traverse_1(cur.right);
167
+ cur.left && _traverse(cur.left);
168
+ cur.right && _traverse(cur.right);
208
169
  }
209
170
  };
210
- _traverse_1(this.root);
171
+ _traverse(this.root);
211
172
  }
212
173
  else {
213
- var queue = [this.root];
174
+ const queue = [this.root];
214
175
  while (queue.length > 0) {
215
- var cur = queue.shift();
176
+ const cur = queue.shift();
216
177
  if (cur) {
217
178
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
218
179
  return result;
@@ -230,7 +191,7 @@ var BST = /** @class */ (function (_super) {
230
191
  }
231
192
  }
232
193
  return result;
233
- };
194
+ }
234
195
  // --- start additional functions
235
196
  /**
236
197
  * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
@@ -241,8 +202,7 @@ var BST = /** @class */ (function (_super) {
241
202
  * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
242
203
  * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
243
204
  */
244
- BST.prototype.lesserSum = function (beginNode, propertyName) {
245
- var _this = this;
205
+ lesserSum(beginNode, propertyName) {
246
206
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
247
207
  if (typeof beginNode === 'number')
248
208
  beginNode = this.get(beginNode, 'id');
@@ -250,9 +210,9 @@ var BST = /** @class */ (function (_super) {
250
210
  return 0;
251
211
  if (!this.root)
252
212
  return 0;
253
- var id = beginNode.id;
254
- var getSumByPropertyName = function (cur) {
255
- var needSum;
213
+ const id = beginNode.id;
214
+ const getSumByPropertyName = (cur) => {
215
+ let needSum;
256
216
  switch (propertyName) {
257
217
  case 'id':
258
218
  needSum = cur.id;
@@ -263,39 +223,39 @@ var BST = /** @class */ (function (_super) {
263
223
  }
264
224
  return needSum;
265
225
  };
266
- var sum = 0;
226
+ let sum = 0;
267
227
  if (this.loopType === types_1.LoopType.RECURSIVE) {
268
- var _traverse_2 = function (cur) {
269
- var compared = _this._compare(cur.id, id);
228
+ const _traverse = (cur) => {
229
+ const compared = this._compare(cur.id, id);
270
230
  if (compared === types_1.CP.eq) {
271
231
  if (cur.right)
272
- sum += _this.subTreeSum(cur.right, propertyName);
232
+ sum += this.subTreeSum(cur.right, propertyName);
273
233
  return;
274
234
  }
275
235
  else if (compared === types_1.CP.lt) {
276
236
  if (cur.left)
277
- sum += _this.subTreeSum(cur.left, propertyName);
237
+ sum += this.subTreeSum(cur.left, propertyName);
278
238
  sum += getSumByPropertyName(cur);
279
239
  if (cur.right)
280
- _traverse_2(cur.right);
240
+ _traverse(cur.right);
281
241
  else
282
242
  return;
283
243
  }
284
244
  else {
285
245
  if (cur.left)
286
- _traverse_2(cur.left);
246
+ _traverse(cur.left);
287
247
  else
288
248
  return;
289
249
  }
290
250
  };
291
- _traverse_2(this.root);
251
+ _traverse(this.root);
292
252
  }
293
253
  else {
294
- var queue = [this.root];
254
+ const queue = [this.root];
295
255
  while (queue.length > 0) {
296
- var cur = queue.shift();
256
+ const cur = queue.shift();
297
257
  if (cur) {
298
- var compared = this._compare(cur.id, id);
258
+ const compared = this._compare(cur.id, id);
299
259
  if (compared === types_1.CP.eq) {
300
260
  if (cur.right)
301
261
  sum += this.subTreeSum(cur.right, propertyName);
@@ -320,7 +280,7 @@ var BST = /** @class */ (function (_super) {
320
280
  }
321
281
  }
322
282
  return sum;
323
- };
283
+ }
324
284
  /**
325
285
  * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
326
286
  * have a greater value than a given node.
@@ -333,17 +293,16 @@ var BST = /** @class */ (function (_super) {
333
293
  * 'id'.
334
294
  * @returns a boolean value.
335
295
  */
336
- BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
337
- var _this = this;
296
+ allGreaterNodesAdd(node, delta, propertyName) {
338
297
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
339
298
  if (typeof node === 'number')
340
299
  node = this.get(node, 'id');
341
300
  if (!node)
342
301
  return false;
343
- var id = node.id;
302
+ const id = node.id;
344
303
  if (!this.root)
345
304
  return false;
346
- var _sumByPropertyName = function (cur) {
305
+ const _sumByPropertyName = (cur) => {
347
306
  switch (propertyName) {
348
307
  case 'id':
349
308
  cur.id += delta;
@@ -354,26 +313,26 @@ var BST = /** @class */ (function (_super) {
354
313
  }
355
314
  };
356
315
  if (this.loopType === types_1.LoopType.RECURSIVE) {
357
- var _traverse_3 = function (cur) {
358
- var compared = _this._compare(cur.id, id);
316
+ const _traverse = (cur) => {
317
+ const compared = this._compare(cur.id, id);
359
318
  if (compared === types_1.CP.gt)
360
319
  _sumByPropertyName(cur);
361
320
  if (!cur.left && !cur.right)
362
321
  return;
363
- if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
364
- _traverse_3(cur.left);
365
- if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
366
- _traverse_3(cur.right);
322
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
323
+ _traverse(cur.left);
324
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
325
+ _traverse(cur.right);
367
326
  };
368
- _traverse_3(this.root);
327
+ _traverse(this.root);
369
328
  return true;
370
329
  }
371
330
  else {
372
- var queue = [this.root];
331
+ const queue = [this.root];
373
332
  while (queue.length > 0) {
374
- var cur = queue.shift();
333
+ const cur = queue.shift();
375
334
  if (cur) {
376
- var compared = this._compare(cur.id, id);
335
+ const compared = this._compare(cur.id, id);
377
336
  if (compared === types_1.CP.gt)
378
337
  _sumByPropertyName(cur);
379
338
  if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
@@ -384,7 +343,7 @@ var BST = /** @class */ (function (_super) {
384
343
  }
385
344
  return true;
386
345
  }
387
- };
346
+ }
388
347
  /**
389
348
  * Balancing Adjustment:
390
349
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -399,34 +358,33 @@ var BST = /** @class */ (function (_super) {
399
358
  * constructs a balanced binary search tree using either a recursive or iterative approach.
400
359
  * @returns The function `perfectlyBalance()` returns a boolean value.
401
360
  */
402
- BST.prototype.perfectlyBalance = function () {
403
- var _this = this;
404
- var sorted = this.DFS('in', 'node'), n = sorted.length;
361
+ perfectlyBalance() {
362
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
405
363
  this.clear();
406
364
  if (sorted.length < 1)
407
365
  return false;
408
366
  if (this.loopType === types_1.LoopType.RECURSIVE) {
409
- var buildBalanceBST_1 = function (l, r) {
367
+ const buildBalanceBST = (l, r) => {
410
368
  if (l > r)
411
369
  return;
412
- var m = l + Math.floor((r - l) / 2);
413
- var midNode = sorted[m];
414
- _this.add(midNode.id, midNode.val);
415
- buildBalanceBST_1(l, m - 1);
416
- buildBalanceBST_1(m + 1, r);
370
+ const m = l + Math.floor((r - l) / 2);
371
+ const midNode = sorted[m];
372
+ this.add(midNode.id, midNode.val);
373
+ buildBalanceBST(l, m - 1);
374
+ buildBalanceBST(m + 1, r);
417
375
  };
418
- buildBalanceBST_1(0, n - 1);
376
+ buildBalanceBST(0, n - 1);
419
377
  return true;
420
378
  }
421
379
  else {
422
- var stack = [[0, n - 1]];
380
+ const stack = [[0, n - 1]];
423
381
  while (stack.length > 0) {
424
- var popped = stack.pop();
382
+ const popped = stack.pop();
425
383
  if (popped) {
426
- var _a = __read(popped, 2), l = _a[0], r = _a[1];
384
+ const [l, r] = popped;
427
385
  if (l <= r) {
428
- var m = l + Math.floor((r - l) / 2);
429
- var midNode = sorted[m];
386
+ const m = l + Math.floor((r - l) / 2);
387
+ const midNode = sorted[m];
430
388
  this.add(midNode.id, midNode.val);
431
389
  stack.push([m + 1, r]);
432
390
  stack.push([l, m - 1]);
@@ -435,31 +393,31 @@ var BST = /** @class */ (function (_super) {
435
393
  }
436
394
  return true;
437
395
  }
438
- };
396
+ }
439
397
  /**
440
398
  * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
441
399
  * @returns a boolean value.
442
400
  */
443
- BST.prototype.isAVLBalanced = function () {
401
+ isAVLBalanced() {
444
402
  var _a, _b;
445
403
  if (!this.root)
446
404
  return true;
447
- var balanced = true;
405
+ let balanced = true;
448
406
  if (this.loopType === types_1.LoopType.RECURSIVE) {
449
- var _height_1 = function (cur) {
407
+ const _height = (cur) => {
450
408
  if (!cur)
451
409
  return 0;
452
- var leftHeight = _height_1(cur.left), rightHeight = _height_1(cur.right);
410
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
453
411
  if (Math.abs(leftHeight - rightHeight) > 1)
454
412
  balanced = false;
455
413
  return Math.max(leftHeight, rightHeight) + 1;
456
414
  };
457
- _height_1(this.root);
415
+ _height(this.root);
458
416
  }
459
417
  else {
460
- var stack = [];
461
- var node = this.root, last = null;
462
- var depths = new Map();
418
+ const stack = [];
419
+ let node = this.root, last = null;
420
+ const depths = new Map();
463
421
  while (stack.length > 0 || node) {
464
422
  if (node) {
465
423
  stack.push(node);
@@ -470,8 +428,8 @@ var BST = /** @class */ (function (_super) {
470
428
  if (!node.right || last === node.right) {
471
429
  node = stack.pop();
472
430
  if (node) {
473
- var left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
474
- var right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
431
+ const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
432
+ const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
475
433
  if (Math.abs(left - right) > 1)
476
434
  return false;
477
435
  depths.set(node, 1 + Math.max(left, right));
@@ -485,7 +443,7 @@ var BST = /** @class */ (function (_super) {
485
443
  }
486
444
  }
487
445
  return balanced;
488
- };
446
+ }
489
447
  /**
490
448
  * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
491
449
  * greater than, less than, or equal to the second ID.
@@ -494,15 +452,14 @@ var BST = /** @class */ (function (_super) {
494
452
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
495
453
  * than), or CP.eq (equal).
496
454
  */
497
- BST.prototype._compare = function (a, b) {
498
- var compared = this._comparator(a, b);
455
+ _compare(a, b) {
456
+ const compared = this._comparator(a, b);
499
457
  if (compared > 0)
500
458
  return types_1.CP.gt;
501
459
  else if (compared < 0)
502
460
  return types_1.CP.lt;
503
461
  else
504
462
  return types_1.CP.eq;
505
- };
506
- return BST;
507
- }(binary_tree_1.BinaryTree));
463
+ }
464
+ }
508
465
  exports.BST = BST;
@@ -1,69 +1,45 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  Object.defineProperty(exports, "__esModule", { value: true });
18
3
  exports.RBTree = exports.RBTreeNode = void 0;
19
- var types_1 = require("../types");
20
- var bst_1 = require("./bst");
21
- var RBTreeNode = /** @class */ (function (_super) {
22
- __extends(RBTreeNode, _super);
23
- function RBTreeNode(id, color, val) {
24
- var _this = _super.call(this, id, val) || this;
25
- _this._color = types_1.RBColor.RED;
26
- _this._color = color;
27
- return _this;
4
+ const types_1 = require("../types");
5
+ const bst_1 = require("./bst");
6
+ class RBTreeNode extends bst_1.BSTNode {
7
+ constructor(id, color, val) {
8
+ super(id, val);
9
+ this._color = types_1.RBColor.RED;
10
+ this._color = color;
28
11
  }
29
- Object.defineProperty(RBTreeNode.prototype, "color", {
30
- get: function () {
31
- return this._color;
32
- },
33
- set: function (value) {
34
- this._color = value;
35
- },
36
- enumerable: false,
37
- configurable: true
38
- });
39
- return RBTreeNode;
40
- }(bst_1.BSTNode));
12
+ get color() {
13
+ return this._color;
14
+ }
15
+ set color(value) {
16
+ this._color = value;
17
+ }
18
+ }
41
19
  exports.RBTreeNode = RBTreeNode;
42
- var RBTree = /** @class */ (function (_super) {
43
- __extends(RBTree, _super);
44
- function RBTree(options) {
45
- return _super.call(this, options) || this;
20
+ class RBTree extends bst_1.BST {
21
+ constructor(options) {
22
+ super(options);
46
23
  }
47
- RBTree.prototype.createNode = function (id, val) {
24
+ createNode(id, val) {
48
25
  return new RBTreeNode(id, types_1.RBColor.RED, val);
49
- };
26
+ }
50
27
  // private override _root: BinaryTreeNode<N> | null = null;
51
28
  //
52
29
  // override get root(): BinaryTreeNode<N> | null {
53
30
  // return this._root;
54
31
  // }
55
- RBTree.prototype.insert = function (id, val) {
56
- };
57
- RBTree.prototype.leftRotate = function (node) {
58
- };
59
- RBTree.prototype.rightRotate = function (node) {
60
- };
61
- RBTree.prototype.insertFixup = function (node) {
62
- };
63
- RBTree.prototype.deleteFixup = function (node) {
64
- };
65
- RBTree.prototype.transplant = function (u, v) {
66
- };
67
- return RBTree;
68
- }(bst_1.BST));
32
+ insert(id, val) {
33
+ }
34
+ leftRotate(node) {
35
+ }
36
+ rightRotate(node) {
37
+ }
38
+ insertFixup(node) {
39
+ }
40
+ deleteFixup(node) {
41
+ }
42
+ transplant(u, v) {
43
+ }
44
+ }
69
45
  exports.RBTree = RBTree;