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.
Files changed (219) hide show
  1. package/.idea/modules.xml +1 -1
  2. package/README.md +197 -2
  3. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
  5. package/dist/data-structures/binary-tree/avl-tree.js +93 -46
  6. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
  10. package/dist/data-structures/binary-tree/binary-tree.js +480 -370
  11. package/dist/data-structures/binary-tree/bst.d.ts +4 -8
  12. package/dist/data-structures/binary-tree/bst.js +152 -107
  13. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  14. package/dist/data-structures/binary-tree/index.js +7 -0
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +91 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
  23. package/dist/data-structures/graph/abstract-graph.js +546 -311
  24. package/dist/data-structures/graph/directed-graph.d.ts +5 -13
  25. package/dist/data-structures/graph/directed-graph.js +250 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
  27. package/dist/data-structures/graph/undirected-graph.js +166 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
  29. package/dist/data-structures/hash/coordinate-map.js +38 -20
  30. package/dist/data-structures/hash/coordinate-set.js +33 -15
  31. package/dist/data-structures/hash/index.d.ts +5 -0
  32. package/dist/data-structures/hash/index.js +5 -0
  33. package/dist/data-structures/heap/heap.d.ts +2 -8
  34. package/dist/data-structures/heap/heap.js +36 -31
  35. package/dist/data-structures/heap/max-heap.d.ts +3 -2
  36. package/dist/data-structures/heap/max-heap.js +27 -9
  37. package/dist/data-structures/heap/min-heap.d.ts +3 -2
  38. package/dist/data-structures/heap/min-heap.js +27 -9
  39. package/dist/data-structures/index.d.ts +2 -0
  40. package/dist/data-structures/index.js +2 -0
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
  44. package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
  45. package/dist/data-structures/matrix/matrix.js +8 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
  47. package/dist/data-structures/matrix/matrix2d.js +80 -63
  48. package/dist/data-structures/matrix/navigator.d.ts +2 -16
  49. package/dist/data-structures/matrix/navigator.js +37 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +18 -18
  51. package/dist/data-structures/matrix/vector2d.js +117 -94
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +159 -116
  58. package/dist/data-structures/queue/deque.js +82 -56
  59. package/dist/data-structures/queue/index.d.ts +1 -0
  60. package/dist/data-structures/queue/index.js +1 -0
  61. package/dist/data-structures/queue/queue.d.ts +9 -10
  62. package/dist/data-structures/queue/queue.js +34 -34
  63. package/dist/data-structures/stack/stack.d.ts +9 -10
  64. package/dist/data-structures/stack/stack.js +31 -31
  65. package/dist/data-structures/trampoline.d.ts +14 -23
  66. package/dist/data-structures/trampoline.js +103 -25
  67. package/dist/data-structures/trie/trie.d.ts +13 -3
  68. package/dist/data-structures/trie/trie.js +234 -80
  69. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  70. package/dist/data-structures/types/abstract-graph.js +2 -0
  71. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  72. package/dist/data-structures/types/avl-tree.js +2 -0
  73. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  74. package/dist/data-structures/types/binary-tree.js +2 -0
  75. package/dist/data-structures/types/bst.d.ts +7 -0
  76. package/dist/data-structures/types/bst.js +2 -0
  77. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  78. package/dist/data-structures/types/directed-graph.js +2 -0
  79. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  80. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  81. package/dist/data-structures/types/heap.d.ts +7 -0
  82. package/dist/data-structures/types/heap.js +2 -0
  83. package/dist/data-structures/types/index.d.ts +13 -0
  84. package/dist/data-structures/types/index.js +29 -0
  85. package/dist/data-structures/types/navigator.d.ts +14 -0
  86. package/dist/data-structures/types/navigator.js +2 -0
  87. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  88. package/dist/data-structures/types/priority-queue.js +2 -0
  89. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  90. package/dist/data-structures/types/segment-tree.js +2 -0
  91. package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
  92. package/dist/data-structures/types/singly-linked-list.js +2 -0
  93. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  94. package/dist/data-structures/types/tree-multiset.js +2 -0
  95. package/dist/{types → data-structures/types}/utils.d.ts +7 -1
  96. package/dist/{types → data-structures/types}/utils.js +20 -19
  97. package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
  98. package/dist/utils/utils.js +651 -0
  99. package/package.json +20 -42
  100. package/src/data-structures/binary-tree/avl-tree.ts +1 -6
  101. package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
  102. package/src/data-structures/binary-tree/binary-tree.ts +184 -139
  103. package/src/data-structures/binary-tree/bst.ts +15 -24
  104. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  105. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  106. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  107. package/src/data-structures/binary-tree/index.ts +7 -0
  108. package/src/data-structures/binary-tree/segment-tree.ts +20 -12
  109. package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
  110. package/src/data-structures/diagrams/README.md +7 -0
  111. package/src/data-structures/graph/abstract-graph.ts +58 -94
  112. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  113. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  114. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  115. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  116. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  117. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  118. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  119. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  120. package/src/data-structures/graph/diagrams/mst.png +0 -0
  121. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  122. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  123. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  124. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  125. package/src/data-structures/graph/directed-graph.ts +12 -28
  126. package/src/data-structures/graph/undirected-graph.ts +11 -10
  127. package/src/data-structures/hash/coordinate-map.ts +1 -1
  128. package/src/data-structures/hash/index.ts +5 -0
  129. package/src/data-structures/heap/heap.ts +2 -11
  130. package/src/data-structures/heap/max-heap.ts +3 -2
  131. package/src/data-structures/heap/min-heap.ts +3 -2
  132. package/src/data-structures/index.ts +2 -0
  133. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
  134. package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
  135. package/src/data-structures/matrix/matrix2d.ts +11 -11
  136. package/src/data-structures/matrix/navigator.ts +2 -14
  137. package/src/data-structures/matrix/vector2d.ts +52 -52
  138. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  139. package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
  140. package/src/data-structures/priority-queue/priority-queue.ts +70 -78
  141. package/src/data-structures/queue/deque.ts +2 -2
  142. package/src/data-structures/queue/index.ts +1 -0
  143. package/src/data-structures/queue/queue.ts +12 -13
  144. package/src/data-structures/stack/stack.ts +12 -13
  145. package/src/data-structures/trampoline.ts +31 -71
  146. package/src/data-structures/trie/trie.ts +61 -11
  147. package/src/data-structures/types/abstract-graph.ts +51 -0
  148. package/src/data-structures/types/avl-tree.ts +6 -0
  149. package/src/data-structures/types/binary-tree.ts +15 -0
  150. package/src/data-structures/types/bst.ts +5 -0
  151. package/src/data-structures/types/directed-graph.ts +18 -0
  152. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  153. package/src/data-structures/types/heap.ts +8 -0
  154. package/src/data-structures/types/index.ts +13 -0
  155. package/src/data-structures/types/navigator.ts +12 -0
  156. package/src/data-structures/types/priority-queue.ts +9 -0
  157. package/src/data-structures/types/segment-tree.ts +1 -0
  158. package/src/data-structures/types/singly-linked-list.ts +15 -0
  159. package/src/data-structures/types/tree-multiset.ts +3 -0
  160. package/src/{types → data-structures/types}/utils.ts +20 -5
  161. package/src/utils/index.ts +1 -0
  162. package/src/{utils.ts → utils/utils.ts} +32 -132
  163. package/tsconfig.json +9 -6
  164. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  165. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  166. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  167. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  168. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  169. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  170. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  171. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  172. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  173. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  174. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  175. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  176. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  177. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  178. package/dist/types/data-structures/graph/index.d.ts +0 -3
  179. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  180. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  181. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  182. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  185. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  186. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  187. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  188. package/dist/types/data-structures/heap/index.d.ts +0 -3
  189. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  190. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  191. package/dist/types/data-structures/index.d.ts +0 -9
  192. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  193. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  194. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  195. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  196. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  197. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  198. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  199. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  200. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  201. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  202. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  203. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  204. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  205. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  206. package/dist/types/data-structures/queue/index.d.ts +0 -1
  207. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  208. package/dist/types/data-structures/stack/index.d.ts +0 -1
  209. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  210. package/dist/types/data-structures/trampoline.d.ts +0 -25
  211. package/dist/types/data-structures/trie/index.d.ts +0 -1
  212. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  213. package/dist/types/types/index.d.ts +0 -1
  214. package/dist/types/types/utils.d.ts +0 -46
  215. package/dist/utils.js +0 -569
  216. package/src/types/index.ts +0 -1
  217. package/src/types/patches/index.d.ts +0 -0
  218. /package/dist/{types → utils}/index.d.ts +0 -0
  219. /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
