data-structure-typed 1.34.7 → 1.34.9

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 (167) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +1 -1
  3. package/CONTRIBUTING.md +0 -0
  4. package/README.md +8 -8
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
  8. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/avl-tree.js +64 -90
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  12. package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.js +12 -31
  16. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/data-structures/binary-tree/bst.js +157 -244
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/rb-tree.js +20 -44
  20. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/data-structures/binary-tree/segment-tree.js +80 -122
  22. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
  26. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  27. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  28. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
  29. package/dist/data-structures/graph/abstract-graph.js +351 -585
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.js +152 -282
  32. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  33. package/dist/data-structures/graph/map-graph.js +47 -88
  34. package/dist/data-structures/graph/map-graph.js.map +1 -1
  35. package/dist/data-structures/graph/undirected-graph.js +91 -180
  36. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  37. package/dist/data-structures/hash/coordinate-map.js +23 -45
  38. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  39. package/dist/data-structures/hash/coordinate-set.js +20 -42
  40. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  41. package/dist/data-structures/hash/hash-map.js +85 -247
  42. package/dist/data-structures/hash/hash-map.js.map +1 -1
  43. package/dist/data-structures/hash/hash-table.js +87 -128
  44. package/dist/data-structures/hash/hash-table.js.map +1 -1
  45. package/dist/data-structures/hash/pair.js +2 -5
  46. package/dist/data-structures/hash/pair.js.map +1 -1
  47. package/dist/data-structures/hash/tree-map.js +2 -5
  48. package/dist/data-structures/hash/tree-map.js.map +1 -1
  49. package/dist/data-structures/hash/tree-set.js +2 -5
  50. package/dist/data-structures/hash/tree-set.js.map +1 -1
  51. package/dist/data-structures/heap/heap.js +56 -80
  52. package/dist/data-structures/heap/heap.js.map +1 -1
  53. package/dist/data-structures/heap/max-heap.js +8 -26
  54. package/dist/data-structures/heap/max-heap.js.map +1 -1
  55. package/dist/data-structures/heap/min-heap.js +8 -26
  56. package/dist/data-structures/heap/min-heap.js.map +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
  58. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  59. package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
  60. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  61. package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
  62. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  63. package/dist/data-structures/matrix/matrix.js +7 -8
  64. package/dist/data-structures/matrix/matrix.js.map +1 -1
  65. package/dist/data-structures/matrix/matrix2d.js +57 -70
  66. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  67. package/dist/data-structures/matrix/navigator.js +18 -37
  68. package/dist/data-structures/matrix/navigator.js.map +1 -1
  69. package/dist/data-structures/matrix/vector2d.js +63 -84
  70. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  71. package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
  72. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  73. package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
  74. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  75. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  76. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  77. package/dist/data-structures/queue/deque.js +82 -128
  78. package/dist/data-structures/queue/deque.js.map +1 -1
  79. package/dist/data-structures/queue/queue.js +57 -157
  80. package/dist/data-structures/queue/queue.js.map +1 -1
  81. package/dist/data-structures/stack/stack.js +21 -22
  82. package/dist/data-structures/stack/stack.js.map +1 -1
  83. package/dist/data-structures/tree/tree.js +33 -46
  84. package/dist/data-structures/tree/tree.js.map +1 -1
  85. package/dist/data-structures/trie/trie.js +93 -200
  86. package/dist/data-structures/trie/trie.js.map +1 -1
  87. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  88. package/dist/utils/utils.js +22 -107
  89. package/dist/utils/utils.js.map +1 -1
  90. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
  91. package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
  92. package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
  93. package/lib/data-structures/binary-tree/avl-tree.js +35 -13
  94. package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
  95. package/lib/data-structures/binary-tree/binary-tree.js +7 -7
  96. package/lib/data-structures/binary-tree/bst.d.ts +34 -34
  97. package/lib/data-structures/binary-tree/bst.js +86 -89
  98. package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
  99. package/lib/data-structures/binary-tree/rb-tree.js +4 -4
  100. package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
  101. package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
  102. package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
  103. package/lib/data-structures/graph/abstract-graph.js +86 -86
  104. package/lib/data-structures/graph/directed-graph.d.ts +51 -51
  105. package/lib/data-structures/graph/directed-graph.js +63 -63
  106. package/lib/data-structures/graph/map-graph.d.ts +13 -13
  107. package/lib/data-structures/graph/map-graph.js +12 -12
  108. package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
  109. package/lib/data-structures/graph/undirected-graph.js +32 -32
  110. package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
  111. package/lib/data-structures/priority-queue/priority-queue.js +3 -3
  112. package/lib/data-structures/tree/tree.d.ts +4 -4
  113. package/lib/data-structures/tree/tree.js +6 -6
  114. package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
  115. package/lib/interfaces/abstract-graph.d.ts +13 -13
  116. package/lib/interfaces/avl-tree.d.ts +6 -4
  117. package/lib/interfaces/bst.d.ts +10 -9
  118. package/lib/interfaces/directed-graph.d.ts +5 -5
  119. package/lib/interfaces/rb-tree.d.ts +2 -2
  120. package/lib/interfaces/undirected-graph.d.ts +2 -2
  121. package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
  122. package/lib/types/data-structures/abstract-binary-tree.js +0 -1
  123. package/lib/types/data-structures/abstract-graph.d.ts +2 -2
  124. package/lib/types/data-structures/bst.d.ts +2 -2
  125. package/lib/types/data-structures/tree-multiset.d.ts +1 -1
  126. package/lib/types/utils/validate-type.d.ts +8 -8
  127. package/package.json +6 -6
  128. package/scripts/rename_clear_files.sh +29 -0
  129. package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
  130. package/src/data-structures/binary-tree/avl-tree.ts +43 -14
  131. package/src/data-structures/binary-tree/binary-tree.ts +8 -8
  132. package/src/data-structures/binary-tree/bst.ts +101 -96
  133. package/src/data-structures/binary-tree/rb-tree.ts +9 -9
  134. package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
  135. package/src/data-structures/graph/abstract-graph.ts +114 -109
  136. package/src/data-structures/graph/directed-graph.ts +77 -77
  137. package/src/data-structures/graph/map-graph.ts +20 -15
  138. package/src/data-structures/graph/undirected-graph.ts +39 -39
  139. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  140. package/src/data-structures/tree/tree.ts +7 -7
  141. package/src/interfaces/abstract-binary-tree.ts +37 -50
  142. package/src/interfaces/abstract-graph.ts +13 -13
  143. package/src/interfaces/avl-tree.ts +6 -4
  144. package/src/interfaces/bst.ts +9 -9
  145. package/src/interfaces/directed-graph.ts +5 -5
  146. package/src/interfaces/rb-tree.ts +2 -2
  147. package/src/interfaces/undirected-graph.ts +2 -2
  148. package/src/types/data-structures/abstract-binary-tree.ts +3 -4
  149. package/src/types/data-structures/abstract-graph.ts +2 -2
  150. package/src/types/data-structures/bst.ts +2 -2
  151. package/src/types/data-structures/tree-multiset.ts +1 -1
  152. package/src/types/utils/validate-type.ts +10 -10
  153. package/test/integration/avl-tree.test.ts +24 -24
  154. package/test/integration/bst.test.ts +71 -71
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  159. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
  160. package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
  161. package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
  162. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  163. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
  165. package/test/unit/data-structures/tree/tree.test.ts +2 -2
  166. package/umd/bundle.min.js +1 -1
  167. package/umd/bundle.min.js.map +1 -1
