data-structure-typed 0.8.17 → 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/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +7 -0
- 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 +2 -0
- package/dist/data-structures/index.js +2 -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/index.d.ts +1 -0
- package/dist/data-structures/queue/index.js +1 -0
- 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/index.ts +7 -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 +2 -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/index.ts +1 -0
- 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,84 +1,82 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.PriorityQueue = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return this.nodes.length;
|
|
7
|
-
}
|
|
8
|
-
constructor(options) {
|
|
40
|
+
var PriorityQueue = /** @class */ (function () {
|
|
41
|
+
function PriorityQueue(options) {
|
|
9
42
|
this.nodes = [];
|
|
10
|
-
this._comparator = (a, b)
|
|
11
|
-
|
|
43
|
+
this._comparator = function (a, b) {
|
|
44
|
+
var aKey = a, bKey = b;
|
|
12
45
|
return aKey - bKey;
|
|
13
46
|
};
|
|
14
|
-
|
|
47
|
+
var nodes = options.nodes, comparator = options.comparator, _a = options.isFix, isFix = _a === void 0 ? true : _a;
|
|
15
48
|
this._comparator = comparator;
|
|
16
49
|
if (nodes && nodes instanceof Array && nodes.length > 0) {
|
|
17
50
|
// TODO support distinct
|
|
18
|
-
this.nodes = Array.isArray(nodes) ? [
|
|
51
|
+
this.nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
|
|
19
52
|
isFix && this._fix();
|
|
20
53
|
}
|
|
21
54
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
_getRight(parent) {
|
|
40
|
-
return (2 * parent) + 2;
|
|
41
|
-
}
|
|
42
|
-
_getComparedChild(parent) {
|
|
43
|
-
let min = parent;
|
|
44
|
-
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
45
|
-
if (left < this.size && this._compare(min, left)) {
|
|
46
|
-
min = left;
|
|
47
|
-
}
|
|
48
|
-
if (right < this.size && this._compare(min, right)) {
|
|
49
|
-
min = right;
|
|
50
|
-
}
|
|
51
|
-
return min;
|
|
52
|
-
}
|
|
53
|
-
_heapifyUp(start) {
|
|
54
|
-
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
55
|
-
const parent = this._getParent(start);
|
|
56
|
-
this._swap(start, parent);
|
|
57
|
-
start = parent;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
_heapifyDown(start) {
|
|
61
|
-
let min = this._getComparedChild(start);
|
|
62
|
-
while (this._compare(start, min)) {
|
|
63
|
-
this._swap(min, start);
|
|
64
|
-
start = min;
|
|
65
|
-
min = this._getComparedChild(start);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
_fix() {
|
|
69
|
-
for (let i = Math.floor(this.size / 2); i > -1; i--)
|
|
70
|
-
this._heapifyDown(i);
|
|
71
|
-
}
|
|
72
|
-
offer(node) {
|
|
55
|
+
Object.defineProperty(PriorityQueue.prototype, "size", {
|
|
56
|
+
get: function () {
|
|
57
|
+
return this.nodes.length;
|
|
58
|
+
},
|
|
59
|
+
enumerable: false,
|
|
60
|
+
configurable: true
|
|
61
|
+
});
|
|
62
|
+
PriorityQueue.heapify = function (options) {
|
|
63
|
+
var heap = new PriorityQueue(options);
|
|
64
|
+
heap._fix();
|
|
65
|
+
return heap;
|
|
66
|
+
};
|
|
67
|
+
PriorityQueue.isPriorityQueueified = function (options) {
|
|
68
|
+
return new PriorityQueue(__assign(__assign({}, options), { isFix: true })).isValid();
|
|
69
|
+
};
|
|
70
|
+
PriorityQueue.prototype.offer = function (node) {
|
|
73
71
|
this.nodes.push(node);
|
|
74
72
|
this._heapifyUp(this.size - 1);
|
|
75
|
-
}
|
|
76
|
-
peek() {
|
|
73
|
+
};
|
|
74
|
+
PriorityQueue.prototype.peek = function () {
|
|
77
75
|
return this.size ? this.nodes[0] : null;
|
|
78
|
-
}
|
|
79
|
-
poll() {
|
|
76
|
+
};
|
|
77
|
+
PriorityQueue.prototype.poll = function () {
|
|
80
78
|
var _a, _b;
|
|
81
|
-
|
|
79
|
+
var res = null;
|
|
82
80
|
if (this.size > 1) {
|
|
83
81
|
this._swap(0, this.nodes.length - 1);
|
|
84
82
|
res = (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
@@ -88,87 +86,132 @@ class PriorityQueue {
|
|
|
88
86
|
res = (_b = this.nodes.pop()) !== null && _b !== void 0 ? _b : null;
|
|
89
87
|
}
|
|
90
88
|
return res;
|
|
91
|
-
}
|
|
92
|
-
leaf() {
|
|
89
|
+
};
|
|
90
|
+
PriorityQueue.prototype.leaf = function () {
|
|
93
91
|
var _a;
|
|
94
92
|
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
|
|
95
|
-
}
|
|
96
|
-
isEmpty() {
|
|
93
|
+
};
|
|
94
|
+
PriorityQueue.prototype.isEmpty = function () {
|
|
97
95
|
return this.size === 0;
|
|
98
|
-
}
|
|
99
|
-
clear() {
|
|
96
|
+
};
|
|
97
|
+
PriorityQueue.prototype.clear = function () {
|
|
100
98
|
this.nodes = [];
|
|
101
|
-
}
|
|
102
|
-
toArray() {
|
|
103
|
-
return [
|
|
104
|
-
}
|
|
105
|
-
clone() {
|
|
99
|
+
};
|
|
100
|
+
PriorityQueue.prototype.toArray = function () {
|
|
101
|
+
return __spreadArray([], __read(this.nodes), false);
|
|
102
|
+
};
|
|
103
|
+
PriorityQueue.prototype.clone = function () {
|
|
106
104
|
return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
|
|
107
|
-
}
|
|
105
|
+
};
|
|
108
106
|
// --- start additional methods ---
|
|
109
|
-
isValid() {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
PriorityQueue.prototype.isValid = function () {
|
|
108
|
+
var _this = this;
|
|
109
|
+
var isValidRecursive = function (parentIndex) {
|
|
110
|
+
var isValidLeft = true;
|
|
111
|
+
var isValidRight = true;
|
|
112
|
+
if (_this._getLeft(parentIndex) !== -1) {
|
|
113
|
+
var leftChildIndex = (parentIndex * 2) + 1;
|
|
114
|
+
if (!_this._compare(parentIndex, leftChildIndex))
|
|
116
115
|
return false;
|
|
117
116
|
isValidLeft = isValidRecursive(leftChildIndex);
|
|
118
117
|
}
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
if (!
|
|
118
|
+
if (_this._getRight(parentIndex) !== -1) {
|
|
119
|
+
var rightChildIndex = (parentIndex * 2) + 2;
|
|
120
|
+
if (!_this._compare(parentIndex, rightChildIndex))
|
|
122
121
|
return false;
|
|
123
122
|
isValidRight = isValidRecursive(rightChildIndex);
|
|
124
123
|
}
|
|
125
124
|
return isValidLeft && isValidRight;
|
|
126
125
|
};
|
|
127
126
|
return isValidRecursive(0);
|
|
128
|
-
}
|
|
129
|
-
sort() {
|
|
130
|
-
|
|
127
|
+
};
|
|
128
|
+
PriorityQueue.prototype.sort = function () {
|
|
129
|
+
var visitedNode = [];
|
|
131
130
|
while (this.size !== 0) {
|
|
132
|
-
|
|
131
|
+
var top = this.poll();
|
|
133
132
|
if (top)
|
|
134
133
|
visitedNode.push(top);
|
|
135
134
|
}
|
|
136
135
|
return visitedNode;
|
|
137
|
-
}
|
|
138
|
-
DFS(dfsMode) {
|
|
139
|
-
|
|
140
|
-
|
|
136
|
+
};
|
|
137
|
+
PriorityQueue.prototype.DFS = function (dfsMode) {
|
|
138
|
+
var _this = this;
|
|
139
|
+
var visitedNode = [];
|
|
140
|
+
var traverse = function (cur) {
|
|
141
141
|
var _a, _b, _c;
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
var leftChildIndex = _this._getLeft(cur);
|
|
143
|
+
var rightChildIndex = _this._getRight(cur);
|
|
144
144
|
switch (dfsMode) {
|
|
145
145
|
case 'in':
|
|
146
|
-
|
|
147
|
-
visitedNode.push((_a =
|
|
148
|
-
|
|
146
|
+
_this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
147
|
+
visitedNode.push((_a = _this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
|
|
148
|
+
_this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
149
149
|
break;
|
|
150
150
|
case 'pre':
|
|
151
|
-
visitedNode.push((_b =
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
visitedNode.push((_b = _this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
|
|
152
|
+
_this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
153
|
+
_this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
154
154
|
break;
|
|
155
155
|
case 'post':
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
visitedNode.push((_c =
|
|
156
|
+
_this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
157
|
+
_this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
158
|
+
visitedNode.push((_c = _this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
|
|
159
159
|
break;
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
162
|
this._isValidIndex(0) && traverse(0);
|
|
163
163
|
return visitedNode;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
164
|
+
};
|
|
165
|
+
PriorityQueue.prototype._compare = function (a, b) {
|
|
166
|
+
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
167
|
+
};
|
|
168
|
+
PriorityQueue.prototype._swap = function (a, b) {
|
|
169
|
+
var temp = this.nodes[a];
|
|
170
|
+
this.nodes[a] = this.nodes[b];
|
|
171
|
+
this.nodes[b] = temp;
|
|
172
|
+
};
|
|
173
|
+
PriorityQueue.prototype._isValidIndex = function (index) {
|
|
174
|
+
return index > -1 && index < this.nodes.length;
|
|
175
|
+
};
|
|
176
|
+
PriorityQueue.prototype._getParent = function (child) {
|
|
177
|
+
return Math.floor((child - 1) / 2);
|
|
178
|
+
};
|
|
179
|
+
PriorityQueue.prototype._getLeft = function (parent) {
|
|
180
|
+
return (2 * parent) + 1;
|
|
181
|
+
};
|
|
182
|
+
PriorityQueue.prototype._getRight = function (parent) {
|
|
183
|
+
return (2 * parent) + 2;
|
|
184
|
+
};
|
|
185
|
+
PriorityQueue.prototype._getComparedChild = function (parent) {
|
|
186
|
+
var min = parent;
|
|
187
|
+
var left = this._getLeft(parent), right = this._getRight(parent);
|
|
188
|
+
if (left < this.size && this._compare(min, left)) {
|
|
189
|
+
min = left;
|
|
190
|
+
}
|
|
191
|
+
if (right < this.size && this._compare(min, right)) {
|
|
192
|
+
min = right;
|
|
193
|
+
}
|
|
194
|
+
return min;
|
|
195
|
+
};
|
|
196
|
+
PriorityQueue.prototype._heapifyUp = function (start) {
|
|
197
|
+
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
198
|
+
var parent = this._getParent(start);
|
|
199
|
+
this._swap(start, parent);
|
|
200
|
+
start = parent;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
PriorityQueue.prototype._heapifyDown = function (start) {
|
|
204
|
+
var min = this._getComparedChild(start);
|
|
205
|
+
while (this._compare(start, min)) {
|
|
206
|
+
this._swap(min, start);
|
|
207
|
+
start = min;
|
|
208
|
+
min = this._getComparedChild(start);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
PriorityQueue.prototype._fix = function () {
|
|
212
|
+
for (var i = Math.floor(this.size / 2); i > -1; i--)
|
|
213
|
+
this._heapifyDown(i);
|
|
214
|
+
};
|
|
215
|
+
return PriorityQueue;
|
|
216
|
+
}());
|
|
174
217
|
exports.PriorityQueue = PriorityQueue;
|
|
@@ -1,17 +1,37 @@
|
|
|
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
|
+
})();
|
|
2
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
18
|
exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
|
|
4
|
-
|
|
19
|
+
var linked_list_1 = require("../linked-list");
|
|
5
20
|
// O(n) time complexity of obtaining the value
|
|
6
21
|
// O(1) time complexity of adding at the beginning and the end
|
|
7
|
-
|
|
8
|
-
|
|
22
|
+
var Deque = /** @class */ (function (_super) {
|
|
23
|
+
__extends(Deque, _super);
|
|
24
|
+
function Deque() {
|
|
25
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
26
|
+
}
|
|
27
|
+
return Deque;
|
|
28
|
+
}(linked_list_1.DoublyLinkedList));
|
|
9
29
|
exports.Deque = Deque;
|
|
10
30
|
// O(1) time complexity of obtaining the value
|
|
11
31
|
// O(n) time complexity of adding at the beginning and the end
|
|
12
32
|
// todo tested slowest one
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
var ObjectDeque = /** @class */ (function () {
|
|
34
|
+
function ObjectDeque(capacity) {
|
|
15
35
|
this._nodes = {};
|
|
16
36
|
this._capacity = Number.MAX_SAFE_INTEGER;
|
|
17
37
|
this._first = -1;
|
|
@@ -20,12 +40,12 @@ class ObjectDeque {
|
|
|
20
40
|
if (capacity !== undefined)
|
|
21
41
|
this._capacity = capacity;
|
|
22
42
|
}
|
|
23
|
-
size() {
|
|
43
|
+
ObjectDeque.prototype.size = function () {
|
|
24
44
|
return this._size;
|
|
25
|
-
}
|
|
26
|
-
offerFirst(value) {
|
|
45
|
+
};
|
|
46
|
+
ObjectDeque.prototype.offerFirst = function (value) {
|
|
27
47
|
if (this._size === 0) {
|
|
28
|
-
|
|
48
|
+
var mid = Math.floor(this._capacity / 2);
|
|
29
49
|
this._first = mid;
|
|
30
50
|
this._last = mid;
|
|
31
51
|
}
|
|
@@ -34,10 +54,10 @@ class ObjectDeque {
|
|
|
34
54
|
}
|
|
35
55
|
this._nodes[this._first] = value;
|
|
36
56
|
this._size++;
|
|
37
|
-
}
|
|
38
|
-
offerLast(value) {
|
|
57
|
+
};
|
|
58
|
+
ObjectDeque.prototype.offerLast = function (value) {
|
|
39
59
|
if (this._size === 0) {
|
|
40
|
-
|
|
60
|
+
var mid = Math.floor(this._capacity / 2);
|
|
41
61
|
this._first = mid;
|
|
42
62
|
this._last = mid;
|
|
43
63
|
}
|
|
@@ -46,87 +66,93 @@ class ObjectDeque {
|
|
|
46
66
|
}
|
|
47
67
|
this._nodes[this._last] = value;
|
|
48
68
|
this._size++;
|
|
49
|
-
}
|
|
50
|
-
pollFirst() {
|
|
69
|
+
};
|
|
70
|
+
ObjectDeque.prototype.pollFirst = function () {
|
|
51
71
|
if (!this._size)
|
|
52
72
|
return;
|
|
53
|
-
|
|
73
|
+
var value = this.peekFirst();
|
|
54
74
|
delete this._nodes[this._first];
|
|
55
75
|
this._first++;
|
|
56
76
|
this._size--;
|
|
57
77
|
return value;
|
|
58
|
-
}
|
|
59
|
-
peekFirst() {
|
|
78
|
+
};
|
|
79
|
+
ObjectDeque.prototype.peekFirst = function () {
|
|
60
80
|
if (this._size)
|
|
61
81
|
return this._nodes[this._first];
|
|
62
|
-
}
|
|
63
|
-
pollLast() {
|
|
82
|
+
};
|
|
83
|
+
ObjectDeque.prototype.pollLast = function () {
|
|
64
84
|
if (!this._size)
|
|
65
85
|
return;
|
|
66
|
-
|
|
86
|
+
var value = this.peekLast();
|
|
67
87
|
delete this._nodes[this._last];
|
|
68
88
|
this._last--;
|
|
69
89
|
this._size--;
|
|
70
90
|
return value;
|
|
71
|
-
}
|
|
72
|
-
peekLast() {
|
|
91
|
+
};
|
|
92
|
+
ObjectDeque.prototype.peekLast = function () {
|
|
73
93
|
if (this._size)
|
|
74
94
|
return this._nodes[this._last];
|
|
75
|
-
}
|
|
76
|
-
get(index) {
|
|
95
|
+
};
|
|
96
|
+
ObjectDeque.prototype.get = function (index) {
|
|
77
97
|
return this._nodes[this._first + index] || null;
|
|
78
|
-
}
|
|
79
|
-
isEmpty() {
|
|
98
|
+
};
|
|
99
|
+
ObjectDeque.prototype.isEmpty = function () {
|
|
80
100
|
return this._size <= 0;
|
|
81
|
-
}
|
|
82
|
-
|
|
101
|
+
};
|
|
102
|
+
return ObjectDeque;
|
|
103
|
+
}());
|
|
83
104
|
exports.ObjectDeque = ObjectDeque;
|
|
84
105
|
// O(1) time complexity of obtaining the value
|
|
85
106
|
// O(n) time complexity of adding at the beginning and the end
|
|
86
|
-
|
|
87
|
-
|
|
107
|
+
var ArrayDeque = /** @class */ (function () {
|
|
108
|
+
function ArrayDeque() {
|
|
88
109
|
this._nodes = [];
|
|
89
110
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
Object.defineProperty(ArrayDeque.prototype, "size", {
|
|
112
|
+
get: function () {
|
|
113
|
+
return this._nodes.length;
|
|
114
|
+
},
|
|
115
|
+
enumerable: false,
|
|
116
|
+
configurable: true
|
|
117
|
+
});
|
|
118
|
+
ArrayDeque.prototype.offerLast = function (value) {
|
|
94
119
|
return this._nodes.push(value);
|
|
95
|
-
}
|
|
96
|
-
pollLast() {
|
|
120
|
+
};
|
|
121
|
+
ArrayDeque.prototype.pollLast = function () {
|
|
97
122
|
var _a;
|
|
98
123
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
99
|
-
}
|
|
100
|
-
pollFirst() {
|
|
124
|
+
};
|
|
125
|
+
ArrayDeque.prototype.pollFirst = function () {
|
|
101
126
|
var _a;
|
|
102
127
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
103
|
-
}
|
|
104
|
-
offerFirst(value) {
|
|
128
|
+
};
|
|
129
|
+
ArrayDeque.prototype.offerFirst = function (value) {
|
|
105
130
|
return this._nodes.unshift(value);
|
|
106
|
-
}
|
|
107
|
-
peekFirst() {
|
|
131
|
+
};
|
|
132
|
+
ArrayDeque.prototype.peekFirst = function () {
|
|
108
133
|
var _a;
|
|
109
134
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
110
|
-
}
|
|
111
|
-
peekLast() {
|
|
135
|
+
};
|
|
136
|
+
ArrayDeque.prototype.peekLast = function () {
|
|
112
137
|
var _a;
|
|
113
138
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
114
|
-
}
|
|
115
|
-
get(index) {
|
|
139
|
+
};
|
|
140
|
+
ArrayDeque.prototype.get = function (index) {
|
|
116
141
|
var _a;
|
|
117
142
|
return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
118
|
-
}
|
|
119
|
-
set(index, value) {
|
|
143
|
+
};
|
|
144
|
+
ArrayDeque.prototype.set = function (index, value) {
|
|
120
145
|
return this._nodes[index] = value;
|
|
121
|
-
}
|
|
122
|
-
insert(index, value) {
|
|
146
|
+
};
|
|
147
|
+
ArrayDeque.prototype.insert = function (index, value) {
|
|
123
148
|
return this._nodes.splice(index, 0, value);
|
|
124
|
-
}
|
|
125
|
-
remove(index) {
|
|
149
|
+
};
|
|
150
|
+
ArrayDeque.prototype.remove = function (index) {
|
|
126
151
|
return this._nodes.splice(index, 1);
|
|
127
|
-
}
|
|
128
|
-
isEmpty() {
|
|
152
|
+
};
|
|
153
|
+
ArrayDeque.prototype.isEmpty = function () {
|
|
129
154
|
return this._nodes.length === 0;
|
|
130
|
-
}
|
|
131
|
-
|
|
155
|
+
};
|
|
156
|
+
return ArrayDeque;
|
|
157
|
+
}());
|
|
132
158
|
exports.ArrayDeque = ArrayDeque;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright 2020
|
|
4
|
-
*
|
|
3
|
+
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
5
4
|
* @class
|
|
6
5
|
*/
|
|
7
6
|
export declare class Queue<T> {
|
|
@@ -12,6 +11,14 @@ export declare class Queue<T> {
|
|
|
12
11
|
* @param {array} [elements]
|
|
13
12
|
*/
|
|
14
13
|
constructor(elements?: T[]);
|
|
14
|
+
/**
|
|
15
|
+
* Creates a queue from an existing array.
|
|
16
|
+
* @public
|
|
17
|
+
* @static
|
|
18
|
+
* @param {array} elements
|
|
19
|
+
* @return {Queue}
|
|
20
|
+
*/
|
|
21
|
+
static fromArray<T>(elements: T[]): Queue<T>;
|
|
15
22
|
/**
|
|
16
23
|
* Adds an element at the back of the queue.
|
|
17
24
|
* @public
|
|
@@ -65,12 +72,4 @@ export declare class Queue<T> {
|
|
|
65
72
|
* @return {Queue}
|
|
66
73
|
*/
|
|
67
74
|
clone(): Queue<T>;
|
|
68
|
-
/**
|
|
69
|
-
* Creates a queue from an existing array.
|
|
70
|
-
* @public
|
|
71
|
-
* @static
|
|
72
|
-
* @param {array} elements
|
|
73
|
-
* @return {Queue}
|
|
74
|
-
*/
|
|
75
|
-
static fromArray<T>(elements: T[]): Queue<T>;
|
|
76
75
|
}
|