data-structure-typed 0.8.6
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/data-structure-typed.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +2 -0
- package/dist/data-structures/binary-tree/aa-tree.js +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +231 -0
- package/dist/data-structures/binary-tree/b-tree.js +6 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +992 -0
- package/dist/data-structures/binary-tree/bst.js +431 -0
- package/dist/data-structures/binary-tree/index.js +20 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -0
- package/dist/data-structures/binary-tree/segment-tree.js +151 -0
- package/dist/data-structures/binary-tree/splay-tree.js +6 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
- package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
- package/dist/data-structures/graph/abstract-graph.js +648 -0
- package/dist/data-structures/graph/directed-graph.js +268 -0
- package/dist/data-structures/graph/index.js +19 -0
- package/dist/data-structures/graph/undirected-graph.js +142 -0
- package/dist/data-structures/hash/coordinate-map.js +24 -0
- package/dist/data-structures/hash/coordinate-set.js +21 -0
- package/dist/data-structures/hash/hash-table.js +2 -0
- package/dist/data-structures/hash/index.js +17 -0
- package/dist/data-structures/hash/pair.js +2 -0
- package/dist/data-structures/hash/tree-map.js +2 -0
- package/dist/data-structures/hash/tree-set.js +2 -0
- package/dist/data-structures/heap/heap.js +114 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.js +22 -0
- package/dist/data-structures/heap/min-heap.js +22 -0
- package/dist/data-structures/index.js +25 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
- package/dist/data-structures/linked-list/index.js +18 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/data-structures/matrix/index.js +19 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.js +119 -0
- package/dist/data-structures/matrix/navigator.js +78 -0
- package/dist/data-structures/matrix/vector2d.js +161 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +174 -0
- package/dist/data-structures/queue/deque.js +132 -0
- package/dist/data-structures/queue/index.js +17 -0
- package/dist/data-structures/queue/queue.js +113 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.js +97 -0
- package/dist/data-structures/trampoline.js +52 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.js +141 -0
- package/dist/index.js +17 -0
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/pair.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +72 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
- package/dist/types/data-structures/index.d.ts +9 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.d.ts +3 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
- package/dist/types/data-structures/queue/deque.d.ts +37 -0
- package/dist/types/data-structures/queue/index.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +76 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +69 -0
- package/dist/types/data-structures/trampoline.d.ts +25 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +28 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/utils.d.ts +46 -0
- package/dist/types/utils.d.ts +122 -0
- package/dist/types/utils.js +53 -0
- package/dist/utils.js +569 -0
- package/package.json +75 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/avl-tree.ts +232 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
- package/src/data-structures/binary-tree/bst.ts +404 -0
- package/src/data-structures/binary-tree/index.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +3 -0
- package/src/data-structures/binary-tree/segment-tree.ts +164 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/graph/abstract-graph.ts +789 -0
- package/src/data-structures/graph/directed-graph.ts +322 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +154 -0
- package/src/data-structures/hash/coordinate-map.ts +24 -0
- package/src/data-structures/hash/coordinate-set.ts +20 -0
- package/src/data-structures/hash/hash-table.ts +1 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +136 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +22 -0
- package/src/data-structures/heap/min-heap.ts +24 -0
- package/src/data-structures/index.ts +10 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
- package/src/data-structures/linked-list/index.ts +2 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +13 -0
- package/src/data-structures/matrix/matrix2d.ts +125 -0
- package/src/data-structures/matrix/navigator.ts +99 -0
- package/src/data-structures/matrix/vector2d.ts +189 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/priority-queue.ts +208 -0
- package/src/data-structures/queue/deque.ts +139 -0
- package/src/data-structures/queue/index.ts +1 -0
- package/src/data-structures/queue/queue.ts +123 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +104 -0
- package/src/data-structures/trampoline.ts +91 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +153 -0
- package/src/index.ts +1 -0
- package/src/types/index.ts +1 -0
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +158 -0
- package/src/utils.ts +605 -0
- package/tsconfig.json +52 -0
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BST = exports.BSTNode = exports.CP = void 0;
|
|
4
|
+
const binary_tree_1 = require("./binary-tree");
|
|
5
|
+
var CP;
|
|
6
|
+
(function (CP) {
|
|
7
|
+
CP[CP["lt"] = -1] = "lt";
|
|
8
|
+
CP[CP["eq"] = 0] = "eq";
|
|
9
|
+
CP[CP["gt"] = 1] = "gt";
|
|
10
|
+
})(CP = exports.CP || (exports.CP = {}));
|
|
11
|
+
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
12
|
+
clone() {
|
|
13
|
+
return new BSTNode(this.id, this.val, this.count);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BSTNode = BSTNode;
|
|
17
|
+
class BST extends binary_tree_1.BinaryTree {
|
|
18
|
+
_compare(a, b) {
|
|
19
|
+
const compared = this._comparator(a, b);
|
|
20
|
+
if (compared > 0)
|
|
21
|
+
return CP.gt;
|
|
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;
|
|
30
|
+
if (options !== undefined) {
|
|
31
|
+
const { comparator } = options;
|
|
32
|
+
if (comparator !== undefined) {
|
|
33
|
+
this._comparator = comparator;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
createNode(id, val, count) {
|
|
38
|
+
return val !== null ? new BSTNode(id, val, count) : null;
|
|
39
|
+
}
|
|
40
|
+
put(id, val, count = 1) {
|
|
41
|
+
var _a;
|
|
42
|
+
let inserted = null;
|
|
43
|
+
const newNode = this.createNode(id, val, count);
|
|
44
|
+
if (this.root === null) {
|
|
45
|
+
this.root = newNode;
|
|
46
|
+
this.size++;
|
|
47
|
+
this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 1;
|
|
48
|
+
inserted = (this.root);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
let cur = this.root;
|
|
52
|
+
let traversing = true;
|
|
53
|
+
while (traversing) {
|
|
54
|
+
if (cur !== null && newNode !== null) {
|
|
55
|
+
if (this._compare(cur.id, id) === CP.eq) {
|
|
56
|
+
if (newNode) {
|
|
57
|
+
cur.count += newNode.count;
|
|
58
|
+
this.count += newNode.count;
|
|
59
|
+
cur.val = newNode.val;
|
|
60
|
+
}
|
|
61
|
+
//Duplicates are not accepted.
|
|
62
|
+
traversing = false;
|
|
63
|
+
inserted = cur;
|
|
64
|
+
}
|
|
65
|
+
else if (this._compare(cur.id, id) === CP.gt) {
|
|
66
|
+
// Traverse left of the node
|
|
67
|
+
if (cur.left === undefined) {
|
|
68
|
+
if (newNode) {
|
|
69
|
+
newNode.parent = cur;
|
|
70
|
+
newNode.familyPosition = binary_tree_1.FamilyPosition.left;
|
|
71
|
+
}
|
|
72
|
+
//Add to the left of the current node
|
|
73
|
+
cur.left = newNode;
|
|
74
|
+
this.size++;
|
|
75
|
+
this.count += newNode.count;
|
|
76
|
+
traversing = false;
|
|
77
|
+
inserted = cur.left;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
//Traverse the left of the current node
|
|
81
|
+
if (cur.left)
|
|
82
|
+
cur = cur.left;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (this._compare(cur.id, id) === CP.lt) {
|
|
86
|
+
// Traverse right of the node
|
|
87
|
+
if (cur.right === undefined) {
|
|
88
|
+
if (newNode) {
|
|
89
|
+
newNode.parent = cur;
|
|
90
|
+
newNode.familyPosition = binary_tree_1.FamilyPosition.right;
|
|
91
|
+
}
|
|
92
|
+
//Add to the right of the current node
|
|
93
|
+
cur.right = newNode;
|
|
94
|
+
this.size++;
|
|
95
|
+
this.count += newNode.count;
|
|
96
|
+
traversing = false;
|
|
97
|
+
inserted = (cur.right);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
//Traverse the left of the current node
|
|
101
|
+
if (cur.right)
|
|
102
|
+
cur = cur.right;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
traversing = false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return inserted;
|
|
112
|
+
}
|
|
113
|
+
get(nodeProperty, propertyName) {
|
|
114
|
+
var _a;
|
|
115
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
116
|
+
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
117
|
+
}
|
|
118
|
+
lastKey() {
|
|
119
|
+
var _a, _b, _c, _d, _e, _f;
|
|
120
|
+
if (this._compare(0, 1) === CP.lt)
|
|
121
|
+
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
|
|
122
|
+
else if (this._compare(0, 1) === CP.gt)
|
|
123
|
+
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
|
|
124
|
+
else
|
|
125
|
+
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
|
+
const bstDeletedResult = [];
|
|
129
|
+
if (!this.root)
|
|
130
|
+
return bstDeletedResult;
|
|
131
|
+
const curr = this.get(id);
|
|
132
|
+
if (!curr)
|
|
133
|
+
return bstDeletedResult;
|
|
134
|
+
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
135
|
+
let needBalanced = null, orgCurrent = curr;
|
|
136
|
+
if (curr.count > 1 && !ignoreCount) {
|
|
137
|
+
curr.count--;
|
|
138
|
+
this.count--;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (!curr.left) {
|
|
142
|
+
if (!parent) {
|
|
143
|
+
if (curr.right !== undefined)
|
|
144
|
+
this.root = curr.right;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
switch (curr.familyPosition) {
|
|
148
|
+
case binary_tree_1.FamilyPosition.left:
|
|
149
|
+
parent.left = curr.right;
|
|
150
|
+
break;
|
|
151
|
+
case binary_tree_1.FamilyPosition.right:
|
|
152
|
+
parent.right = curr.right;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
needBalanced = parent;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
160
|
+
if (leftSubTreeMax) {
|
|
161
|
+
const parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
162
|
+
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
163
|
+
if (parentOfLeftSubTreeMax) {
|
|
164
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
165
|
+
parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
|
|
166
|
+
else
|
|
167
|
+
parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
|
|
168
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
this.size--;
|
|
173
|
+
this.count -= curr.count;
|
|
174
|
+
}
|
|
175
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
176
|
+
return bstDeletedResult;
|
|
177
|
+
}
|
|
178
|
+
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
179
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
180
|
+
if (!this.root)
|
|
181
|
+
return [];
|
|
182
|
+
const result = [];
|
|
183
|
+
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
184
|
+
const _traverse = (cur) => {
|
|
185
|
+
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
186
|
+
return;
|
|
187
|
+
if (!cur.left && !cur.right)
|
|
188
|
+
return;
|
|
189
|
+
if (propertyName === 'id') {
|
|
190
|
+
if (this._compare(cur.id, nodeProperty) === CP.gt)
|
|
191
|
+
cur.left && _traverse(cur.left);
|
|
192
|
+
if (this._compare(cur.id, nodeProperty) === CP.lt)
|
|
193
|
+
cur.right && _traverse(cur.right);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
cur.left && _traverse(cur.left);
|
|
197
|
+
cur.right && _traverse(cur.right);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
_traverse(this.root);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
const queue = [this.root];
|
|
204
|
+
while (queue.length > 0) {
|
|
205
|
+
const cur = queue.shift();
|
|
206
|
+
if (cur) {
|
|
207
|
+
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
208
|
+
return result;
|
|
209
|
+
if (propertyName === 'id') {
|
|
210
|
+
if (this._compare(cur.id, nodeProperty) === CP.gt)
|
|
211
|
+
cur.left && queue.push(cur.left);
|
|
212
|
+
if (this._compare(cur.id, nodeProperty) === CP.lt)
|
|
213
|
+
cur.right && queue.push(cur.right);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
cur.left && queue.push(cur.left);
|
|
217
|
+
cur.right && queue.push(cur.right);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
// --- start additional functions
|
|
225
|
+
lesserSum(id, propertyName) {
|
|
226
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
227
|
+
if (!this.root)
|
|
228
|
+
return 0;
|
|
229
|
+
const getSumByPropertyName = (cur) => {
|
|
230
|
+
let needSum;
|
|
231
|
+
switch (propertyName) {
|
|
232
|
+
case 'id':
|
|
233
|
+
needSum = cur.id;
|
|
234
|
+
break;
|
|
235
|
+
case 'count':
|
|
236
|
+
needSum = cur.count;
|
|
237
|
+
break;
|
|
238
|
+
default:
|
|
239
|
+
needSum = cur.id;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
return needSum;
|
|
243
|
+
};
|
|
244
|
+
let sum = 0;
|
|
245
|
+
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
246
|
+
const _traverse = (cur) => {
|
|
247
|
+
const compared = this._compare(cur.id, id);
|
|
248
|
+
if (compared === CP.eq) {
|
|
249
|
+
if (cur.right)
|
|
250
|
+
sum += this.subTreeSum(cur.right, propertyName);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
else if (compared === CP.lt) {
|
|
254
|
+
if (cur.left)
|
|
255
|
+
sum += this.subTreeSum(cur.left, propertyName);
|
|
256
|
+
sum += getSumByPropertyName(cur);
|
|
257
|
+
if (cur.right)
|
|
258
|
+
_traverse(cur.right);
|
|
259
|
+
else
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
if (cur.left)
|
|
264
|
+
_traverse(cur.left);
|
|
265
|
+
else
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
_traverse(this.root);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
const queue = [this.root];
|
|
273
|
+
while (queue.length > 0) {
|
|
274
|
+
const cur = queue.shift();
|
|
275
|
+
if (cur) {
|
|
276
|
+
const compared = this._compare(cur.id, id);
|
|
277
|
+
if (compared === CP.eq) {
|
|
278
|
+
if (cur.right)
|
|
279
|
+
sum += this.subTreeSum(cur.right, propertyName);
|
|
280
|
+
return sum;
|
|
281
|
+
}
|
|
282
|
+
else if (compared === CP.lt) { // todo maybe a bug
|
|
283
|
+
if (cur.left)
|
|
284
|
+
sum += this.subTreeSum(cur.left, propertyName);
|
|
285
|
+
sum += getSumByPropertyName(cur);
|
|
286
|
+
if (cur.right)
|
|
287
|
+
queue.push(cur.right);
|
|
288
|
+
else
|
|
289
|
+
return sum;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
if (cur.left)
|
|
293
|
+
queue.push(cur.left);
|
|
294
|
+
else
|
|
295
|
+
return sum;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return sum;
|
|
301
|
+
}
|
|
302
|
+
allGreaterNodesAdd(node, delta, propertyName) {
|
|
303
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
304
|
+
if (!this.root)
|
|
305
|
+
return false;
|
|
306
|
+
const _sumByPropertyName = (cur) => {
|
|
307
|
+
switch (propertyName) {
|
|
308
|
+
case 'id':
|
|
309
|
+
cur.id += delta;
|
|
310
|
+
break;
|
|
311
|
+
case 'count':
|
|
312
|
+
cur.count += delta;
|
|
313
|
+
break;
|
|
314
|
+
default:
|
|
315
|
+
cur.id += delta;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
320
|
+
const _traverse = (cur) => {
|
|
321
|
+
const compared = this._compare(cur.id, node.id);
|
|
322
|
+
_sumByPropertyName(cur);
|
|
323
|
+
if (!cur.left && !cur.right)
|
|
324
|
+
return;
|
|
325
|
+
if (cur.left && compared === CP.gt)
|
|
326
|
+
_traverse(cur.left);
|
|
327
|
+
else if (cur.right && compared === CP.gt)
|
|
328
|
+
_traverse(cur.right);
|
|
329
|
+
};
|
|
330
|
+
_traverse(this.root);
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
const queue = [this.root];
|
|
335
|
+
while (queue.length > 0) {
|
|
336
|
+
const cur = queue.shift();
|
|
337
|
+
if (cur) {
|
|
338
|
+
const compared = this._compare(cur.id, node.id);
|
|
339
|
+
_sumByPropertyName(cur);
|
|
340
|
+
if (cur.left && compared === CP.gt)
|
|
341
|
+
queue.push(cur.left);
|
|
342
|
+
else if (cur.right && compared === CP.gt)
|
|
343
|
+
queue.push(cur.right);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
balance() {
|
|
350
|
+
const sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
351
|
+
this.clear();
|
|
352
|
+
if (sorted.length < 1)
|
|
353
|
+
return false;
|
|
354
|
+
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
355
|
+
const buildBalanceBST = (l, r) => {
|
|
356
|
+
if (l > r)
|
|
357
|
+
return;
|
|
358
|
+
const m = l + Math.floor((r - l) / 2);
|
|
359
|
+
const midNode = sorted[m];
|
|
360
|
+
this.put(midNode.id, midNode.val, midNode.count);
|
|
361
|
+
buildBalanceBST(l, m - 1);
|
|
362
|
+
buildBalanceBST(m + 1, r);
|
|
363
|
+
};
|
|
364
|
+
buildBalanceBST(0, n - 1);
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
const stack = [[0, n - 1]];
|
|
369
|
+
while (stack.length > 0) {
|
|
370
|
+
const popped = stack.pop();
|
|
371
|
+
if (popped) {
|
|
372
|
+
const [l, r] = popped;
|
|
373
|
+
if (l <= r) {
|
|
374
|
+
const m = l + Math.floor((r - l) / 2);
|
|
375
|
+
const midNode = sorted[m];
|
|
376
|
+
this.put(midNode.id, midNode.val, midNode.count);
|
|
377
|
+
stack.push([m + 1, r]);
|
|
378
|
+
stack.push([l, m - 1]);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
isAVLBalanced() {
|
|
386
|
+
var _a, _b;
|
|
387
|
+
if (!this.root)
|
|
388
|
+
return true;
|
|
389
|
+
let balanced = true;
|
|
390
|
+
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
391
|
+
const _height = (cur) => {
|
|
392
|
+
if (!cur)
|
|
393
|
+
return 0;
|
|
394
|
+
const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
|
|
395
|
+
if (Math.abs(leftHeight - rightHeight) > 1)
|
|
396
|
+
balanced = false;
|
|
397
|
+
return Math.max(leftHeight, rightHeight) + 1;
|
|
398
|
+
};
|
|
399
|
+
_height(this.root);
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
const stack = [];
|
|
403
|
+
let node = this.root, last = null, depths = new Map();
|
|
404
|
+
while (stack.length > 0 || node) {
|
|
405
|
+
if (node) {
|
|
406
|
+
stack.push(node);
|
|
407
|
+
node = node.left;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
node = stack[stack.length - 1];
|
|
411
|
+
if (!node.right || last === node.right) {
|
|
412
|
+
node = stack.pop();
|
|
413
|
+
if (node) {
|
|
414
|
+
let left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
415
|
+
let right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
416
|
+
if (Math.abs(left - right) > 1)
|
|
417
|
+
return false;
|
|
418
|
+
depths.set(node, 1 + Math.max(left, right));
|
|
419
|
+
last = node;
|
|
420
|
+
node = null;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
else
|
|
424
|
+
node = node.right;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return balanced;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
exports.BST = BST;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./binary-tree"), exports);
|
|
18
|
+
__exportStar(require("./bst"), exports);
|
|
19
|
+
__exportStar(require("./binary-indexed-tree"), exports);
|
|
20
|
+
__exportStar(require("./segment-tree"), exports);
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
4
|
+
class SegmentTreeNode {
|
|
5
|
+
get start() {
|
|
6
|
+
return this._start;
|
|
7
|
+
}
|
|
8
|
+
set start(v) {
|
|
9
|
+
this._start = v;
|
|
10
|
+
}
|
|
11
|
+
get end() {
|
|
12
|
+
return this._end;
|
|
13
|
+
}
|
|
14
|
+
set end(v) {
|
|
15
|
+
this._end = v;
|
|
16
|
+
}
|
|
17
|
+
get val() {
|
|
18
|
+
return this._val;
|
|
19
|
+
}
|
|
20
|
+
set val(v) {
|
|
21
|
+
this._val = v;
|
|
22
|
+
}
|
|
23
|
+
get sum() {
|
|
24
|
+
return this._sum;
|
|
25
|
+
}
|
|
26
|
+
set sum(v) {
|
|
27
|
+
this._sum = v;
|
|
28
|
+
}
|
|
29
|
+
get left() {
|
|
30
|
+
return this._left;
|
|
31
|
+
}
|
|
32
|
+
set left(v) {
|
|
33
|
+
this._left = v;
|
|
34
|
+
}
|
|
35
|
+
get right() {
|
|
36
|
+
return this._right;
|
|
37
|
+
}
|
|
38
|
+
set right(v) {
|
|
39
|
+
this._right = v;
|
|
40
|
+
}
|
|
41
|
+
constructor(start, end, sum, val) {
|
|
42
|
+
this._start = 0;
|
|
43
|
+
this._end = 0;
|
|
44
|
+
this._val = null;
|
|
45
|
+
this._sum = 0;
|
|
46
|
+
this._left = null;
|
|
47
|
+
this._right = null;
|
|
48
|
+
this._start = start;
|
|
49
|
+
this._end = end;
|
|
50
|
+
this._sum = sum;
|
|
51
|
+
this._val = val || null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.SegmentTreeNode = SegmentTreeNode;
|
|
55
|
+
class SegmentTree {
|
|
56
|
+
get root() {
|
|
57
|
+
return this._root;
|
|
58
|
+
}
|
|
59
|
+
constructor(values, start, end) {
|
|
60
|
+
this._values = [];
|
|
61
|
+
this._start = 0;
|
|
62
|
+
start = start || 0;
|
|
63
|
+
end = end || values.length - 1;
|
|
64
|
+
this._values = values;
|
|
65
|
+
this._start = start;
|
|
66
|
+
this._end = end;
|
|
67
|
+
this._root = this.build(start, end);
|
|
68
|
+
}
|
|
69
|
+
build(start, end) {
|
|
70
|
+
if (start === end) {
|
|
71
|
+
return new SegmentTreeNode(start, end, this._values[start]);
|
|
72
|
+
}
|
|
73
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
74
|
+
const left = this.build(start, mid);
|
|
75
|
+
const right = this.build(mid + 1, end);
|
|
76
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
77
|
+
cur.left = left;
|
|
78
|
+
cur.right = right;
|
|
79
|
+
return cur;
|
|
80
|
+
}
|
|
81
|
+
updateNode(index, sum, val) {
|
|
82
|
+
const root = this.root || null;
|
|
83
|
+
if (!root) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const dfs = (cur, index, sum, val) => {
|
|
87
|
+
if (cur.start === cur.end && cur.start === index) {
|
|
88
|
+
cur.sum = sum;
|
|
89
|
+
// cur.val = val;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
93
|
+
if (index <= mid) {
|
|
94
|
+
if (cur.left) {
|
|
95
|
+
dfs(cur.left, index, sum, val);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
if (cur.right) {
|
|
100
|
+
dfs(cur.right, index, sum, val);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (cur.left && cur.right) {
|
|
104
|
+
cur.sum = cur.left.sum + cur.right.sum;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
dfs(root, index, sum);
|
|
108
|
+
}
|
|
109
|
+
querySumByRange(indexA, indexB) {
|
|
110
|
+
const root = this.root || null;
|
|
111
|
+
if (!root) {
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
const dfs = (cur, i, j) => {
|
|
115
|
+
if (cur.start === i && cur.end === j) {
|
|
116
|
+
return cur.sum;
|
|
117
|
+
}
|
|
118
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
119
|
+
if (j <= mid) {
|
|
120
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
121
|
+
if (cur.left) {
|
|
122
|
+
return dfs(cur.left, i, j);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return NaN;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else if (i > mid) {
|
|
129
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
130
|
+
if (cur.right) {
|
|
131
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
132
|
+
return dfs(cur.right, i, j);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return NaN;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
140
|
+
if (cur.left && cur.right) {
|
|
141
|
+
return dfs(cur.left, i, mid) + dfs(cur.right, mid + 1, j);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return NaN;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
return dfs(root, indexA, indexB);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.SegmentTree = SegmentTree;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TreeMultiSet = void 0;
|
|
4
|
+
const bst_1 = require("./bst");
|
|
5
|
+
class TreeMultiSet extends bst_1.BST {
|
|
6
|
+
createNode(id, val, count) {
|
|
7
|
+
return new bst_1.BSTNode(id, val, count);
|
|
8
|
+
}
|
|
9
|
+
put(id, val, count) {
|
|
10
|
+
return super.put(id, val, count);
|
|
11
|
+
}
|
|
12
|
+
remove(id, isUpdateAllLeftSum) {
|
|
13
|
+
return super.remove(id, isUpdateAllLeftSum);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.TreeMultiSet = TreeMultiSet;
|