@@ -1,84 +1,38 @@
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 __values = (this && this.__values) || function(o) {
18
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
- if (m) return m.call(o);
20
- if (o && typeof o.length === "number") return {
21
- next: function () {
22
- if (o && i >= o.length) o = void 0;
23
- return { value: o && o[i++], done: !o };
24
- }
25
- };
26
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
- };
28
- var __read = (this && this.__read) || function (o, n) {
29
- var m = typeof Symbol === "function" && o[Symbol.iterator];
30
- if (!m) return o;
31
- var i = m.call(o), r, ar = [], e;
32
- try {
33
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
34
- }
35
- catch (error) { e = { error: error }; }
36
- finally {
37
- try {
38
- if (r && !r.done && (m = i["return"])) m.call(i);
39
- }
40
- finally { if (e) throw e.error; }
41
- }
42
- return ar;
43
- };
44
2
  Object.defineProperty(exports, "__esModule", { value: true });
45
3
  exports.BST = exports.BSTNode = void 0;
46
- var types_1 = require("../../types");
47
- var binary_tree_1 = require("./binary-tree");
48
- var BSTNode = (function (_super) {
49
- __extends(BSTNode, _super);
50
- function BSTNode(id, val) {
51
- return _super.call(this, id, val) || this;
4
+ const types_1 = require("../../types");
5
+ const binary_tree_1 = require("./binary-tree");
6
+ class BSTNode extends binary_tree_1.BinaryTreeNode {
7
+ constructor(key, val) {
8
+ super(key, val);
52
9
  }
53
- return BSTNode;
54
- }(binary_tree_1.BinaryTreeNode));
10
+ }
55
11
  exports.BSTNode = BSTNode;
56
- var BST = (function (_super) {
57
- __extends(BST, _super);
58
- function BST(options) {
59
- var _this = _super.call(this, options) || this;
60
- _this._comparator = function (a, b) { return a - b; };
12
+ class BST extends binary_tree_1.BinaryTree {
13
+ constructor(options) {
14
+ super(options);
15
+ this._comparator = (a, b) => a - b;
61
16
  if (options !== undefined) {
62
- var comparator = options.comparator;
17
+ const { comparator } = options;
63
18
  if (comparator !== undefined) {
64
- _this._comparator = comparator;
19
+ this._comparator = comparator;
65
20
  }
66
21
  }
67
- return _this;
68
22
  }
69
- BST.prototype.createNode = function (id, val) {
70
- return new BSTNode(id, val);
71
- };
72
- BST.prototype.add = function (idOrNode, val) {
73
- var inserted = null;
74
- var newNode = null;
75
- if (idOrNode instanceof BSTNode) {
76
- newNode = idOrNode;
23
+ createNode(key, val) {
24
+ return new BSTNode(key, val);
25
+ }
26
+ add(keyOrNode, val) {
27
+ let inserted = null;
28
+ let newNode = null;
29
+ if (keyOrNode instanceof BSTNode) {
30
+ newNode = keyOrNode;
77
31
  }
78
- else if (typeof idOrNode === 'number') {
79
- newNode = this.createNode(idOrNode, val);
32
+ else if (typeof keyOrNode === 'number') {
33
+ newNode = this.createNode(keyOrNode, val);
80
34
  }
81
- else if (idOrNode === null) {
35
+ else if (keyOrNode === null) {
82
36
  newNode = null;
83
37
  }
84
38
  if (this.root === null) {
@@ -87,18 +41,18 @@ var BST = (function (_super) {
87
41
  inserted = this.root;
88
42
  }
89
43
  else {
90
- var cur = this.root;
91
- var traversing = true;
44
+ let cur = this.root;
45
+ let traversing = true;
92
46
  while (traversing) {
93
47
  if (cur !== null && newNode !== null) {
94
- if (this._compare(cur.id, newNode.id) === types_1.CP.eq) {
48
+ if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
95
49
  if (newNode) {
96
50
  cur.val = newNode.val;
97
51
  }
98
52
  traversing = false;
99
53
  inserted = cur;
100
54
  }
101
- else if (this._compare(cur.id, newNode.id) === types_1.CP.gt) {
55
+ else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
102
56
  if (cur.left === undefined) {
103
57
  if (newNode) {
104
58
  newNode.parent = cur;
@@ -113,7 +67,7 @@ var BST = (function (_super) {
113
67
  cur = cur.left;
114
68
  }
115
69
  }
116
- else if (this._compare(cur.id, newNode.id) === types_1.CP.lt) {
70
+ else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
117
71
  if (cur.right === undefined) {
118
72
  if (newNode) {
119
73
  newNode.parent = cur;
@@ -135,92 +89,60 @@ var BST = (function (_super) {
135
89
  }
136
90
  }
137
91
  return inserted;
138
- };
139
- BST.prototype.addMany = function (idsOrNodes, data, isBalanceAdd) {
140
- var _this = this;
141
- if (isBalanceAdd === void 0) { isBalanceAdd = false; }
92
+ }
93
+ addMany(keysOrNodes, data, isBalanceAdd = false) {
142
94
  function hasNoNull(arr) {
143
95
  return arr.indexOf(null) === -1;
144
96
  }
145
- if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
146
- return _super.prototype.addMany.call(this, idsOrNodes, data);
97
+ if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
98
+ return super.addMany(keysOrNodes, data);
147
99
  }
148
- var inserted = [];
149
- var combinedArr = idsOrNodes.map(function (value, index) { return [value, data === null || data === void 0 ? void 0 : data[index]]; });
150
- var sorted = [];
100
+ const inserted = [];
101
+ const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
102
+ let sorted = [];
151
103
  function isNodeOrNullTuple(arr) {
152
- var e_1, _a;
153
- try {
154
- for (var arr_1 = __values(arr), arr_1_1 = arr_1.next(); !arr_1_1.done; arr_1_1 = arr_1.next()) {
155
- var _b = __read(arr_1_1.value, 1), idOrNode = _b[0];
156
- if (idOrNode instanceof BSTNode)
157
- return true;
158
- }
159
- }
160
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
161
- finally {
162
- try {
163
- if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) _a.call(arr_1);
164
- }
165
- finally { if (e_1) throw e_1.error; }
166
- }
104
+ for (const [keyOrNode] of arr)
105
+ if (keyOrNode instanceof BSTNode)
106
+ return true;
167
107
  return false;
168
108
  }
169
- function isBinaryTreeIdOrNullTuple(arr) {
170
- var e_2, _a;
171
- try {
172
- for (var arr_2 = __values(arr), arr_2_1 = arr_2.next(); !arr_2_1.done; arr_2_1 = arr_2.next()) {
173
- var _b = __read(arr_2_1.value, 1), idOrNode = _b[0];
174
- if (typeof idOrNode === 'number')
175
- return true;
176
- }
177
- }
178
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
179
- finally {
180
- try {
181
- if (arr_2_1 && !arr_2_1.done && (_a = arr_2.return)) _a.call(arr_2);
182
- }
183
- finally { if (e_2) throw e_2.error; }
184
- }
109
+ function isBinaryTreeKeyOrNullTuple(arr) {
110
+ for (const [keyOrNode] of arr)
111
+ if (typeof keyOrNode === 'number')
112
+ return true;
185
113
  return false;
186
114
  }
187
- var sortedIdsOrNodes = [], sortedData = [];
115
+ let sortedKeysOrNodes = [], sortedData = [];
188
116
  if (isNodeOrNullTuple(combinedArr)) {
189
- sorted = combinedArr.sort(function (a, b) { return a[0].id - b[0].id; });
117
+ sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
190
118
  }
191
- else if (isBinaryTreeIdOrNullTuple(combinedArr)) {
192
- sorted = combinedArr.sort(function (a, b) { return a[0] - b[0]; });
119
+ else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
120
+ sorted = combinedArr.sort((a, b) => a[0] - b[0]);
193
121
  }
194
122
  else {
195
- throw new Error('Invalid input idsOrNodes');
123
+ throw new Error('Invalid input keysOrNodes');
196
124
  }
197
- sortedIdsOrNodes = sorted.map(function (_a) {
198
- var _b = __read(_a, 1), idOrNode = _b[0];
199
- return idOrNode;
200
- });
201
- sortedData = sorted.map(function (_a) {
202
- var _b = __read(_a, 2), val = _b[1];
203
- return val;
204
- });
205
- var recursive = function (arr, data) {
125
+ sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
126
+ sortedData = sorted.map(([, val]) => val);
127
+ const recursive = (arr, data) => {
206
128
  if (arr.length === 0)
207
129
  return;
208
- var mid = Math.floor((arr.length - 1) / 2);
209
- var newNode = _this.add(arr[mid], data === null || data === void 0 ? void 0 : data[mid]);
130
+ const mid = Math.floor((arr.length - 1) / 2);
131
+ const newNode = this.add(arr[mid], data === null || data === void 0 ? void 0 : data[mid]);
210
132
  inserted.push(newNode);
211
133
  recursive(arr.slice(0, mid), data === null || data === void 0 ? void 0 : data.slice(0, mid));
212
134
  recursive(arr.slice(mid + 1), data === null || data === void 0 ? void 0 : data.slice(mid + 1));
213
135
  };
214
- var iterative = function () {
215
- var n = sorted.length;
216
- var stack = [[0, n - 1]];
136
+ const iterative = () => {
137
+ const n = sorted.length;
138
+ const stack = [[0, n - 1]];
217
139
  while (stack.length > 0) {
218
- var popped = stack.pop();
140
+ const popped = stack.pop();
219
141
  if (popped) {
220
- var _a = __read(popped, 2), l = _a[0], r = _a[1];
142
+ const [l, r] = popped;
221
143
  if (l <= r) {
222
- var m = l + Math.floor((r - l) / 2);
223
- var newNode = _this.add(sortedIdsOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
144
+ const m = l + Math.floor((r - l) / 2);
145
+ const newNode = this.add(sortedKeysOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
224
146
  inserted.push(newNode);
225
147
  stack.push([m + 1, r]);
226
148
  stack.push([l, m - 1]);
@@ -229,63 +151,60 @@ var BST = (function (_super) {
229
151
  }
230
152
  };
231
153
  if (this.loopType === types_1.LoopType.RECURSIVE) {
232
- recursive(sortedIdsOrNodes, sortedData);
154
+ recursive(sortedKeysOrNodes, sortedData);
233
155
  }
234
156
  else {
235
157
  iterative();
236
158
  }
237
159
  return inserted;
238
- };
239
- BST.prototype.get = function (nodeProperty, propertyName) {
160
+ }
161
+ get(nodeProperty, propertyName = 'key') {
240
162
  var _a;
241
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
242
163
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
243
- };
244
- BST.prototype.lastKey = function () {
164
+ }
165
+ lastKey() {
245
166
  var _a, _b, _c, _d, _e, _f;
246
167
  if (this._compare(0, 1) === types_1.CP.lt)
247
- return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
168
+ return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
248
169
  else if (this._compare(0, 1) === types_1.CP.gt)
249
- return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
170
+ return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
250
171
  else
251
- return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
252
- };
253
- BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
254
- var _this = this;
255
- if (propertyName === void 0) { propertyName = 'id'; }
172
+ return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
173
+ }
174
+ getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
256
175
  if (!this.root)
257
176
  return [];
258
- var result = [];
177
+ const result = [];
259
178
  if (this.loopType === types_1.LoopType.RECURSIVE) {
260
- var _traverse_1 = function (cur) {
261
- if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
179
+ const _traverse = (cur) => {
180
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
262
181
  return;
263
182
  if (!cur.left && !cur.right)
264
183
  return;
265
- if (propertyName === 'id') {
266
- if (_this._compare(cur.id, nodeProperty) === types_1.CP.gt)
267
- cur.left && _traverse_1(cur.left);
268
- if (_this._compare(cur.id, nodeProperty) === types_1.CP.lt)
269
- cur.right && _traverse_1(cur.right);
184
+ if (propertyName === 'key') {
185
+ if (this._compare(cur.key, nodeProperty) === types_1.CP.gt)
186
+ cur.left && _traverse(cur.left);
187
+ if (this._compare(cur.key, nodeProperty) === types_1.CP.lt)
188
+ cur.right && _traverse(cur.right);
270
189
  }
271
190
  else {
272
- cur.left && _traverse_1(cur.left);
273
- cur.right && _traverse_1(cur.right);
191
+ cur.left && _traverse(cur.left);
192
+ cur.right && _traverse(cur.right);
274
193
  }
275
194
  };
276
- _traverse_1(this.root);
195
+ _traverse(this.root);
277
196
  }
278
197
  else {
279
- var queue = [this.root];
198
+ const queue = [this.root];
280
199
  while (queue.length > 0) {
281
- var cur = queue.shift();
200
+ const cur = queue.shift();
282
201
  if (cur) {
283
202
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
284
203
  return result;
285
- if (propertyName === 'id') {
286
- if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
204
+ if (propertyName === 'key') {
205
+ if (this._compare(cur.key, nodeProperty) === types_1.CP.gt)
287
206
  cur.left && queue.push(cur.left);
288
- if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
207
+ if (this._compare(cur.key, nodeProperty) === types_1.CP.lt)
289
208
  cur.right && queue.push(cur.right);
290
209
  }
291
210
  else {
@@ -296,62 +215,60 @@ var BST = (function (_super) {
296
215
  }
297
216
  }
298
217
  return result;
299
- };
300
- BST.prototype.lesserSum = function (beginNode, propertyName) {
301
- var _this = this;
302
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
218
+ }
219
+ lesserSum(beginNode, propertyName = 'key') {
303
220
  if (typeof beginNode === 'number')
304
- beginNode = this.get(beginNode, 'id');
221
+ beginNode = this.get(beginNode, 'key');
305
222
  if (!beginNode)
306
223
  return 0;
307
224
  if (!this.root)
308
225
  return 0;
309
- var id = beginNode.id;
310
- var getSumByPropertyName = function (cur) {
311
- var needSum;
226
+ const key = beginNode.key;
227
+ const getSumByPropertyName = (cur) => {
228
+ let needSum;
312
229
  switch (propertyName) {
313
- case 'id':
314
- needSum = cur.id;
230
+ case 'key':
231
+ needSum = cur.key;
315
232
  break;
316
233
  default:
317
- needSum = cur.id;
234
+ needSum = cur.key;
318
235
  break;
319
236
  }
320
237
  return needSum;
321
238
  };
322
- var sum = 0;
239
+ let sum = 0;
323
240
  if (this.loopType === types_1.LoopType.RECURSIVE) {
324
- var _traverse_2 = function (cur) {
325
- var compared = _this._compare(cur.id, id);
241
+ const _traverse = (cur) => {
242
+ const compared = this._compare(cur.key, key);
326
243
  if (compared === types_1.CP.eq) {
327
244
  if (cur.right)
328
- sum += _this.subTreeSum(cur.right, propertyName);
245
+ sum += this.subTreeSum(cur.right, propertyName);
329
246
  return;
330
247
  }
331
248
  else if (compared === types_1.CP.lt) {
332
249
  if (cur.left)
333
- sum += _this.subTreeSum(cur.left, propertyName);
250
+ sum += this.subTreeSum(cur.left, propertyName);
334
251
  sum += getSumByPropertyName(cur);
335
252
  if (cur.right)
336
- _traverse_2(cur.right);
253
+ _traverse(cur.right);
337
254
  else
338
255
  return;
339
256
  }
340
257
  else {
341
258
  if (cur.left)
342
- _traverse_2(cur.left);
259
+ _traverse(cur.left);
343
260
  else
344
261
  return;
345
262
  }
346
263
  };
347
- _traverse_2(this.root);
264
+ _traverse(this.root);
348
265
  }
349
266
  else {
350
- var queue = [this.root];
267
+ const queue = [this.root];
351
268
  while (queue.length > 0) {
352
- var cur = queue.shift();
269
+ const cur = queue.shift();
353
270
  if (cur) {
354
- var compared = this._compare(cur.id, id);
271
+ const compared = this._compare(cur.key, key);
355
272
  if (compared === types_1.CP.eq) {
356
273
  if (cur.right)
357
274
  sum += this.subTreeSum(cur.right, propertyName);
@@ -376,88 +293,85 @@ var BST = (function (_super) {
376
293
  }
377
294
  }
378
295
  return sum;
379
- };
380
- BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
381
- var _this = this;
382
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
296
+ }
297
+ allGreaterNodesAdd(node, delta, propertyName = 'key') {
383
298
  if (typeof node === 'number')
384
- node = this.get(node, 'id');
299
+ node = this.get(node, 'key');
385
300
  if (!node)
386
301
  return false;
387
- var id = node.id;
302
+ const key = node.key;
388
303
  if (!this.root)
389
304
  return false;
390
- var _sumByPropertyName = function (cur) {
305
+ const _sumByPropertyName = (cur) => {
391
306
  switch (propertyName) {
392
- case 'id':
393
- cur.id += delta;
307
+ case 'key':
308
+ cur.key += delta;
394
309
  break;
395
310
  default:
396
- cur.id += delta;
311
+ cur.key += delta;
397
312
  break;
398
313
  }
399
314
  };
400
315
  if (this.loopType === types_1.LoopType.RECURSIVE) {
401
- var _traverse_3 = function (cur) {
402
- var compared = _this._compare(cur.id, id);
316
+ const _traverse = (cur) => {
317
+ const compared = this._compare(cur.key, key);
403
318
  if (compared === types_1.CP.gt)
404
319
  _sumByPropertyName(cur);
405
320
  if (!cur.left && !cur.right)
406
321
  return;
407
- if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
408
- _traverse_3(cur.left);
409
- if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
410
- _traverse_3(cur.right);
322
+ if (cur.left && this._compare(cur.left.key, key) === types_1.CP.gt)
323
+ _traverse(cur.left);
324
+ if (cur.right && this._compare(cur.right.key, key) === types_1.CP.gt)
325
+ _traverse(cur.right);
411
326
  };
412
- _traverse_3(this.root);
327
+ _traverse(this.root);
413
328
  return true;
414
329
  }
415
330
  else {
416
- var queue = [this.root];
331
+ const queue = [this.root];
417
332
  while (queue.length > 0) {
418
- var cur = queue.shift();
333
+ const cur = queue.shift();
419
334
  if (cur) {
420
- var compared = this._compare(cur.id, id);
335
+ const compared = this._compare(cur.key, key);
421
336
  if (compared === types_1.CP.gt)
422
337
  _sumByPropertyName(cur);
423
- if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
338
+ if (cur.left && this._compare(cur.left.key, key) === types_1.CP.gt)
424
339
  queue.push(cur.left);
425
- if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
340
+ if (cur.right && this._compare(cur.right.key, key) === types_1.CP.gt)
426
341
  queue.push(cur.right);
427
342
  }
428
343
  }
429
344
  return true;
430
345
  }
431
- };
432
- BST.prototype.perfectlyBalance = function () {
433
- var _this = this;
434
- var sorted = this.DFS('in', 'node'), n = sorted.length;
346
+ }
347
+ perfectlyBalance() {
348
+ const sorted = this.dfs('in', 'node'), n = sorted.length;
435
349
  this.clear();
436
350
  if (sorted.length < 1)
437
351
  return false;
438
352
  if (this.loopType === types_1.LoopType.RECURSIVE) {
439
- var buildBalanceBST_1 = function (l, r) {
353
+ const buildBalanceBST = (l, r) => {
440
354
  if (l > r)
441
355
  return;
442
- var m = l + Math.floor((r - l) / 2);
443
- var midNode = sorted[m];
444
- _this.add(midNode.id, midNode.val);
445
- buildBalanceBST_1(l, m - 1);
446
- buildBalanceBST_1(m + 1, r);
356
+ const m = l + Math.floor((r - l) / 2);
357
+ const midNode = sorted[m];
358
+ this.add(midNode.key, midNode.val);
359
+ buildBalanceBST(l, m - 1);
360
+ buildBalanceBST(m + 1, r);
447
361
  };
448
- buildBalanceBST_1(0, n - 1);
362
+ buildBalanceBST(0, n - 1);
449
363
  return true;
450
364
  }
451
365
  else {
452
- var stack = [[0, n - 1]];
366
+ const stack = [[0, n - 1]];
453
367
  while (stack.length > 0) {
454
- var popped = stack.pop();
368
+ const popped = stack.pop();
455
369
  if (popped) {
456
- var _a = __read(popped, 2), l = _a[0], r = _a[1];
370
+ const [l, r] = popped;
457
371
  if (l <= r) {
458
- var m = l + Math.floor((r - l) / 2);
459
- var midNode = sorted[m];
460
- this.add(midNode.id, midNode.val);
372
+ const m = l + Math.floor((r - l) / 2);
373
+ const midNode = sorted[m];
374
+ this.add(midNode.key, midNode.val);
461
375
  stack.push([m + 1, r]);
462
376
  stack.push([l, m - 1]);
463
377
  }
@@ -465,27 +379,27 @@ var BST = (function (_super) {
465
379
  }
466
380
  return true;
467
381
  }
468
- };
469
- BST.prototype.isAVLBalanced = function () {
382
+ }
383
+ isAVLBalanced() {
470
384
  var _a, _b;
471
385
  if (!this.root)
472
386
  return true;
473
- var balanced = true;
387
+ let balanced = true;
474
388
  if (this.loopType === types_1.LoopType.RECURSIVE) {
475
- var _height_1 = function (cur) {
389
+ const _height = (cur) => {
476
390
  if (!cur)
477
391
  return 0;
478
- var leftHeight = _height_1(cur.left), rightHeight = _height_1(cur.right);
392
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
479
393
  if (Math.abs(leftHeight - rightHeight) > 1)
480
394
  balanced = false;
481
395
  return Math.max(leftHeight, rightHeight) + 1;
482
396
  };
483
- _height_1(this.root);
397
+ _height(this.root);
484
398
  }
485
399
  else {
486
- var stack = [];
487
- var node = this.root, last = null;
488
- var depths = new Map();
400
+ const stack = [];
401
+ let node = this.root, last = null;
402
+ const depths = new Map();
489
403
  while (stack.length > 0 || node) {
490
404
  if (node) {
491
405
  stack.push(node);
@@ -496,8 +410,8 @@ var BST = (function (_super) {
496
410
  if (!node.right || last === node.right) {
497
411
  node = stack.pop();
498
412
  if (node) {
499
- var left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
500
- var right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
413
+ const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
414
+ const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
501
415
  if (Math.abs(left - right) > 1)
502
416
  return false;
503
417
  depths.set(node, 1 + Math.max(left, right));
@@ -511,17 +425,16 @@ var BST = (function (_super) {
511
425
  }
512
426
  }
513
427
  return balanced;
514
- };
515
- BST.prototype._compare = function (a, b) {
516
- var compared = this._comparator(a, b);
428
+ }
429
+ _compare(a, b) {
430
+ const compared = this._comparator(a, b);
517
431
  if (compared > 0)
518
432
  return types_1.CP.gt;
519
433
  else if (compared < 0)
520
434
  return types_1.CP.lt;
521
435
  else
522
436
  return types_1.CP.eq;
523
- };
524
- return BST;
525
- }(binary_tree_1.BinaryTree));
437
+ }
438
+ }
526
439
  exports.BST = BST;
527
440
  //# sourceMappingURL=bst.js.map