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.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- 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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
}(binary_tree_1.BinaryTreeNode));
|
|
10
|
+
}
|
|
55
11
|
exports.BSTNode = BSTNode;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
17
|
+
const { comparator } = options;
|
|
63
18
|
if (comparator !== undefined) {
|
|
64
|
-
|
|
19
|
+
this._comparator = comparator;
|
|
65
20
|
}
|
|
66
21
|
}
|
|
67
|
-
return _this;
|
|
68
22
|
}
|
|
69
|
-
|
|
70
|
-
return new BSTNode(
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
newNode =
|
|
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
|
|
79
|
-
newNode = this.createNode(
|
|
32
|
+
else if (typeof keyOrNode === 'number') {
|
|
33
|
+
newNode = this.createNode(keyOrNode, val);
|
|
80
34
|
}
|
|
81
|
-
else if (
|
|
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
|
-
|
|
91
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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(
|
|
146
|
-
return
|
|
97
|
+
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
98
|
+
return super.addMany(keysOrNodes, data);
|
|
147
99
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
115
|
+
let sortedKeysOrNodes = [], sortedData = [];
|
|
188
116
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
189
|
-
sorted = combinedArr.sort(
|
|
117
|
+
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
190
118
|
}
|
|
191
|
-
else if (
|
|
192
|
-
sorted = combinedArr.sort(
|
|
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
|
|
123
|
+
throw new Error('Invalid input keysOrNodes');
|
|
196
124
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
|
|
209
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
136
|
+
const iterative = () => {
|
|
137
|
+
const n = sorted.length;
|
|
138
|
+
const stack = [[0, n - 1]];
|
|
217
139
|
while (stack.length > 0) {
|
|
218
|
-
|
|
140
|
+
const popped = stack.pop();
|
|
219
141
|
if (popped) {
|
|
220
|
-
|
|
142
|
+
const [l, r] = popped;
|
|
221
143
|
if (l <= r) {
|
|
222
|
-
|
|
223
|
-
|
|
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(
|
|
154
|
+
recursive(sortedKeysOrNodes, sortedData);
|
|
233
155
|
}
|
|
234
156
|
else {
|
|
235
157
|
iterative();
|
|
236
158
|
}
|
|
237
159
|
return inserted;
|
|
238
|
-
}
|
|
239
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
252
|
-
}
|
|
253
|
-
|
|
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
|
-
|
|
177
|
+
const result = [];
|
|
259
178
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
260
|
-
|
|
261
|
-
if (
|
|
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 === '
|
|
266
|
-
if (
|
|
267
|
-
cur.left &&
|
|
268
|
-
if (
|
|
269
|
-
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 &&
|
|
273
|
-
cur.right &&
|
|
191
|
+
cur.left && _traverse(cur.left);
|
|
192
|
+
cur.right && _traverse(cur.right);
|
|
274
193
|
}
|
|
275
194
|
};
|
|
276
|
-
|
|
195
|
+
_traverse(this.root);
|
|
277
196
|
}
|
|
278
197
|
else {
|
|
279
|
-
|
|
198
|
+
const queue = [this.root];
|
|
280
199
|
while (queue.length > 0) {
|
|
281
|
-
|
|
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 === '
|
|
286
|
-
if (this._compare(cur.
|
|
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.
|
|
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
|
-
|
|
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, '
|
|
221
|
+
beginNode = this.get(beginNode, 'key');
|
|
305
222
|
if (!beginNode)
|
|
306
223
|
return 0;
|
|
307
224
|
if (!this.root)
|
|
308
225
|
return 0;
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
226
|
+
const key = beginNode.key;
|
|
227
|
+
const getSumByPropertyName = (cur) => {
|
|
228
|
+
let needSum;
|
|
312
229
|
switch (propertyName) {
|
|
313
|
-
case '
|
|
314
|
-
needSum = cur.
|
|
230
|
+
case 'key':
|
|
231
|
+
needSum = cur.key;
|
|
315
232
|
break;
|
|
316
233
|
default:
|
|
317
|
-
needSum = cur.
|
|
234
|
+
needSum = cur.key;
|
|
318
235
|
break;
|
|
319
236
|
}
|
|
320
237
|
return needSum;
|
|
321
238
|
};
|
|
322
|
-
|
|
239
|
+
let sum = 0;
|
|
323
240
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
324
|
-
|
|
325
|
-
|
|
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 +=
|
|
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 +=
|
|
250
|
+
sum += this.subTreeSum(cur.left, propertyName);
|
|
334
251
|
sum += getSumByPropertyName(cur);
|
|
335
252
|
if (cur.right)
|
|
336
|
-
|
|
253
|
+
_traverse(cur.right);
|
|
337
254
|
else
|
|
338
255
|
return;
|
|
339
256
|
}
|
|
340
257
|
else {
|
|
341
258
|
if (cur.left)
|
|
342
|
-
|
|
259
|
+
_traverse(cur.left);
|
|
343
260
|
else
|
|
344
261
|
return;
|
|
345
262
|
}
|
|
346
263
|
};
|
|
347
|
-
|
|
264
|
+
_traverse(this.root);
|
|
348
265
|
}
|
|
349
266
|
else {
|
|
350
|
-
|
|
267
|
+
const queue = [this.root];
|
|
351
268
|
while (queue.length > 0) {
|
|
352
|
-
|
|
269
|
+
const cur = queue.shift();
|
|
353
270
|
if (cur) {
|
|
354
|
-
|
|
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
|
-
|
|
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, '
|
|
299
|
+
node = this.get(node, 'key');
|
|
385
300
|
if (!node)
|
|
386
301
|
return false;
|
|
387
|
-
|
|
302
|
+
const key = node.key;
|
|
388
303
|
if (!this.root)
|
|
389
304
|
return false;
|
|
390
|
-
|
|
305
|
+
const _sumByPropertyName = (cur) => {
|
|
391
306
|
switch (propertyName) {
|
|
392
|
-
case '
|
|
393
|
-
cur.
|
|
307
|
+
case 'key':
|
|
308
|
+
cur.key += delta;
|
|
394
309
|
break;
|
|
395
310
|
default:
|
|
396
|
-
cur.
|
|
311
|
+
cur.key += delta;
|
|
397
312
|
break;
|
|
398
313
|
}
|
|
399
314
|
};
|
|
400
315
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
401
|
-
|
|
402
|
-
|
|
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 &&
|
|
408
|
-
|
|
409
|
-
if (cur.right &&
|
|
410
|
-
|
|
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
|
-
|
|
327
|
+
_traverse(this.root);
|
|
413
328
|
return true;
|
|
414
329
|
}
|
|
415
330
|
else {
|
|
416
|
-
|
|
331
|
+
const queue = [this.root];
|
|
417
332
|
while (queue.length > 0) {
|
|
418
|
-
|
|
333
|
+
const cur = queue.shift();
|
|
419
334
|
if (cur) {
|
|
420
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
433
|
-
|
|
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
|
-
|
|
353
|
+
const buildBalanceBST = (l, r) => {
|
|
440
354
|
if (l > r)
|
|
441
355
|
return;
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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
|
-
|
|
362
|
+
buildBalanceBST(0, n - 1);
|
|
449
363
|
return true;
|
|
450
364
|
}
|
|
451
365
|
else {
|
|
452
|
-
|
|
366
|
+
const stack = [[0, n - 1]];
|
|
453
367
|
while (stack.length > 0) {
|
|
454
|
-
|
|
368
|
+
const popped = stack.pop();
|
|
455
369
|
if (popped) {
|
|
456
|
-
|
|
370
|
+
const [l, r] = popped;
|
|
457
371
|
if (l <= r) {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
this.add(midNode.
|
|
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
|
-
|
|
382
|
+
}
|
|
383
|
+
isAVLBalanced() {
|
|
470
384
|
var _a, _b;
|
|
471
385
|
if (!this.root)
|
|
472
386
|
return true;
|
|
473
|
-
|
|
387
|
+
let balanced = true;
|
|
474
388
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
475
|
-
|
|
389
|
+
const _height = (cur) => {
|
|
476
390
|
if (!cur)
|
|
477
391
|
return 0;
|
|
478
|
-
|
|
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
|
-
|
|
397
|
+
_height(this.root);
|
|
484
398
|
}
|
|
485
399
|
else {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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
|
-
|
|
500
|
-
|
|
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
|
-
|
|
516
|
-
|
|
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
|
-
|
|
525
|
-
}(binary_tree_1.BinaryTree));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
526
439
|
exports.BST = BST;
|
|
527
440
|
//# sourceMappingURL=bst.js.map
|