data-structure-typed 0.8.18 → 0.9.16
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/.idea/modules.xml +1 -1
- package/README.md +197 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
- package/dist/data-structures/binary-tree/avl-tree.js +93 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
- package/dist/data-structures/binary-tree/binary-tree.js +480 -370
- package/dist/data-structures/binary-tree/bst.d.ts +4 -8
- package/dist/data-structures/binary-tree/bst.js +152 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/segment-tree.js +91 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
- package/dist/data-structures/graph/abstract-graph.js +546 -311
- package/dist/data-structures/graph/directed-graph.d.ts +5 -13
- package/dist/data-structures/graph/directed-graph.js +250 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
- package/dist/data-structures/graph/undirected-graph.js +166 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
- package/dist/data-structures/hash/coordinate-map.js +38 -20
- package/dist/data-structures/hash/coordinate-set.js +33 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +2 -8
- package/dist/data-structures/heap/heap.js +36 -31
- package/dist/data-structures/heap/max-heap.d.ts +3 -2
- package/dist/data-structures/heap/max-heap.js +27 -9
- package/dist/data-structures/heap/min-heap.d.ts +3 -2
- package/dist/data-structures/heap/min-heap.js +27 -9
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
- package/dist/data-structures/matrix/matrix.js +8 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
- package/dist/data-structures/matrix/matrix2d.js +80 -63
- package/dist/data-structures/matrix/navigator.d.ts +2 -16
- package/dist/data-structures/matrix/navigator.js +37 -18
- package/dist/data-structures/matrix/vector2d.d.ts +18 -18
- package/dist/data-structures/matrix/vector2d.js +117 -94
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
- package/dist/data-structures/priority-queue/priority-queue.js +159 -116
- package/dist/data-structures/queue/deque.js +82 -56
- package/dist/data-structures/queue/queue.d.ts +9 -10
- package/dist/data-structures/queue/queue.js +34 -34
- package/dist/data-structures/stack/stack.d.ts +9 -10
- package/dist/data-structures/stack/stack.js +31 -31
- package/dist/data-structures/trampoline.d.ts +14 -23
- package/dist/data-structures/trampoline.js +103 -25
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +234 -80
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/{types → data-structures/types}/utils.d.ts +7 -1
- package/dist/{types → data-structures/types}/utils.js +20 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
- package/dist/utils/utils.js +651 -0
- package/package.json +20 -42
- package/src/data-structures/binary-tree/avl-tree.ts +1 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +184 -139
- package/src/data-structures/binary-tree/bst.ts +15 -24
- 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/segment-tree.ts +20 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
- package/src/data-structures/diagrams/README.md +7 -0
- package/src/data-structures/graph/abstract-graph.ts +58 -94
- 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 +12 -28
- package/src/data-structures/graph/undirected-graph.ts +11 -10
- package/src/data-structures/hash/coordinate-map.ts +1 -1
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +2 -11
- package/src/data-structures/heap/max-heap.ts +3 -2
- package/src/data-structures/heap/min-heap.ts +3 -2
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
- package/src/data-structures/matrix/matrix2d.ts +11 -11
- package/src/data-structures/matrix/navigator.ts +2 -14
- package/src/data-structures/matrix/vector2d.ts +52 -52
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/priority-queue.ts +70 -78
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +12 -13
- package/src/data-structures/stack/stack.ts +12 -13
- package/src/data-structures/trampoline.ts +31 -71
- package/src/data-structures/trie/trie.ts +61 -11
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +12 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +15 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/{types → data-structures/types}/utils.ts +20 -5
- package/src/utils/index.ts +1 -0
- package/src/{utils.ts → utils/utils.ts} +32 -132
- package/tsconfig.json +9 -6
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export type BSTDeletedResult<T> = {
|
|
4
|
-
deleted: BSTNode<T> | null;
|
|
5
|
-
needBalanced: BSTNode<T> | null;
|
|
6
|
-
};
|
|
1
|
+
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult } from '../types';
|
|
2
|
+
import { BinaryTree, BinaryTreeNode, LoopType } from './binary-tree';
|
|
7
3
|
export declare enum CP {
|
|
8
4
|
lt = -1,
|
|
9
5
|
eq = 0,
|
|
@@ -13,8 +9,6 @@ export declare class BSTNode<T> extends BinaryTreeNode<T> {
|
|
|
13
9
|
clone(): BSTNode<T>;
|
|
14
10
|
}
|
|
15
11
|
export declare class BST<T> extends BinaryTree<T> {
|
|
16
|
-
protected _comparator: BSTComparator;
|
|
17
|
-
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
|
|
18
12
|
constructor(options?: {
|
|
19
13
|
comparator?: BSTComparator;
|
|
20
14
|
loopType?: LoopType;
|
|
@@ -29,4 +23,6 @@ export declare class BST<T> extends BinaryTree<T> {
|
|
|
29
23
|
allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
30
24
|
balance(): boolean;
|
|
31
25
|
isAVLBalanced(): boolean;
|
|
26
|
+
protected _comparator: BSTComparator;
|
|
27
|
+
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
|
|
32
28
|
}
|
|
@@ -1,46 +1,76 @@
|
|
|
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
|
+
};
|
|
2
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
34
|
exports.BST = exports.BSTNode = exports.CP = void 0;
|
|
4
|
-
|
|
35
|
+
var binary_tree_1 = require("./binary-tree");
|
|
5
36
|
var CP;
|
|
6
37
|
(function (CP) {
|
|
7
38
|
CP[CP["lt"] = -1] = "lt";
|
|
8
39
|
CP[CP["eq"] = 0] = "eq";
|
|
9
40
|
CP[CP["gt"] = 1] = "gt";
|
|
10
41
|
})(CP = exports.CP || (exports.CP = {}));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
42
|
+
var BSTNode = /** @class */ (function (_super) {
|
|
43
|
+
__extends(BSTNode, _super);
|
|
44
|
+
function BSTNode() {
|
|
45
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
14
46
|
}
|
|
15
|
-
|
|
47
|
+
BSTNode.prototype.clone = function () {
|
|
48
|
+
return new BSTNode(this.id, this.val, this.count);
|
|
49
|
+
};
|
|
50
|
+
return BSTNode;
|
|
51
|
+
}(binary_tree_1.BinaryTreeNode));
|
|
16
52
|
exports.BSTNode = BSTNode;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
else if (compared < 0)
|
|
23
|
-
return CP.lt;
|
|
24
|
-
else
|
|
25
|
-
return CP.eq;
|
|
26
|
-
}
|
|
27
|
-
constructor(options) {
|
|
28
|
-
super(options);
|
|
29
|
-
this._comparator = (a, b) => a - b;
|
|
53
|
+
var BST = /** @class */ (function (_super) {
|
|
54
|
+
__extends(BST, _super);
|
|
55
|
+
function BST(options) {
|
|
56
|
+
var _this = _super.call(this, options) || this;
|
|
57
|
+
_this._comparator = function (a, b) { return a - b; };
|
|
30
58
|
if (options !== undefined) {
|
|
31
|
-
|
|
59
|
+
var comparator = options.comparator;
|
|
32
60
|
if (comparator !== undefined) {
|
|
33
|
-
|
|
61
|
+
_this._comparator = comparator;
|
|
34
62
|
}
|
|
35
63
|
}
|
|
64
|
+
return _this;
|
|
36
65
|
}
|
|
37
|
-
createNode(id, val, count) {
|
|
66
|
+
BST.prototype.createNode = function (id, val, count) {
|
|
38
67
|
return val !== null ? new BSTNode(id, val, count) : null;
|
|
39
|
-
}
|
|
40
|
-
put(id, val, count
|
|
68
|
+
};
|
|
69
|
+
BST.prototype.put = function (id, val, count) {
|
|
41
70
|
var _a;
|
|
42
|
-
|
|
43
|
-
|
|
71
|
+
if (count === void 0) { count = 1; }
|
|
72
|
+
var inserted = null;
|
|
73
|
+
var newNode = this.createNode(id, val, count);
|
|
44
74
|
if (this.root === null) {
|
|
45
75
|
this.root = newNode;
|
|
46
76
|
this.size++;
|
|
@@ -48,8 +78,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
48
78
|
inserted = (this.root);
|
|
49
79
|
}
|
|
50
80
|
else {
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
var cur = this.root;
|
|
82
|
+
var traversing = true;
|
|
53
83
|
while (traversing) {
|
|
54
84
|
if (cur !== null && newNode !== null) {
|
|
55
85
|
if (this._compare(cur.id, id) === CP.eq) {
|
|
@@ -109,13 +139,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
109
139
|
}
|
|
110
140
|
}
|
|
111
141
|
return inserted;
|
|
112
|
-
}
|
|
113
|
-
get(nodeProperty, propertyName) {
|
|
142
|
+
};
|
|
143
|
+
BST.prototype.get = function (nodeProperty, propertyName) {
|
|
114
144
|
var _a;
|
|
115
145
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
116
146
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
117
|
-
}
|
|
118
|
-
lastKey() {
|
|
147
|
+
};
|
|
148
|
+
BST.prototype.lastKey = function () {
|
|
119
149
|
var _a, _b, _c, _d, _e, _f;
|
|
120
150
|
if (this._compare(0, 1) === CP.lt)
|
|
121
151
|
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
|
|
@@ -123,16 +153,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
123
153
|
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
|
|
124
154
|
else
|
|
125
155
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
126
|
-
}
|
|
127
|
-
remove(id, ignoreCount) {
|
|
128
|
-
|
|
156
|
+
};
|
|
157
|
+
BST.prototype.remove = function (id, ignoreCount) {
|
|
158
|
+
var bstDeletedResult = [];
|
|
129
159
|
if (!this.root)
|
|
130
160
|
return bstDeletedResult;
|
|
131
|
-
|
|
161
|
+
var curr = this.get(id);
|
|
132
162
|
if (!curr)
|
|
133
163
|
return bstDeletedResult;
|
|
134
|
-
|
|
135
|
-
|
|
164
|
+
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
165
|
+
var needBalanced = null, orgCurrent = curr;
|
|
136
166
|
if (curr.count > 1 && !ignoreCount) {
|
|
137
167
|
curr.count--;
|
|
138
168
|
this.count--;
|
|
@@ -156,9 +186,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
156
186
|
}
|
|
157
187
|
}
|
|
158
188
|
else {
|
|
159
|
-
|
|
189
|
+
var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
160
190
|
if (leftSubTreeMax) {
|
|
161
|
-
|
|
191
|
+
var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
162
192
|
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
163
193
|
if (parentOfLeftSubTreeMax) {
|
|
164
194
|
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
@@ -172,37 +202,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
172
202
|
this.size--;
|
|
173
203
|
this.count -= curr.count;
|
|
174
204
|
}
|
|
175
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
205
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
176
206
|
return bstDeletedResult;
|
|
177
|
-
}
|
|
178
|
-
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
207
|
+
};
|
|
208
|
+
BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
209
|
+
var _this = this;
|
|
179
210
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
180
211
|
if (!this.root)
|
|
181
212
|
return [];
|
|
182
|
-
|
|
213
|
+
var result = [];
|
|
183
214
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
184
|
-
|
|
185
|
-
if (
|
|
215
|
+
var _traverse_1 = function (cur) {
|
|
216
|
+
if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
186
217
|
return;
|
|
187
218
|
if (!cur.left && !cur.right)
|
|
188
219
|
return;
|
|
189
220
|
if (propertyName === 'id') {
|
|
190
|
-
if (
|
|
191
|
-
cur.left &&
|
|
192
|
-
if (
|
|
193
|
-
cur.right &&
|
|
221
|
+
if (_this._compare(cur.id, nodeProperty) === CP.gt)
|
|
222
|
+
cur.left && _traverse_1(cur.left);
|
|
223
|
+
if (_this._compare(cur.id, nodeProperty) === CP.lt)
|
|
224
|
+
cur.right && _traverse_1(cur.right);
|
|
194
225
|
}
|
|
195
226
|
else {
|
|
196
|
-
cur.left &&
|
|
197
|
-
cur.right &&
|
|
227
|
+
cur.left && _traverse_1(cur.left);
|
|
228
|
+
cur.right && _traverse_1(cur.right);
|
|
198
229
|
}
|
|
199
230
|
};
|
|
200
|
-
|
|
231
|
+
_traverse_1(this.root);
|
|
201
232
|
}
|
|
202
233
|
else {
|
|
203
|
-
|
|
234
|
+
var queue = [this.root];
|
|
204
235
|
while (queue.length > 0) {
|
|
205
|
-
|
|
236
|
+
var cur = queue.shift();
|
|
206
237
|
if (cur) {
|
|
207
238
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
208
239
|
return result;
|
|
@@ -220,14 +251,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
220
251
|
}
|
|
221
252
|
}
|
|
222
253
|
return result;
|
|
223
|
-
}
|
|
254
|
+
};
|
|
224
255
|
// --- start additional functions
|
|
225
|
-
lesserSum(id, propertyName) {
|
|
256
|
+
BST.prototype.lesserSum = function (id, propertyName) {
|
|
257
|
+
var _this = this;
|
|
226
258
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
227
259
|
if (!this.root)
|
|
228
260
|
return 0;
|
|
229
|
-
|
|
230
|
-
|
|
261
|
+
var getSumByPropertyName = function (cur) {
|
|
262
|
+
var needSum;
|
|
231
263
|
switch (propertyName) {
|
|
232
264
|
case 'id':
|
|
233
265
|
needSum = cur.id;
|
|
@@ -241,39 +273,39 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
241
273
|
}
|
|
242
274
|
return needSum;
|
|
243
275
|
};
|
|
244
|
-
|
|
276
|
+
var sum = 0;
|
|
245
277
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
246
|
-
|
|
247
|
-
|
|
278
|
+
var _traverse_2 = function (cur) {
|
|
279
|
+
var compared = _this._compare(cur.id, id);
|
|
248
280
|
if (compared === CP.eq) {
|
|
249
281
|
if (cur.right)
|
|
250
|
-
sum +=
|
|
282
|
+
sum += _this.subTreeSum(cur.right, propertyName);
|
|
251
283
|
return;
|
|
252
284
|
}
|
|
253
285
|
else if (compared === CP.lt) {
|
|
254
286
|
if (cur.left)
|
|
255
|
-
sum +=
|
|
287
|
+
sum += _this.subTreeSum(cur.left, propertyName);
|
|
256
288
|
sum += getSumByPropertyName(cur);
|
|
257
289
|
if (cur.right)
|
|
258
|
-
|
|
290
|
+
_traverse_2(cur.right);
|
|
259
291
|
else
|
|
260
292
|
return;
|
|
261
293
|
}
|
|
262
294
|
else {
|
|
263
295
|
if (cur.left)
|
|
264
|
-
|
|
296
|
+
_traverse_2(cur.left);
|
|
265
297
|
else
|
|
266
298
|
return;
|
|
267
299
|
}
|
|
268
300
|
};
|
|
269
|
-
|
|
301
|
+
_traverse_2(this.root);
|
|
270
302
|
}
|
|
271
303
|
else {
|
|
272
|
-
|
|
304
|
+
var queue = [this.root];
|
|
273
305
|
while (queue.length > 0) {
|
|
274
|
-
|
|
306
|
+
var cur = queue.shift();
|
|
275
307
|
if (cur) {
|
|
276
|
-
|
|
308
|
+
var compared = this._compare(cur.id, id);
|
|
277
309
|
if (compared === CP.eq) {
|
|
278
310
|
if (cur.right)
|
|
279
311
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -298,12 +330,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
330
|
}
|
|
299
331
|
}
|
|
300
332
|
return sum;
|
|
301
|
-
}
|
|
302
|
-
allGreaterNodesAdd(node, delta, propertyName) {
|
|
333
|
+
};
|
|
334
|
+
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
335
|
+
var _this = this;
|
|
303
336
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
304
337
|
if (!this.root)
|
|
305
338
|
return false;
|
|
306
|
-
|
|
339
|
+
var _sumByPropertyName = function (cur) {
|
|
307
340
|
switch (propertyName) {
|
|
308
341
|
case 'id':
|
|
309
342
|
cur.id += delta;
|
|
@@ -317,25 +350,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
317
350
|
}
|
|
318
351
|
};
|
|
319
352
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
320
|
-
|
|
321
|
-
|
|
353
|
+
var _traverse_3 = function (cur) {
|
|
354
|
+
var compared = _this._compare(cur.id, node.id);
|
|
322
355
|
_sumByPropertyName(cur);
|
|
323
356
|
if (!cur.left && !cur.right)
|
|
324
357
|
return;
|
|
325
358
|
if (cur.left && compared === CP.gt)
|
|
326
|
-
|
|
359
|
+
_traverse_3(cur.left);
|
|
327
360
|
else if (cur.right && compared === CP.gt)
|
|
328
|
-
|
|
361
|
+
_traverse_3(cur.right);
|
|
329
362
|
};
|
|
330
|
-
|
|
363
|
+
_traverse_3(this.root);
|
|
331
364
|
return true;
|
|
332
365
|
}
|
|
333
366
|
else {
|
|
334
|
-
|
|
367
|
+
var queue = [this.root];
|
|
335
368
|
while (queue.length > 0) {
|
|
336
|
-
|
|
369
|
+
var cur = queue.shift();
|
|
337
370
|
if (cur) {
|
|
338
|
-
|
|
371
|
+
var compared = this._compare(cur.id, node.id);
|
|
339
372
|
_sumByPropertyName(cur);
|
|
340
373
|
if (cur.left && compared === CP.gt)
|
|
341
374
|
queue.push(cur.left);
|
|
@@ -345,34 +378,35 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
345
378
|
}
|
|
346
379
|
return true;
|
|
347
380
|
}
|
|
348
|
-
}
|
|
349
|
-
balance() {
|
|
350
|
-
|
|
381
|
+
};
|
|
382
|
+
BST.prototype.balance = function () {
|
|
383
|
+
var _this = this;
|
|
384
|
+
var sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
351
385
|
this.clear();
|
|
352
386
|
if (sorted.length < 1)
|
|
353
387
|
return false;
|
|
354
388
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
355
|
-
|
|
389
|
+
var buildBalanceBST_1 = function (l, r) {
|
|
356
390
|
if (l > r)
|
|
357
391
|
return;
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
392
|
+
var m = l + Math.floor((r - l) / 2);
|
|
393
|
+
var midNode = sorted[m];
|
|
394
|
+
_this.put(midNode.id, midNode.val, midNode.count);
|
|
395
|
+
buildBalanceBST_1(l, m - 1);
|
|
396
|
+
buildBalanceBST_1(m + 1, r);
|
|
363
397
|
};
|
|
364
|
-
|
|
398
|
+
buildBalanceBST_1(0, n - 1);
|
|
365
399
|
return true;
|
|
366
400
|
}
|
|
367
401
|
else {
|
|
368
|
-
|
|
402
|
+
var stack = [[0, n - 1]];
|
|
369
403
|
while (stack.length > 0) {
|
|
370
|
-
|
|
404
|
+
var popped = stack.pop();
|
|
371
405
|
if (popped) {
|
|
372
|
-
|
|
406
|
+
var _a = __read(popped, 2), l = _a[0], r = _a[1];
|
|
373
407
|
if (l <= r) {
|
|
374
|
-
|
|
375
|
-
|
|
408
|
+
var m = l + Math.floor((r - l) / 2);
|
|
409
|
+
var midNode = sorted[m];
|
|
376
410
|
this.put(midNode.id, midNode.val, midNode.count);
|
|
377
411
|
stack.push([m + 1, r]);
|
|
378
412
|
stack.push([l, m - 1]);
|
|
@@ -381,26 +415,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
381
415
|
}
|
|
382
416
|
return true;
|
|
383
417
|
}
|
|
384
|
-
}
|
|
385
|
-
isAVLBalanced() {
|
|
418
|
+
};
|
|
419
|
+
BST.prototype.isAVLBalanced = function () {
|
|
386
420
|
var _a, _b;
|
|
387
421
|
if (!this.root)
|
|
388
422
|
return true;
|
|
389
|
-
|
|
423
|
+
var balanced = true;
|
|
390
424
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
391
|
-
|
|
425
|
+
var _height_1 = function (cur) {
|
|
392
426
|
if (!cur)
|
|
393
427
|
return 0;
|
|
394
|
-
|
|
428
|
+
var leftHeight = _height_1(cur.left), rightHeight = _height_1(cur.right);
|
|
395
429
|
if (Math.abs(leftHeight - rightHeight) > 1)
|
|
396
430
|
balanced = false;
|
|
397
431
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
398
432
|
};
|
|
399
|
-
|
|
433
|
+
_height_1(this.root);
|
|
400
434
|
}
|
|
401
435
|
else {
|
|
402
|
-
|
|
403
|
-
|
|
436
|
+
var stack = [];
|
|
437
|
+
var node = this.root, last = null;
|
|
438
|
+
var depths = new Map();
|
|
404
439
|
while (stack.length > 0 || node) {
|
|
405
440
|
if (node) {
|
|
406
441
|
stack.push(node);
|
|
@@ -411,8 +446,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
411
446
|
if (!node.right || last === node.right) {
|
|
412
447
|
node = stack.pop();
|
|
413
448
|
if (node) {
|
|
414
|
-
|
|
415
|
-
|
|
449
|
+
var left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
450
|
+
var right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
416
451
|
if (Math.abs(left - right) > 1)
|
|
417
452
|
return false;
|
|
418
453
|
depths.set(node, 1 + Math.max(left, right));
|
|
@@ -426,6 +461,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
426
461
|
}
|
|
427
462
|
}
|
|
428
463
|
return balanced;
|
|
429
|
-
}
|
|
430
|
-
|
|
464
|
+
};
|
|
465
|
+
BST.prototype._compare = function (a, b) {
|
|
466
|
+
var compared = this._comparator(a, b);
|
|
467
|
+
if (compared > 0)
|
|
468
|
+
return CP.gt;
|
|
469
|
+
else if (compared < 0)
|
|
470
|
+
return CP.lt;
|
|
471
|
+
else
|
|
472
|
+
return CP.eq;
|
|
473
|
+
};
|
|
474
|
+
return BST;
|
|
475
|
+
}(binary_tree_1.BinaryTree));
|
|
431
476
|
exports.BST = BST;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { SegmentTreeNodeVal } from '../types';
|
|
2
2
|
export declare class SegmentTreeNode {
|
|
3
|
+
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
|
|
3
4
|
protected _start: number;
|
|
4
5
|
get start(): number;
|
|
5
6
|
set start(v: number);
|
|
@@ -18,15 +19,14 @@ export declare class SegmentTreeNode {
|
|
|
18
19
|
protected _right: SegmentTreeNode | null;
|
|
19
20
|
get right(): SegmentTreeNode | null;
|
|
20
21
|
set right(v: SegmentTreeNode | null);
|
|
21
|
-
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
|
|
22
22
|
}
|
|
23
23
|
export declare class SegmentTree {
|
|
24
24
|
protected _values: number[];
|
|
25
25
|
protected _start: number;
|
|
26
26
|
protected _end: number;
|
|
27
|
+
constructor(values: number[], start?: number, end?: number);
|
|
27
28
|
protected _root: SegmentTreeNode | null;
|
|
28
29
|
get root(): SegmentTreeNode | null;
|
|
29
|
-
constructor(values: number[], start?: number, end?: number);
|
|
30
30
|
build(start: number, end: number): SegmentTreeNode;
|
|
31
31
|
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal): void;
|
|
32
32
|
querySumByRange(indexA: number, indexB: number): number;
|