data-structure-typed 0.9.16 → 1.3.0
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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +96 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- 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 +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +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/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,108 +1,80 @@
|
|
|
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
|
-
exports.BST = exports.BSTNode =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
CP[CP["gt"] = 1] = "gt";
|
|
41
|
-
})(CP = exports.CP || (exports.CP = {}));
|
|
42
|
-
var BSTNode = /** @class */ (function (_super) {
|
|
43
|
-
__extends(BSTNode, _super);
|
|
44
|
-
function BSTNode() {
|
|
45
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
3
|
+
exports.BST = exports.BSTNode = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const binary_tree_1 = require("./binary-tree");
|
|
6
|
+
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
7
|
+
constructor(id, val) {
|
|
8
|
+
super(id, val);
|
|
46
9
|
}
|
|
47
|
-
|
|
48
|
-
return new BSTNode(this.id, this.val, this.count);
|
|
49
|
-
};
|
|
50
|
-
return BSTNode;
|
|
51
|
-
}(binary_tree_1.BinaryTreeNode));
|
|
10
|
+
}
|
|
52
11
|
exports.BSTNode = BSTNode;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
12
|
+
class BST extends binary_tree_1.BinaryTree {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor function initializes a binary search tree object with an optional comparator function.
|
|
15
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super(options);
|
|
19
|
+
this._comparator = (a, b) => a - b;
|
|
58
20
|
if (options !== undefined) {
|
|
59
|
-
|
|
21
|
+
const { comparator } = options;
|
|
60
22
|
if (comparator !== undefined) {
|
|
61
|
-
|
|
23
|
+
this._comparator = comparator;
|
|
62
24
|
}
|
|
63
25
|
}
|
|
64
|
-
return _this;
|
|
65
26
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The function creates a new binary search tree node with the given id and value.
|
|
29
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
30
|
+
* identify each node in the binary tree.
|
|
31
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
32
|
+
* that will be stored in the node.
|
|
33
|
+
* @returns a new instance of the BSTNode class with the specified id and value.
|
|
34
|
+
*/
|
|
35
|
+
createNode(id, val) {
|
|
36
|
+
return new BSTNode(id, val);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
|
|
40
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
|
|
41
|
+
* is of type `BinaryTreeNodeId`.
|
|
42
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
|
|
43
|
+
* the value associated with the node.
|
|
44
|
+
* @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
|
|
45
|
+
* If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
|
|
46
|
+
*/
|
|
47
|
+
add(id, val) {
|
|
48
|
+
// TODO support node as a param
|
|
49
|
+
let inserted = null;
|
|
50
|
+
const newNode = this.createNode(id, val);
|
|
74
51
|
if (this.root === null) {
|
|
75
|
-
this.
|
|
76
|
-
this.size
|
|
77
|
-
this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 1;
|
|
52
|
+
this._setRoot(newNode);
|
|
53
|
+
this._setSize(this.size + 1);
|
|
78
54
|
inserted = (this.root);
|
|
79
55
|
}
|
|
80
56
|
else {
|
|
81
|
-
|
|
82
|
-
|
|
57
|
+
let cur = this.root;
|
|
58
|
+
let traversing = true;
|
|
83
59
|
while (traversing) {
|
|
84
60
|
if (cur !== null && newNode !== null) {
|
|
85
|
-
if (this._compare(cur.id, id) === CP.eq) {
|
|
61
|
+
if (this._compare(cur.id, id) === types_1.CP.eq) {
|
|
86
62
|
if (newNode) {
|
|
87
|
-
cur.count += newNode.count;
|
|
88
|
-
this.count += newNode.count;
|
|
89
63
|
cur.val = newNode.val;
|
|
90
64
|
}
|
|
91
65
|
//Duplicates are not accepted.
|
|
92
66
|
traversing = false;
|
|
93
67
|
inserted = cur;
|
|
94
68
|
}
|
|
95
|
-
else if (this._compare(cur.id, id) === CP.gt) {
|
|
69
|
+
else if (this._compare(cur.id, id) === types_1.CP.gt) {
|
|
96
70
|
// Traverse left of the node
|
|
97
71
|
if (cur.left === undefined) {
|
|
98
72
|
if (newNode) {
|
|
99
73
|
newNode.parent = cur;
|
|
100
|
-
newNode.familyPosition = binary_tree_1.FamilyPosition.left;
|
|
101
74
|
}
|
|
102
75
|
//Add to the left of the current node
|
|
103
76
|
cur.left = newNode;
|
|
104
|
-
this.size
|
|
105
|
-
this.count += newNode.count;
|
|
77
|
+
this._setSize(this.size + 1);
|
|
106
78
|
traversing = false;
|
|
107
79
|
inserted = cur.left;
|
|
108
80
|
}
|
|
@@ -112,17 +84,15 @@ var BST = /** @class */ (function (_super) {
|
|
|
112
84
|
cur = cur.left;
|
|
113
85
|
}
|
|
114
86
|
}
|
|
115
|
-
else if (this._compare(cur.id, id) === CP.lt) {
|
|
87
|
+
else if (this._compare(cur.id, id) === types_1.CP.lt) {
|
|
116
88
|
// Traverse right of the node
|
|
117
89
|
if (cur.right === undefined) {
|
|
118
90
|
if (newNode) {
|
|
119
91
|
newNode.parent = cur;
|
|
120
|
-
newNode.familyPosition = binary_tree_1.FamilyPosition.right;
|
|
121
92
|
}
|
|
122
93
|
//Add to the right of the current node
|
|
123
94
|
cur.right = newNode;
|
|
124
|
-
this.size
|
|
125
|
-
this.count += newNode.count;
|
|
95
|
+
this._setSize(this.size + 1);
|
|
126
96
|
traversing = false;
|
|
127
97
|
inserted = (cur.right);
|
|
128
98
|
}
|
|
@@ -139,108 +109,82 @@ var BST = /** @class */ (function (_super) {
|
|
|
139
109
|
}
|
|
140
110
|
}
|
|
141
111
|
return inserted;
|
|
142
|
-
}
|
|
143
|
-
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
115
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
116
|
+
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
117
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
118
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
|
|
119
|
+
* @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
|
|
120
|
+
*/
|
|
121
|
+
get(nodeProperty, propertyName) {
|
|
144
122
|
var _a;
|
|
145
123
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
146
124
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
147
|
-
}
|
|
148
|
-
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
128
|
+
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
129
|
+
* @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
|
|
130
|
+
* the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
|
|
131
|
+
* equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
132
|
+
*/
|
|
133
|
+
lastKey() {
|
|
149
134
|
var _a, _b, _c, _d, _e, _f;
|
|
150
|
-
if (this._compare(0, 1) === CP.lt)
|
|
135
|
+
if (this._compare(0, 1) === types_1.CP.lt)
|
|
151
136
|
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
|
|
152
|
-
else if (this._compare(0, 1) === CP.gt)
|
|
137
|
+
else if (this._compare(0, 1) === types_1.CP.gt)
|
|
153
138
|
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
|
|
154
139
|
else
|
|
155
140
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
if (!curr.left) {
|
|
172
|
-
if (!parent) {
|
|
173
|
-
if (curr.right !== undefined)
|
|
174
|
-
this.root = curr.right;
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
switch (curr.familyPosition) {
|
|
178
|
-
case binary_tree_1.FamilyPosition.left:
|
|
179
|
-
parent.left = curr.right;
|
|
180
|
-
break;
|
|
181
|
-
case binary_tree_1.FamilyPosition.right:
|
|
182
|
-
parent.right = curr.right;
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
needBalanced = parent;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
190
|
-
if (leftSubTreeMax) {
|
|
191
|
-
var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
192
|
-
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
193
|
-
if (parentOfLeftSubTreeMax) {
|
|
194
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
195
|
-
parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
|
|
196
|
-
else
|
|
197
|
-
parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
|
|
198
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
this.size--;
|
|
203
|
-
this.count -= curr.count;
|
|
204
|
-
}
|
|
205
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
206
|
-
return bstDeletedResult;
|
|
207
|
-
};
|
|
208
|
-
BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
209
|
-
var _this = this;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
144
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
|
|
145
|
+
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
146
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
147
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
|
|
148
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
149
|
+
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
150
|
+
* is set to `true`, the function will return an array with only one node (if
|
|
151
|
+
* @returns an array of nodes (type N).
|
|
152
|
+
*/
|
|
153
|
+
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
210
154
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
211
155
|
if (!this.root)
|
|
212
156
|
return [];
|
|
213
|
-
|
|
214
|
-
if (this.
|
|
215
|
-
|
|
216
|
-
if (
|
|
157
|
+
const result = [];
|
|
158
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
159
|
+
const _traverse = (cur) => {
|
|
160
|
+
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
217
161
|
return;
|
|
218
162
|
if (!cur.left && !cur.right)
|
|
219
163
|
return;
|
|
220
164
|
if (propertyName === 'id') {
|
|
221
|
-
if (
|
|
222
|
-
cur.left &&
|
|
223
|
-
if (
|
|
224
|
-
cur.right &&
|
|
165
|
+
if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
|
|
166
|
+
cur.left && _traverse(cur.left);
|
|
167
|
+
if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
|
|
168
|
+
cur.right && _traverse(cur.right);
|
|
225
169
|
}
|
|
226
170
|
else {
|
|
227
|
-
cur.left &&
|
|
228
|
-
cur.right &&
|
|
171
|
+
cur.left && _traverse(cur.left);
|
|
172
|
+
cur.right && _traverse(cur.right);
|
|
229
173
|
}
|
|
230
174
|
};
|
|
231
|
-
|
|
175
|
+
_traverse(this.root);
|
|
232
176
|
}
|
|
233
177
|
else {
|
|
234
|
-
|
|
178
|
+
const queue = [this.root];
|
|
235
179
|
while (queue.length > 0) {
|
|
236
|
-
|
|
180
|
+
const cur = queue.shift();
|
|
237
181
|
if (cur) {
|
|
238
182
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
239
183
|
return result;
|
|
240
184
|
if (propertyName === 'id') {
|
|
241
|
-
if (this._compare(cur.id, nodeProperty) === CP.gt)
|
|
185
|
+
if (this._compare(cur.id, nodeProperty) === types_1.CP.gt)
|
|
242
186
|
cur.left && queue.push(cur.left);
|
|
243
|
-
if (this._compare(cur.id, nodeProperty) === CP.lt)
|
|
187
|
+
if (this._compare(cur.id, nodeProperty) === types_1.CP.lt)
|
|
244
188
|
cur.right && queue.push(cur.right);
|
|
245
189
|
}
|
|
246
190
|
else {
|
|
@@ -251,67 +195,77 @@ var BST = /** @class */ (function (_super) {
|
|
|
251
195
|
}
|
|
252
196
|
}
|
|
253
197
|
return result;
|
|
254
|
-
}
|
|
198
|
+
}
|
|
255
199
|
// --- start additional functions
|
|
256
|
-
|
|
257
|
-
|
|
200
|
+
/**
|
|
201
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
202
|
+
* less than a given node.
|
|
203
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
204
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
205
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
206
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
207
|
+
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
208
|
+
*/
|
|
209
|
+
lesserSum(beginNode, propertyName) {
|
|
258
210
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
211
|
+
if (typeof beginNode === 'number')
|
|
212
|
+
beginNode = this.get(beginNode, 'id');
|
|
213
|
+
if (!beginNode)
|
|
214
|
+
return 0;
|
|
259
215
|
if (!this.root)
|
|
260
216
|
return 0;
|
|
261
|
-
|
|
262
|
-
|
|
217
|
+
const id = beginNode.id;
|
|
218
|
+
const getSumByPropertyName = (cur) => {
|
|
219
|
+
let needSum;
|
|
263
220
|
switch (propertyName) {
|
|
264
221
|
case 'id':
|
|
265
222
|
needSum = cur.id;
|
|
266
223
|
break;
|
|
267
|
-
case 'count':
|
|
268
|
-
needSum = cur.count;
|
|
269
|
-
break;
|
|
270
224
|
default:
|
|
271
225
|
needSum = cur.id;
|
|
272
226
|
break;
|
|
273
227
|
}
|
|
274
228
|
return needSum;
|
|
275
229
|
};
|
|
276
|
-
|
|
277
|
-
if (this.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (compared === CP.eq) {
|
|
230
|
+
let sum = 0;
|
|
231
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
232
|
+
const _traverse = (cur) => {
|
|
233
|
+
const compared = this._compare(cur.id, id);
|
|
234
|
+
if (compared === types_1.CP.eq) {
|
|
281
235
|
if (cur.right)
|
|
282
|
-
sum +=
|
|
236
|
+
sum += this.subTreeSum(cur.right, propertyName);
|
|
283
237
|
return;
|
|
284
238
|
}
|
|
285
|
-
else if (compared === CP.lt) {
|
|
239
|
+
else if (compared === types_1.CP.lt) {
|
|
286
240
|
if (cur.left)
|
|
287
|
-
sum +=
|
|
241
|
+
sum += this.subTreeSum(cur.left, propertyName);
|
|
288
242
|
sum += getSumByPropertyName(cur);
|
|
289
243
|
if (cur.right)
|
|
290
|
-
|
|
244
|
+
_traverse(cur.right);
|
|
291
245
|
else
|
|
292
246
|
return;
|
|
293
247
|
}
|
|
294
248
|
else {
|
|
295
249
|
if (cur.left)
|
|
296
|
-
|
|
250
|
+
_traverse(cur.left);
|
|
297
251
|
else
|
|
298
252
|
return;
|
|
299
253
|
}
|
|
300
254
|
};
|
|
301
|
-
|
|
255
|
+
_traverse(this.root);
|
|
302
256
|
}
|
|
303
257
|
else {
|
|
304
|
-
|
|
258
|
+
const queue = [this.root];
|
|
305
259
|
while (queue.length > 0) {
|
|
306
|
-
|
|
260
|
+
const cur = queue.shift();
|
|
307
261
|
if (cur) {
|
|
308
|
-
|
|
309
|
-
if (compared === CP.eq) {
|
|
262
|
+
const compared = this._compare(cur.id, id);
|
|
263
|
+
if (compared === types_1.CP.eq) {
|
|
310
264
|
if (cur.right)
|
|
311
265
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
312
266
|
return sum;
|
|
313
267
|
}
|
|
314
|
-
else if (compared === CP.lt) { // todo maybe a bug
|
|
268
|
+
else if (compared === types_1.CP.lt) { // todo maybe a bug
|
|
315
269
|
if (cur.left)
|
|
316
270
|
sum += this.subTreeSum(cur.left, propertyName);
|
|
317
271
|
sum += getSumByPropertyName(cur);
|
|
@@ -330,84 +284,112 @@ var BST = /** @class */ (function (_super) {
|
|
|
330
284
|
}
|
|
331
285
|
}
|
|
332
286
|
return sum;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
290
|
+
* have a greater value than a given node.
|
|
291
|
+
* @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
292
|
+
* `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
293
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
294
|
+
* each greater node should be increased.
|
|
295
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
296
|
+
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
297
|
+
* 'id'.
|
|
298
|
+
* @returns a boolean value.
|
|
299
|
+
*/
|
|
300
|
+
allGreaterNodesAdd(node, delta, propertyName) {
|
|
336
301
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
302
|
+
if (typeof node === 'number')
|
|
303
|
+
node = this.get(node, 'id');
|
|
304
|
+
if (!node)
|
|
305
|
+
return false;
|
|
306
|
+
const id = node.id;
|
|
337
307
|
if (!this.root)
|
|
338
308
|
return false;
|
|
339
|
-
|
|
309
|
+
const _sumByPropertyName = (cur) => {
|
|
340
310
|
switch (propertyName) {
|
|
341
311
|
case 'id':
|
|
342
312
|
cur.id += delta;
|
|
343
313
|
break;
|
|
344
|
-
case 'count':
|
|
345
|
-
cur.count += delta;
|
|
346
|
-
break;
|
|
347
314
|
default:
|
|
348
315
|
cur.id += delta;
|
|
349
316
|
break;
|
|
350
317
|
}
|
|
351
318
|
};
|
|
352
|
-
if (this.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
319
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
320
|
+
const _traverse = (cur) => {
|
|
321
|
+
const compared = this._compare(cur.id, id);
|
|
322
|
+
if (compared === types_1.CP.gt)
|
|
323
|
+
_sumByPropertyName(cur);
|
|
356
324
|
if (!cur.left && !cur.right)
|
|
357
325
|
return;
|
|
358
|
-
if (cur.left &&
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
326
|
+
if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
|
|
327
|
+
_traverse(cur.left);
|
|
328
|
+
if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
|
|
329
|
+
_traverse(cur.right);
|
|
362
330
|
};
|
|
363
|
-
|
|
331
|
+
_traverse(this.root);
|
|
364
332
|
return true;
|
|
365
333
|
}
|
|
366
334
|
else {
|
|
367
|
-
|
|
335
|
+
const queue = [this.root];
|
|
368
336
|
while (queue.length > 0) {
|
|
369
|
-
|
|
337
|
+
const cur = queue.shift();
|
|
370
338
|
if (cur) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
339
|
+
const compared = this._compare(cur.id, id);
|
|
340
|
+
if (compared === types_1.CP.gt)
|
|
341
|
+
_sumByPropertyName(cur);
|
|
342
|
+
if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
|
|
374
343
|
queue.push(cur.left);
|
|
375
|
-
|
|
344
|
+
if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
|
|
376
345
|
queue.push(cur.right);
|
|
377
346
|
}
|
|
378
347
|
}
|
|
379
348
|
return true;
|
|
380
349
|
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Balancing Adjustment:
|
|
353
|
+
* 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.
|
|
354
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
355
|
+
*
|
|
356
|
+
* Use Cases and Efficiency:
|
|
357
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
358
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
359
|
+
*/
|
|
360
|
+
/**
|
|
361
|
+
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
362
|
+
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
363
|
+
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
364
|
+
*/
|
|
365
|
+
perfectlyBalance() {
|
|
366
|
+
const sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
385
367
|
this.clear();
|
|
386
368
|
if (sorted.length < 1)
|
|
387
369
|
return false;
|
|
388
|
-
if (this.
|
|
389
|
-
|
|
370
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
371
|
+
const buildBalanceBST = (l, r) => {
|
|
390
372
|
if (l > r)
|
|
391
373
|
return;
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
374
|
+
const m = l + Math.floor((r - l) / 2);
|
|
375
|
+
const midNode = sorted[m];
|
|
376
|
+
this.add(midNode.id, midNode.val);
|
|
377
|
+
buildBalanceBST(l, m - 1);
|
|
378
|
+
buildBalanceBST(m + 1, r);
|
|
397
379
|
};
|
|
398
|
-
|
|
380
|
+
buildBalanceBST(0, n - 1);
|
|
399
381
|
return true;
|
|
400
382
|
}
|
|
401
383
|
else {
|
|
402
|
-
|
|
384
|
+
const stack = [[0, n - 1]];
|
|
403
385
|
while (stack.length > 0) {
|
|
404
|
-
|
|
386
|
+
const popped = stack.pop();
|
|
405
387
|
if (popped) {
|
|
406
|
-
|
|
388
|
+
const [l, r] = popped;
|
|
407
389
|
if (l <= r) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
this.
|
|
390
|
+
const m = l + Math.floor((r - l) / 2);
|
|
391
|
+
const midNode = sorted[m];
|
|
392
|
+
this.add(midNode.id, midNode.val);
|
|
411
393
|
stack.push([m + 1, r]);
|
|
412
394
|
stack.push([l, m - 1]);
|
|
413
395
|
}
|
|
@@ -415,27 +397,31 @@ var BST = /** @class */ (function (_super) {
|
|
|
415
397
|
}
|
|
416
398
|
return true;
|
|
417
399
|
}
|
|
418
|
-
}
|
|
419
|
-
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
403
|
+
* @returns a boolean value.
|
|
404
|
+
*/
|
|
405
|
+
isAVLBalanced() {
|
|
420
406
|
var _a, _b;
|
|
421
407
|
if (!this.root)
|
|
422
408
|
return true;
|
|
423
|
-
|
|
424
|
-
if (this.
|
|
425
|
-
|
|
409
|
+
let balanced = true;
|
|
410
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
411
|
+
const _height = (cur) => {
|
|
426
412
|
if (!cur)
|
|
427
413
|
return 0;
|
|
428
|
-
|
|
414
|
+
const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
|
|
429
415
|
if (Math.abs(leftHeight - rightHeight) > 1)
|
|
430
416
|
balanced = false;
|
|
431
417
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
432
418
|
};
|
|
433
|
-
|
|
419
|
+
_height(this.root);
|
|
434
420
|
}
|
|
435
421
|
else {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
422
|
+
const stack = [];
|
|
423
|
+
let node = this.root, last = null;
|
|
424
|
+
const depths = new Map();
|
|
439
425
|
while (stack.length > 0 || node) {
|
|
440
426
|
if (node) {
|
|
441
427
|
stack.push(node);
|
|
@@ -446,8 +432,8 @@ var BST = /** @class */ (function (_super) {
|
|
|
446
432
|
if (!node.right || last === node.right) {
|
|
447
433
|
node = stack.pop();
|
|
448
434
|
if (node) {
|
|
449
|
-
|
|
450
|
-
|
|
435
|
+
const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
436
|
+
const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
451
437
|
if (Math.abs(left - right) > 1)
|
|
452
438
|
return false;
|
|
453
439
|
depths.set(node, 1 + Math.max(left, right));
|
|
@@ -461,16 +447,23 @@ var BST = /** @class */ (function (_super) {
|
|
|
461
447
|
}
|
|
462
448
|
}
|
|
463
449
|
return balanced;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
453
|
+
* greater than, less than, or equal to the second ID.
|
|
454
|
+
* @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
|
|
455
|
+
* @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
|
|
456
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
457
|
+
* than), or CP.eq (equal).
|
|
458
|
+
*/
|
|
459
|
+
_compare(a, b) {
|
|
460
|
+
const compared = this._comparator(a, b);
|
|
467
461
|
if (compared > 0)
|
|
468
|
-
return CP.gt;
|
|
462
|
+
return types_1.CP.gt;
|
|
469
463
|
else if (compared < 0)
|
|
470
|
-
return CP.lt;
|
|
464
|
+
return types_1.CP.lt;
|
|
471
465
|
else
|
|
472
|
-
return CP.eq;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
}(binary_tree_1.BinaryTree));
|
|
466
|
+
return types_1.CP.eq;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
476
469
|
exports.BST = BST;
|