- class PriorityQueue {
5
- get size() {
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
- const aKey = a, bKey = b;
43
+ this._comparator = function (a, b) {
44
+ var aKey = a, bKey = b;
12
45
  return aKey - bKey;
13
46
  };
14
- const { nodes, comparator, isFix = true } = options;
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) ? [...nodes] : [];
51
+ this.nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
19
52
  isFix && this._fix();
20
53
  }
21
54
  }
22
- _compare(a, b) {
23
- return this._comparator(this.nodes[a], this.nodes[b]) > 0;
24
- }
25
- _swap(a, b) {
26
- const temp = this.nodes[a];
27
- this.nodes[a] = this.nodes[b];
28
- this.nodes[b] = temp;
29
- }
30
- _isValidIndex(index) {
31
- return index > -1 && index < this.nodes.length;
32
- }
33
- _getParent(child) {
34
- return Math.floor((child - 1) / 2);
35
- }
36
- _getLeft(parent) {
37
- return (2 * parent) + 1;
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
- let res = null;
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 [...this.nodes];
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
- const isValidRecursive = (parentIndex) => {
111
- let isValidLeft = true;
112
- let isValidRight = true;
113
- if (this._getLeft(parentIndex) !== -1) {
114
- const leftChildIndex = (parentIndex * 2) + 1;
115
- if (!this._compare(parentIndex, leftChildIndex))
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 (this._getRight(parentIndex) !== -1) {
120
- const rightChildIndex = (parentIndex * 2) + 2;
121
- if (!this._compare(parentIndex, rightChildIndex))
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
- const visitedNode = [];
127
+ };
128
+ PriorityQueue.prototype.sort = function () {
129
+ var visitedNode = [];
131
130
  while (this.size !== 0) {
132
- const top = this.poll();
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
- const visitedNode = [];
140
- const traverse = (cur) => {
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
- const leftChildIndex = this._getLeft(cur);
143
- const rightChildIndex = this._getRight(cur);
142
+ var leftChildIndex = _this._getLeft(cur);
143
+ var rightChildIndex = _this._getRight(cur);
144
144
  switch (dfsMode) {
145
145
  case 'in':
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);
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 = this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
152
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
153
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
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
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
157
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
158
- visitedNode.push((_c = this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
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
- static heapify(options) {
166
- const heap = new PriorityQueue(options);
167
- heap._fix();
168
- return heap;
169
- }
170
- static isPriorityQueueified(options) {
171
- return new PriorityQueue(Object.assign(Object.assign({}, options), { isFix: true })).isValid();
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
- const linked_list_1 = require("../linked-list");
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
- class Deque extends linked_list_1.DoublyLinkedList {
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
- class ObjectDeque {
14
- constructor(capacity) {
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
- const mid = Math.floor(this._capacity / 2);
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
- const mid = Math.floor(this._capacity / 2);
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
- let value = this.peekFirst();
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
- let value = this.peekLast();
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
- class ArrayDeque {
87
- constructor() {
107
+ var ArrayDeque = /** @class */ (function () {
108
+ function ArrayDeque() {
88
109
  this._nodes = [];
89
110
  }
90
- get size() {
91
- return this._nodes.length;
92
- }
93
- offerLast(value) {
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 +1,2 @@
1
1
  export * from './queue';
2
+ export * from './deque';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./queue"), exports);
18
+ __exportStar(require("./deque"), exports);
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2020 Pablo
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
  }