min-heap-typed 1.40.0-rc → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +422 -422
- package/.eslintrc.js +59 -59
- package/.prettierrc.js +14 -14
- package/README.md +20 -3
- package/coverage/clover.xml +11 -7
- package/coverage/coverage-final.json +95 -1
- package/coverage/coverage-summary.json +59 -2
- package/coverage/lcov-report/base.css +278 -99
- package/coverage/lcov-report/index.html +69 -65
- package/coverage/lcov-report/index.ts.html +36 -35
- package/coverage/lcov-report/sorter.js +15 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +347 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +355 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -0
- package/dist/data-structures/binary-tree/rb-tree.js +21 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
- package/dist/data-structures/graph/abstract-graph.js +884 -0
- package/dist/data-structures/graph/directed-graph.d.ts +194 -0
- package/dist/data-structures/graph/directed-graph.js +404 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
- package/dist/data-structures/graph/undirected-graph.js +239 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
- package/dist/data-structures/hash/coordinate-map.js +62 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
- package/dist/data-structures/hash/coordinate-set.js +52 -0
- package/dist/data-structures/hash/hash-map.d.ts +50 -0
- package/dist/data-structures/hash/hash-map.js +153 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +235 -0
- package/dist/data-structures/heap/heap.js +515 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +161 -0
- package/dist/data-structures/queue/deque.js +264 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +122 -0
- package/dist/data-structures/queue/queue.js +187 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +64 -0
- package/dist/data-structures/stack/stack.js +94 -0
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +79 -0
- package/dist/data-structures/trie/trie.js +251 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +18 -4
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/helpers.d.ts +8 -0
- package/dist/types/helpers.js +9 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +73 -0
- package/jest.config.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +350 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
- package/src/data-structures/binary-tree/bst.ts +522 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +358 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
- package/src/data-structures/graph/abstract-graph.ts +1000 -0
- package/src/data-structures/graph/directed-graph.ts +449 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +259 -0
- package/src/data-structures/hash/coordinate-map.ts +63 -0
- package/src/data-structures/hash/coordinate-set.ts +52 -0
- package/src/data-structures/hash/hash-map.ts +185 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +589 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +282 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +209 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +102 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +262 -0
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +11 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +86 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license MIT
|
|
3
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
+
* @class
|
|
5
|
+
*/
|
|
6
|
+
import {SinglyLinkedList} from '../linked-list';
|
|
7
|
+
|
|
8
|
+
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
|
+
/**
|
|
10
|
+
* The enqueue function adds a value to the end of an array.
|
|
11
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
12
|
+
*/
|
|
13
|
+
enqueue(value: E) {
|
|
14
|
+
this.push(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
19
|
+
* @returns The method is returning the element at the front of the queue, or null if the queue is empty.
|
|
20
|
+
*/
|
|
21
|
+
dequeue(): E | undefined {
|
|
22
|
+
return this.shift();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
+
*/
|
|
29
|
+
getFirst(): E | undefined {
|
|
30
|
+
return this.head?.value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
35
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
36
|
+
*/
|
|
37
|
+
peek(): E | undefined {
|
|
38
|
+
return this.getFirst();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class Queue<E = any> {
|
|
43
|
+
/**
|
|
44
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
45
|
+
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
46
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
47
|
+
* initialized as an empty array.
|
|
48
|
+
*/
|
|
49
|
+
constructor(elements?: E[]) {
|
|
50
|
+
this._nodes = elements || [];
|
|
51
|
+
this._offset = 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected _nodes: E[];
|
|
55
|
+
|
|
56
|
+
get nodes(): E[] {
|
|
57
|
+
return this._nodes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected _offset: number;
|
|
61
|
+
|
|
62
|
+
get offset(): number {
|
|
63
|
+
return this._offset;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The size function returns the number of elements in an array.
|
|
68
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
69
|
+
*/
|
|
70
|
+
get size(): number {
|
|
71
|
+
return this.nodes.length - this.offset;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
76
|
+
* @public
|
|
77
|
+
* @static
|
|
78
|
+
* @param {E[]} elements - The "elements" parameter is an array of elements of type E.
|
|
79
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
80
|
+
* array.
|
|
81
|
+
*/
|
|
82
|
+
static fromArray<E>(elements: E[]): Queue<E> {
|
|
83
|
+
return new Queue(elements);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
88
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
89
|
+
* @returns The `add` method is returning a `Queue<E>` object.
|
|
90
|
+
*/
|
|
91
|
+
push(element: E): Queue<E> {
|
|
92
|
+
this.nodes.push(element);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
98
|
+
* necessary to optimize performance.
|
|
99
|
+
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
|
|
100
|
+
*/
|
|
101
|
+
shift(): E | undefined {
|
|
102
|
+
if (this.size === 0) return undefined;
|
|
103
|
+
|
|
104
|
+
const first = this.getFirst();
|
|
105
|
+
this._offset += 1;
|
|
106
|
+
|
|
107
|
+
if (this.offset * 2 < this.nodes.length) return first;
|
|
108
|
+
|
|
109
|
+
// only delete dequeued elements when reaching half size
|
|
110
|
+
// to decrease latency of shifting elements.
|
|
111
|
+
this._nodes = this.nodes.slice(this.offset);
|
|
112
|
+
this._offset = 0;
|
|
113
|
+
return first;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
118
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
119
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
120
|
+
*/
|
|
121
|
+
getFirst(): E | undefined {
|
|
122
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
127
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
128
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
129
|
+
*/
|
|
130
|
+
peek(): E | undefined {
|
|
131
|
+
return this.getFirst();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
136
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
137
|
+
* array is empty, it returns `null`.
|
|
138
|
+
*/
|
|
139
|
+
getLast(): E | undefined {
|
|
140
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
145
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
146
|
+
* array is empty, it returns `null`.
|
|
147
|
+
*/
|
|
148
|
+
peekLast(): E | undefined {
|
|
149
|
+
return this.getLast();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The enqueue function adds a value to the end of a queue.
|
|
154
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
155
|
+
*/
|
|
156
|
+
enqueue(value: E) {
|
|
157
|
+
this.push(value);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
162
|
+
* @returns The method is returning a value of type E or null.
|
|
163
|
+
*/
|
|
164
|
+
dequeue(): E | undefined {
|
|
165
|
+
return this.shift();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
getAt(index: number): E | undefined {
|
|
169
|
+
return this.nodes[index];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
174
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
175
|
+
*/
|
|
176
|
+
isEmpty(): boolean {
|
|
177
|
+
return this.size === 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
182
|
+
* @returns An array of type E is being returned.
|
|
183
|
+
*/
|
|
184
|
+
toArray(): E[] {
|
|
185
|
+
return this.nodes.slice(this.offset);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
190
|
+
*/
|
|
191
|
+
clear(): void {
|
|
192
|
+
this._nodes = [];
|
|
193
|
+
this._offset = 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
198
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
199
|
+
*/
|
|
200
|
+
clone(): Queue<E> {
|
|
201
|
+
return new Queue(this.nodes.slice(this.offset));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
* [Symbol.iterator]() {
|
|
205
|
+
for (const item of this.nodes) {
|
|
206
|
+
yield item;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stack';
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license MIT
|
|
3
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
+
* @class
|
|
5
|
+
*/
|
|
6
|
+
export class Stack<E = any> {
|
|
7
|
+
/**
|
|
8
|
+
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
9
|
+
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
10
|
+
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
11
|
+
* is provided and is an array, it is assigned to the `_elements
|
|
12
|
+
*/
|
|
13
|
+
constructor(elements?: E[]) {
|
|
14
|
+
this._elements = Array.isArray(elements) ? elements : [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected _elements: E[];
|
|
18
|
+
|
|
19
|
+
get elements(): E[] {
|
|
20
|
+
return this._elements;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
25
|
+
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
26
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
27
|
+
* array.
|
|
28
|
+
*/
|
|
29
|
+
static fromArray<E>(elements: E[]): Stack<E> {
|
|
30
|
+
return new Stack(elements);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
35
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
36
|
+
*/
|
|
37
|
+
isEmpty(): boolean {
|
|
38
|
+
return this.elements.length === 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The size() function returns the number of elements in an array.
|
|
43
|
+
* @returns The size of the elements array.
|
|
44
|
+
*/
|
|
45
|
+
size(): number {
|
|
46
|
+
return this.elements.length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
51
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
|
|
52
|
+
*/
|
|
53
|
+
peek(): E | null {
|
|
54
|
+
if (this.isEmpty()) return null;
|
|
55
|
+
|
|
56
|
+
return this.elements[this.elements.length - 1];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
61
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
62
|
+
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
63
|
+
*/
|
|
64
|
+
push(element: E): Stack<E> {
|
|
65
|
+
this.elements.push(element);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
|
|
71
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
72
|
+
* array is empty, it returns `null`.
|
|
73
|
+
*/
|
|
74
|
+
pop(): E | null {
|
|
75
|
+
if (this.isEmpty()) return null;
|
|
76
|
+
|
|
77
|
+
return this.elements.pop() || null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The toArray function returns a copy of the elements in an array.
|
|
82
|
+
* @returns An array of type E.
|
|
83
|
+
*/
|
|
84
|
+
toArray(): E[] {
|
|
85
|
+
return this.elements.slice();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The clear function clears the elements array.
|
|
90
|
+
*/
|
|
91
|
+
clear(): void {
|
|
92
|
+
this._elements = [];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
97
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
98
|
+
*/
|
|
99
|
+
clone(): Stack<E> {
|
|
100
|
+
return new Stack(this.elements.slice());
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './tree';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class TreeNode<V = any> {
|
|
2
|
+
key: string;
|
|
3
|
+
value?: V | undefined;
|
|
4
|
+
children?: TreeNode<V>[] | undefined;
|
|
5
|
+
|
|
6
|
+
constructor(key: string, value?: V, children?: TreeNode<V>[]) {
|
|
7
|
+
this.key = key;
|
|
8
|
+
this.value = value || undefined;
|
|
9
|
+
this.children = children || [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
addChildren(children: TreeNode<V> | TreeNode<V>[]) {
|
|
13
|
+
if (!this.children) {
|
|
14
|
+
this.children = [];
|
|
15
|
+
}
|
|
16
|
+
if (children instanceof TreeNode) {
|
|
17
|
+
this.children.push(children);
|
|
18
|
+
} else {
|
|
19
|
+
this.children = this.children.concat(children);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getHeight() {
|
|
24
|
+
let maxDepth = 0;
|
|
25
|
+
if (this) {
|
|
26
|
+
const bfs = (node: TreeNode<V>, level: number) => {
|
|
27
|
+
if (level > maxDepth) {
|
|
28
|
+
maxDepth = level;
|
|
29
|
+
}
|
|
30
|
+
const {children} = node;
|
|
31
|
+
if (children) {
|
|
32
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
33
|
+
bfs(children[i], level + 1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
bfs(this, 0);
|
|
38
|
+
}
|
|
39
|
+
return maxDepth;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './trie';
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
11
|
+
* and a flag indicating whether it's the end of a word.
|
|
12
|
+
*/
|
|
13
|
+
export class TrieNode {
|
|
14
|
+
key: string;
|
|
15
|
+
children: Map<string, TrieNode>;
|
|
16
|
+
isEnd: boolean;
|
|
17
|
+
|
|
18
|
+
constructor(key: string) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.isEnd = false;
|
|
21
|
+
this.children = new Map<string, TrieNode>();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
27
|
+
*/
|
|
28
|
+
export class Trie {
|
|
29
|
+
constructor(words?: string[], caseSensitive = true) {
|
|
30
|
+
this._root = new TrieNode('');
|
|
31
|
+
this._caseSensitive = caseSensitive;
|
|
32
|
+
if (words) {
|
|
33
|
+
for (const i of words) {
|
|
34
|
+
this.add(i);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected _caseSensitive: boolean;
|
|
40
|
+
|
|
41
|
+
get caseSensitive(): boolean {
|
|
42
|
+
return this._caseSensitive;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected _root: TrieNode;
|
|
46
|
+
|
|
47
|
+
get root() {
|
|
48
|
+
return this._root;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Add a word to the Trie structure.
|
|
53
|
+
* @param {string} word - The word to add.
|
|
54
|
+
* @returns {boolean} True if the word was successfully added.
|
|
55
|
+
*/
|
|
56
|
+
add(word: string): boolean {
|
|
57
|
+
word = this._caseProcess(word);
|
|
58
|
+
let cur = this.root;
|
|
59
|
+
for (const c of word) {
|
|
60
|
+
let nodeC = cur.children.get(c);
|
|
61
|
+
if (!nodeC) {
|
|
62
|
+
nodeC = new TrieNode(c);
|
|
63
|
+
cur.children.set(c, nodeC);
|
|
64
|
+
}
|
|
65
|
+
cur = nodeC;
|
|
66
|
+
}
|
|
67
|
+
cur.isEnd = true;
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if the Trie contains a given word.
|
|
73
|
+
* @param {string} word - The word to check for.
|
|
74
|
+
* @returns {boolean} True if the word is present in the Trie.
|
|
75
|
+
*/
|
|
76
|
+
has(word: string): boolean {
|
|
77
|
+
word = this._caseProcess(word);
|
|
78
|
+
let cur = this.root;
|
|
79
|
+
for (const c of word) {
|
|
80
|
+
const nodeC = cur.children.get(c);
|
|
81
|
+
if (!nodeC) return false;
|
|
82
|
+
cur = nodeC;
|
|
83
|
+
}
|
|
84
|
+
return cur.isEnd;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Remove a word from the Trie structure.
|
|
89
|
+
* @param{string} word - The word to delete.
|
|
90
|
+
* @returns {boolean} True if the word was successfully removed.
|
|
91
|
+
*/
|
|
92
|
+
delete(word: string) {
|
|
93
|
+
word = this._caseProcess(word);
|
|
94
|
+
let isDeleted = false;
|
|
95
|
+
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
96
|
+
const char = word[i];
|
|
97
|
+
const child = cur.children.get(char);
|
|
98
|
+
if (child) {
|
|
99
|
+
if (i === word.length - 1) {
|
|
100
|
+
if (child.isEnd) {
|
|
101
|
+
if (child.children.size > 0) {
|
|
102
|
+
child.isEnd = false;
|
|
103
|
+
} else {
|
|
104
|
+
cur.children.delete(char);
|
|
105
|
+
}
|
|
106
|
+
isDeleted = true;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const res = dfs(child, i + 1);
|
|
112
|
+
if (res && !cur.isEnd && child.children.size === 0) {
|
|
113
|
+
cur.children.delete(char);
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
dfs(this.root, 0);
|
|
122
|
+
return isDeleted;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
getHeight() {
|
|
126
|
+
const beginRoot = this.root;
|
|
127
|
+
let maxDepth = 0;
|
|
128
|
+
if (beginRoot) {
|
|
129
|
+
const bfs = (node: TrieNode, level: number) => {
|
|
130
|
+
if (level > maxDepth) {
|
|
131
|
+
maxDepth = level;
|
|
132
|
+
}
|
|
133
|
+
const {children} = node;
|
|
134
|
+
if (children) {
|
|
135
|
+
for (const child of children.entries()) {
|
|
136
|
+
bfs(child[1], level + 1);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
bfs(beginRoot, 0);
|
|
141
|
+
}
|
|
142
|
+
return maxDepth;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// --- start additional methods ---
|
|
146
|
+
/**
|
|
147
|
+
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
|
|
148
|
+
* @param {string} input - The input string to check.
|
|
149
|
+
* @returns {boolean} True if it's an absolute prefix in the Trie.
|
|
150
|
+
*/
|
|
151
|
+
hasPurePrefix(input: string): boolean {
|
|
152
|
+
input = this._caseProcess(input);
|
|
153
|
+
let cur = this.root;
|
|
154
|
+
for (const c of input) {
|
|
155
|
+
const nodeC = cur.children.get(c);
|
|
156
|
+
if (!nodeC) return false;
|
|
157
|
+
cur = nodeC;
|
|
158
|
+
}
|
|
159
|
+
return !cur.isEnd;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
|
|
164
|
+
* @param {string} input - The input string representing the prefix to check.
|
|
165
|
+
* @returns {boolean} True if it's a prefix in the Trie.
|
|
166
|
+
*/
|
|
167
|
+
hasPrefix(input: string): boolean {
|
|
168
|
+
input = this._caseProcess(input);
|
|
169
|
+
let cur = this.root;
|
|
170
|
+
for (const c of input) {
|
|
171
|
+
const nodeC = cur.children.get(c);
|
|
172
|
+
if (!nodeC) return false;
|
|
173
|
+
cur = nodeC;
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
|
|
180
|
+
* @param {string} input - The input string representing the common prefix to check for.
|
|
181
|
+
* @returns {boolean} True if it's a common prefix in the Trie.
|
|
182
|
+
*/
|
|
183
|
+
hasCommonPrefix(input: string): boolean {
|
|
184
|
+
input = this._caseProcess(input);
|
|
185
|
+
let commonPre = '';
|
|
186
|
+
const dfs = (cur: TrieNode) => {
|
|
187
|
+
commonPre += cur.key;
|
|
188
|
+
if (commonPre === input) return;
|
|
189
|
+
if (cur.isEnd) return;
|
|
190
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
191
|
+
else return;
|
|
192
|
+
};
|
|
193
|
+
dfs(this.root);
|
|
194
|
+
return commonPre === input;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get the longest common prefix among all the words stored in the Trie.
|
|
199
|
+
* @returns {string} The longest common prefix found in the Trie.
|
|
200
|
+
*/
|
|
201
|
+
getLongestCommonPrefix(): string {
|
|
202
|
+
let commonPre = '';
|
|
203
|
+
const dfs = (cur: TrieNode) => {
|
|
204
|
+
commonPre += cur.key;
|
|
205
|
+
if (cur.isEnd) return;
|
|
206
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
207
|
+
else return;
|
|
208
|
+
};
|
|
209
|
+
dfs(this.root);
|
|
210
|
+
return commonPre;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
215
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
216
|
+
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
217
|
+
* @param {number} max - The max count of words will be found
|
|
218
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
219
|
+
* @returns {string[]} an array of strings.
|
|
220
|
+
*/
|
|
221
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
222
|
+
prefix = this._caseProcess(prefix);
|
|
223
|
+
const words: string[] = [];
|
|
224
|
+
let found = 0;
|
|
225
|
+
|
|
226
|
+
function dfs(node: TrieNode, word: string) {
|
|
227
|
+
for (const char of node.children.keys()) {
|
|
228
|
+
const charNode = node.children.get(char);
|
|
229
|
+
if (charNode !== undefined) {
|
|
230
|
+
dfs(charNode, word.concat(char));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (node.isEnd) {
|
|
234
|
+
if (found > max - 1) return;
|
|
235
|
+
words.push(word);
|
|
236
|
+
found++;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let startNode = this.root;
|
|
241
|
+
|
|
242
|
+
if (prefix) {
|
|
243
|
+
for (const c of prefix) {
|
|
244
|
+
const nodeC = startNode.children.get(c);
|
|
245
|
+
if (nodeC) startNode = nodeC;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
250
|
+
|
|
251
|
+
return words;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
protected _caseProcess(str: string) {
|
|
255
|
+
if (!this._caseSensitive) {
|
|
256
|
+
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
257
|
+
}
|
|
258
|
+
return str;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// --- end additional methods ---
|
|
262
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,4 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
export { MinHeap
|
|
8
|
+
// export { MinHeap } from 'data-structure-typed';
|
|
9
|
+
export * from './data-structures/heap/min-heap';
|
|
10
|
+
export * from './types/data-structures/heap/min-heap';
|
|
11
|
+
export * from './types/helpers';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
|
|
3
|
+
|
|
4
|
+
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
|
+
createNode(key: BTNKey, value?: N['value']): N;
|
|
6
|
+
|
|
7
|
+
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
8
|
+
|
|
9
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import {AVLTreeNode} from '../../../data-structures';
|
|
2
|
+
import {BSTOptions} from './bst';
|
|
3
|
+
|
|
4
|
+
export type AVLTreeNodeNested<T> = AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
5
|
+
export type AVLTreeOptions = BSTOptions & {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|