data-structure-typed 1.19.3 → 1.19.4
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/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- 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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super(options);
|
|
16
|
+
this._comparator = (a, b) => a - b;
|
|
54
17
|
if (options !== undefined) {
|
|
55
|
-
|
|
18
|
+
const { comparator } = options;
|
|
56
19
|
if (comparator !== undefined) {
|
|
57
|
-
|
|
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
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
153
|
+
const result = [];
|
|
193
154
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
194
|
-
|
|
195
|
-
if (
|
|
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 (
|
|
201
|
-
cur.left &&
|
|
202
|
-
if (
|
|
203
|
-
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 &&
|
|
207
|
-
cur.right &&
|
|
167
|
+
cur.left && _traverse(cur.left);
|
|
168
|
+
cur.right && _traverse(cur.right);
|
|
208
169
|
}
|
|
209
170
|
};
|
|
210
|
-
|
|
171
|
+
_traverse(this.root);
|
|
211
172
|
}
|
|
212
173
|
else {
|
|
213
|
-
|
|
174
|
+
const queue = [this.root];
|
|
214
175
|
while (queue.length > 0) {
|
|
215
|
-
|
|
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
|
-
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
226
|
+
let sum = 0;
|
|
267
227
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
268
|
-
|
|
269
|
-
|
|
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 +=
|
|
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 +=
|
|
237
|
+
sum += this.subTreeSum(cur.left, propertyName);
|
|
278
238
|
sum += getSumByPropertyName(cur);
|
|
279
239
|
if (cur.right)
|
|
280
|
-
|
|
240
|
+
_traverse(cur.right);
|
|
281
241
|
else
|
|
282
242
|
return;
|
|
283
243
|
}
|
|
284
244
|
else {
|
|
285
245
|
if (cur.left)
|
|
286
|
-
|
|
246
|
+
_traverse(cur.left);
|
|
287
247
|
else
|
|
288
248
|
return;
|
|
289
249
|
}
|
|
290
250
|
};
|
|
291
|
-
|
|
251
|
+
_traverse(this.root);
|
|
292
252
|
}
|
|
293
253
|
else {
|
|
294
|
-
|
|
254
|
+
const queue = [this.root];
|
|
295
255
|
while (queue.length > 0) {
|
|
296
|
-
|
|
256
|
+
const cur = queue.shift();
|
|
297
257
|
if (cur) {
|
|
298
|
-
|
|
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
|
-
|
|
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
|
-
|
|
302
|
+
const id = node.id;
|
|
344
303
|
if (!this.root)
|
|
345
304
|
return false;
|
|
346
|
-
|
|
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
|
-
|
|
358
|
-
|
|
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 &&
|
|
364
|
-
|
|
365
|
-
if (cur.right &&
|
|
366
|
-
|
|
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
|
-
|
|
327
|
+
_traverse(this.root);
|
|
369
328
|
return true;
|
|
370
329
|
}
|
|
371
330
|
else {
|
|
372
|
-
|
|
331
|
+
const queue = [this.root];
|
|
373
332
|
while (queue.length > 0) {
|
|
374
|
-
|
|
333
|
+
const cur = queue.shift();
|
|
375
334
|
if (cur) {
|
|
376
|
-
|
|
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
|
-
|
|
403
|
-
|
|
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
|
-
|
|
367
|
+
const buildBalanceBST = (l, r) => {
|
|
410
368
|
if (l > r)
|
|
411
369
|
return;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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
|
-
|
|
376
|
+
buildBalanceBST(0, n - 1);
|
|
419
377
|
return true;
|
|
420
378
|
}
|
|
421
379
|
else {
|
|
422
|
-
|
|
380
|
+
const stack = [[0, n - 1]];
|
|
423
381
|
while (stack.length > 0) {
|
|
424
|
-
|
|
382
|
+
const popped = stack.pop();
|
|
425
383
|
if (popped) {
|
|
426
|
-
|
|
384
|
+
const [l, r] = popped;
|
|
427
385
|
if (l <= r) {
|
|
428
|
-
|
|
429
|
-
|
|
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
|
-
|
|
401
|
+
isAVLBalanced() {
|
|
444
402
|
var _a, _b;
|
|
445
403
|
if (!this.root)
|
|
446
404
|
return true;
|
|
447
|
-
|
|
405
|
+
let balanced = true;
|
|
448
406
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
449
|
-
|
|
407
|
+
const _height = (cur) => {
|
|
450
408
|
if (!cur)
|
|
451
409
|
return 0;
|
|
452
|
-
|
|
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
|
-
|
|
415
|
+
_height(this.root);
|
|
458
416
|
}
|
|
459
417
|
else {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
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
|
-
|
|
474
|
-
|
|
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
|
-
|
|
498
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return _super.call(this, options) || this;
|
|
20
|
+
class RBTree extends bst_1.BST {
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super(options);
|
|
46
23
|
}
|
|
47
|
-
|
|
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
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
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;
|