data-structure-typed 2.5.0 → 2.5.2
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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
---
|
|
2
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
3
|
+
sidebar_label: "CONCEPTS"
|
|
4
|
+
description: "Core concepts behind data-structure-typed: uniform API, iterators, generics, comparators, and the 5 design traits."
|
|
5
|
+
---
|
|
6
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
7
|
+
|
|
8
|
+
# CONCEPTS
|
|
9
|
+
|
|
10
|
+
This guide explains the foundational concepts behind data structures through plain language and practical understanding.
|
|
11
|
+
|
|
12
|
+
**👈 [Back to README](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Guides](/guide/guides.md)**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
16
|
+
|
|
17
|
+
## Table of Contents
|
|
18
|
+
|
|
19
|
+
1. [The Big Three Concepts](#the-big-three-concepts)
|
|
20
|
+
2. [Plain Language Explanations](#-plain-language-explanations)
|
|
21
|
+
3. [Iterator Protocol Design](#iterator-protocol-design)
|
|
22
|
+
4. [Seamless Interoperability](#-seamless-interoperability-iterator-protocol-everywhere)
|
|
23
|
+
5. [All Array Methods Work Everywhere](#-all-array-methods-work-everywhere)
|
|
24
|
+
6. [Why Not Just Use Native JavaScript?](#why-not-just-use-native-javascript)
|
|
25
|
+
7. [Decision Guide](#-decision-guide-choose-the-right-data-structure)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
29
|
+
|
|
30
|
+
## The Big Three Concepts
|
|
31
|
+
|
|
32
|
+
### 1. **Binary Search Tree (BST)** — O(log n) search/insert/delete
|
|
33
|
+
|
|
34
|
+
Maintains sorted order by keeping all left children smaller and right children larger than each node.
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
// Property: For any node
|
|
38
|
+
// All left subtree values < node value
|
|
39
|
+
// All right subtree values > node value
|
|
40
|
+
|
|
41
|
+
// 5
|
|
42
|
+
// / \
|
|
43
|
+
// 3 8
|
|
44
|
+
// / \ \
|
|
45
|
+
// 1 4 9
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**Advantage**: Fast operations without pre-sorting
|
|
49
|
+
**Trade-off**: Unbalanced trees degrade to O(n)
|
|
50
|
+
|
|
51
|
+
### 2. **Balanced Trees (AVL, Red-Black)** — Auto-rebalancing
|
|
52
|
+
|
|
53
|
+
Automatically reorganize themselves to maintain O(log n) guarantees even after insertions/deletions.
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
// Red-Black Tree: Color rules ensure balance
|
|
57
|
+
// AVL Tree: Height difference ≤ 1
|
|
58
|
+
|
|
59
|
+
// Both: Insert = O(log n), Delete = O(log n), Search = O(log n) always
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Advantage**: Guaranteed O(log n) performance
|
|
63
|
+
**Cost**: Rebalancing overhead on every modification
|
|
64
|
+
|
|
65
|
+
### 3. **Heap** — Parent-child priority relationships
|
|
66
|
+
|
|
67
|
+
A complete binary tree where parent always has priority over children.
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
// Max Heap: // Min Heap:
|
|
71
|
+
// 9 1
|
|
72
|
+
// / \ / \
|
|
73
|
+
// 7 8 2 3
|
|
74
|
+
// / \ / \
|
|
75
|
+
// 3 2 8 9
|
|
76
|
+
|
|
77
|
+
// Parent = 1.5x better than children
|
|
78
|
+
// Root always has best priority
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Advantage**: Very fast to get highest/lowest priority
|
|
82
|
+
**Perfect for**: Priority queues, heap sort
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
86
|
+
|
|
87
|
+
## 🌍 Plain Language Explanations
|
|
88
|
+
|
|
89
|
+
For those who love understanding concepts through metaphors:
|
|
90
|
+
|
|
91
|
+
| Data Structure | Plain Language Definition | Example |
|
|
92
|
+
|------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|
|
|
93
|
+
| **Linked List** | A line of bunnies, where each bunny holds the tail of the bunny in front of it. You want to find a bunny named Pablo, and you have to start searching from the first bunny. If it's not Pablo, you continue following that bunny's tail to the next one. So, you might need to search n times to find Pablo (O(n) time complexity). If you want to insert a bunny named Remi between Pablo and Vicky, it's very simple. You just need to let Vicky release Pablo's tail, let Remi hold Pablo's tail, and then let Vicky hold Remi's tail (O(1) time complexity). | To find bunny "Pablo", start from the first bunny and follow tails until found |
|
|
94
|
+
| **Array** | A line of numbered bunnies. If you want to find the bunny named Pablo, you can directly shout out Pablo's number 0680 (finding the element directly through array indexing, O(1) time complexity). However, if you don't know Pablo's number, you still need to search one by one (O(n) time complexity). Moreover, if you want to add a bunny named Vicky behind Pablo, you will need to renumber all the bunnies after Vicky (O(n) time complexity). | Finding element by index is instant, but inserting in the middle is slow |
|
|
95
|
+
| **Queue** | A line of numbered bunnies with a sticky note on the first bunny. For this line with a sticky note on the first bunny, whenever we want to remove a bunny from the front of the line, we only need to move the sticky note to the face of the next bunny without actually removing the bunny to avoid renumbering all the bunnies behind (removing from the front is also O(1) time complexity). For the tail of the line, we don't need to worry because each new bunny added to the tail is directly given a new number (O(1) time complexity) without needing to renumber all the previous bunnies. | Process items in FIFO order, efficiently from both ends |
|
|
96
|
+
| **Deque** | A line of grouped, numbered bunnies with a sticky note on the first bunny. For this line, we manage it by groups. Each time we remove a bunny from the front of the line, we only move the sticky note to the next bunny. This way, we don't need to renumber all the bunnies behind the first bunny each time a bunny is removed. Only when all members of a group are removed do we reassign numbers and regroup. The tail is handled similarly. This is a strategy of delaying and batching operations to offset the drawbacks of the Array data structure that requires moving all elements behind when inserting or deleting elements in the middle. | Efficient removal/insertion from both ends with batching optimization |
|
|
97
|
+
| **Stack** | A line of bunnies in a dead-end tunnel, where bunnies can only be removed from the tunnel entrance (end), and new bunnies can only be added at the entrance (end) as well. | Process items in LIFO order; undo/redo functionality |
|
|
98
|
+
| **Binary Tree** | A tree where each node has at most two children. | Hierarchical data organization |
|
|
99
|
+
| **Binary Search Tree** | A tree where all nodes in the left subtree are less than the node, and all nodes in the right subtree are greater than the node. Maintaining O(log n) for all operations. | Efficient search/insert/delete without re-sorting |
|
|
100
|
+
| **Red-Black Tree** | A self-balancing BST that automatically maintains balance through color-coding rules. | Used in Java TreeMap and maintains O(log n) guarantees |
|
|
101
|
+
| **AVL Tree** | A stricter self-balancing BST with stricter balance requirements than Red-Black trees. | Maximum search speed with slower insertions/deletions |
|
|
102
|
+
| **Heap** | A special binary tree stored in an array where parent always maintains priority relationship to children. | Efficient priority queue; heap sort |
|
|
103
|
+
| **Trie** | A tree of characters used for prefix-based searching. | Autocomplete, spell checking |
|
|
104
|
+
| **Graph** | A network of vertices (nodes) connected by edges. | Model relationships, networks |
|
|
105
|
+
| **SkipList** | A linked list with extra "express lanes" — higher levels skip over many nodes, giving probabilistic O(log n) like a balanced BST without rotations. | Sorted key-value store; simpler than Red-Black Tree, same average performance |
|
|
106
|
+
| **SegmentTree** | A binary tree where each node stores the aggregate (sum/min/max) of a range. Queries work by combining only the nodes that cover the target range. | Range sum/min/max queries with point updates; e.g. profit over date range |
|
|
107
|
+
| **BinaryIndexedTree** | A compact array where each cell stores partial sums using bit manipulation tricks. Much simpler than SegmentTree but only supports prefix sums. | Prefix sums, frequency counting, inversion counting |
|
|
108
|
+
| **Matrix** | A 2D grid of numbers supporting standard linear algebra operations. | 2D grid transformations, linear algebra |
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
112
|
+
|
|
113
|
+
## Iterator Protocol Design
|
|
114
|
+
|
|
115
|
+
### The Hidden Superpower
|
|
116
|
+
|
|
117
|
+
Every single data structure in this library implements the **Iterator protocol**:
|
|
118
|
+
|
|
119
|
+
- ✅ Spread operator: `[...tree]`
|
|
120
|
+
- ✅ for...of loops: `for (const item of tree)`
|
|
121
|
+
- ✅ Destructuring: `const [a, b, c] = tree`
|
|
122
|
+
- ✅ Array.from(): `Array.from(tree)`
|
|
123
|
+
- ✅ Set/Map constructors: `new Set(tree)`
|
|
124
|
+
|
|
125
|
+
### Iterator Support Comparison
|
|
126
|
+
|
|
127
|
+
| Feature | Array | Map | Set | Other Lib | data-structure-typed |
|
|
128
|
+
|----------------------|-------|------|-----|-----------|----------------------|
|
|
129
|
+
| Spread operator | ✅ | ❌/⚠️ | ✅ | ❌/⚠️ | ✅ |
|
|
130
|
+
| for...of loop | ✅ | ✅ | ✅ | ❌/⚠️ | ✅ |
|
|
131
|
+
| Destructuring | ✅ | ❌ | ❌ | ❌ | ✅ |
|
|
132
|
+
| Array.from() | ✅ | ❌/⚠️ | ❌ | ❌/⚠️ | ✅ |
|
|
133
|
+
| Set constructor | ✅ | ❌ | ✅ | ❌ | ✅ |
|
|
134
|
+
| **Full Integration** | ✅ | ⚠️ | ⚠️ | ⚠️ | **✅** |
|
|
135
|
+
|
|
136
|
+
### Live Examples: Zero Friction Conversions
|
|
137
|
+
|
|
138
|
+
#### Example 1: Array to Tree to Array
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
const array = [64, 34, 25, 12, 22, 11, 90];
|
|
142
|
+
const rbTree = new RedBlackTree(array);
|
|
143
|
+
const sorted = [...rbTree.keys()];
|
|
144
|
+
console.log(sorted); // [11, 12, 22, 25, 34, 64, 90] ✅
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### Example 2: Extract Keys and Values
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
const rbTree = new RedBlackTree([
|
|
151
|
+
[1, 'Alice'],
|
|
152
|
+
[2, 'Bob'],
|
|
153
|
+
[3, 'Charlie']
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
const allKeys = [...rbTree.keys()]; // [1, 2, 3]
|
|
157
|
+
const allValues = [...rbTree.values()]; // ['Alice', 'Bob', 'Charlie']
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Example 3: for...of on Any Structure
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
const tree = new RedBlackTree(entries);
|
|
164
|
+
const deque = new Deque(items);
|
|
165
|
+
const heap = new MaxHeap(items);
|
|
166
|
+
|
|
167
|
+
for (const entry of tree) console.log(entry);
|
|
168
|
+
for (const item of deque) console.log(item);
|
|
169
|
+
for (const item of heap) console.log(item);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
174
|
+
|
|
175
|
+
## 🔗 Seamless Interoperability: Iterator Protocol Everywhere
|
|
176
|
+
|
|
177
|
+
### The Design Philosophy
|
|
178
|
+
|
|
179
|
+
Instead of forcing conversions between data structures, we made every structure speak the same language as JavaScript's native iterables. This means:
|
|
180
|
+
|
|
181
|
+
- You can pass any data structure to `Array.from()`
|
|
182
|
+
- You can destructure any data structure
|
|
183
|
+
- You can spread any data structure
|
|
184
|
+
- You can loop over any data structure with `for...of`
|
|
185
|
+
|
|
186
|
+
This is **zero friction** because you use the same mental model.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
190
|
+
|
|
191
|
+
## 🎁 All Array Methods Work Everywhere
|
|
192
|
+
|
|
193
|
+
### The Biggest Developer Joy: Array Methods, Everywhere
|
|
194
|
+
|
|
195
|
+
You know these methods. You use them every day. They work on **every data structure**:
|
|
196
|
+
|
|
197
|
+
#### Chain on Tree
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
const rbTree = new RedBlackTree([
|
|
201
|
+
[1, { name: 'Alice', age: 25 }],
|
|
202
|
+
[2, { name: 'Bob', age: 30 }],
|
|
203
|
+
[3, { name: 'Charlie', age: 28 }],
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
const result = rbTree
|
|
207
|
+
.filter((value, _key) => (value?.age ?? 0) > 26)
|
|
208
|
+
.map((value, key) => [key, { ...value, id: key }])
|
|
209
|
+
.reduce((sum, value) => sum + (value?.age ?? 0), 0);
|
|
210
|
+
|
|
211
|
+
console.log(result); // 58 ✅
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
#### Chain on Heap
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
const minHeap = new Heap(
|
|
218
|
+
[
|
|
219
|
+
{ priority: 5, task: 'Email' },
|
|
220
|
+
{ priority: 3, task: 'Chat' },
|
|
221
|
+
{ priority: 8, task: 'Alert' },
|
|
222
|
+
],
|
|
223
|
+
{ comparator: (a, b) => a.priority - b.priority }
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
const urgent = minHeap
|
|
227
|
+
.filter((value, _key) => value.priority > 4)
|
|
228
|
+
.map((value, _key) => value.task, {
|
|
229
|
+
comparator: (a, b) => a.localeCompare(b),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
urgent.print(); // ['Alert', 'Email'] ✅
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### Chain on Deque
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const deque = new Deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
239
|
+
|
|
240
|
+
const stats = {
|
|
241
|
+
even: deque.filter((item) => item % 2 === 0).toArray(),
|
|
242
|
+
squared: deque.map((item) => item * item).toArray(),
|
|
243
|
+
hasLarge: deque.some((item) => item > 8),
|
|
244
|
+
sum: deque.reduce((acc, item) => acc + item, 0),
|
|
245
|
+
};
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Supported Methods Across All Structures
|
|
249
|
+
|
|
250
|
+
| Method | BinaryTrees | Heap | Deque | Graph | LinkedList |
|
|
251
|
+
|-------------|-------------|------|-------|-------|------------|
|
|
252
|
+
| map | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
253
|
+
| filter | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
254
|
+
| reduce | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
255
|
+
| find | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
256
|
+
| some/every | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
257
|
+
| keys/values | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
258
|
+
| forEach | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
262
|
+
|
|
263
|
+
## Why Not Just Use Native JavaScript?
|
|
264
|
+
|
|
265
|
+
### Case 1: Map Doesn't Maintain Sorted Order
|
|
266
|
+
|
|
267
|
+
❌ Map iteration is insertion order, not key order:
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
const map = new Map([[5, 'E'], [2, 'B'], [8, 'H'], [1, 'A']]);
|
|
271
|
+
for (const [key, value] of map) {
|
|
272
|
+
console.log(key); // 5, 2, 8, 1 (insertion order)
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
✅ RedBlackTree maintains sorted order automatically:
|
|
277
|
+
|
|
278
|
+
```javascript
|
|
279
|
+
const tree = new RedBlackTree([[5, 'E'], [2, 'B'], [8, 'H'], [1, 'A']]);
|
|
280
|
+
for (const [key, value] of tree) {
|
|
281
|
+
console.log(key); // 1, 2, 5, 8 (key order) ✅
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Case 2: Array.shift is Too Slow
|
|
286
|
+
|
|
287
|
+
❌ Array.shift is O(n):
|
|
288
|
+
|
|
289
|
+
```javascript
|
|
290
|
+
const queue = [];
|
|
291
|
+
for (let i = 0; i < 100000; i++) queue.push(i);
|
|
292
|
+
for (let i = 0; i < 100000; i++) queue.shift();
|
|
293
|
+
// Time: 2829ms ❌
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
✅ Deque.shift is O(1):
|
|
297
|
+
|
|
298
|
+
```javascript
|
|
299
|
+
const deque = new Deque();
|
|
300
|
+
for (let i = 0; i < 100000; i++) deque.push(i);
|
|
301
|
+
for (let i = 0; i < 100000; i++) deque.shift();
|
|
302
|
+
// Time: 5.83ms ✅
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Case 3: Maintaining Priority is Manual
|
|
306
|
+
|
|
307
|
+
❌ Array requires re-sorting O(n log n):
|
|
308
|
+
|
|
309
|
+
```javascript
|
|
310
|
+
const tasks = [];
|
|
311
|
+
|
|
312
|
+
function addTask(task) {
|
|
313
|
+
tasks.push(task);
|
|
314
|
+
tasks.sort((a, b) => b.priority - a.priority);
|
|
315
|
+
} // O(n² log n)
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
✅ PriorityQueue maintains priority O(log n):
|
|
319
|
+
|
|
320
|
+
```javascript
|
|
321
|
+
const pq = new MaxPriorityQueue();
|
|
322
|
+
|
|
323
|
+
function addTask(task) {
|
|
324
|
+
pq.add(task); // O(log n)
|
|
325
|
+
} // O(n log n)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Case 4: Range Queries are Tedious
|
|
329
|
+
|
|
330
|
+
❌ Array.filter is O(n):
|
|
331
|
+
|
|
332
|
+
```javascript
|
|
333
|
+
const prices = [10, 45, 23, 67, 89, 12, 54, 33, 78];
|
|
334
|
+
const inRange = prices.filter(p => p >= 30 && p <= 70);
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
✅ RedBlackTree range queries are O(log n + k):
|
|
338
|
+
|
|
339
|
+
```javascript
|
|
340
|
+
const tree = new RedBlackTree(prices);
|
|
341
|
+
const inRange = tree.rangeSearch([30, 70]);
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Case 5: Finding the K-th Element Requires Sorting
|
|
345
|
+
|
|
346
|
+
❌ Array: sort + index is O(n log n):
|
|
347
|
+
|
|
348
|
+
```javascript
|
|
349
|
+
const scores = [85, 92, 78, 95, 88, 100, 73];
|
|
350
|
+
scores.sort((a, b) => a - b);
|
|
351
|
+
const median = scores[Math.floor(scores.length / 2)]; // re-sort on every update
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
✅ Order-statistic tree: O(log n) getByRank/getRank with live updates:
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
const tree = new RedBlackTree(scores, { enableOrderStatistic: true });
|
|
358
|
+
const median = tree.getByRank(Math.floor(tree.size / 2)); // O(log n)
|
|
359
|
+
const rank = tree.getRank(92); // "how many scores below 92?" — O(log n)
|
|
360
|
+
const top3 = tree.rangeByRank(tree.size - 3, tree.size - 1); // O(log n + 3)
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Case 6: Prefix Matching is Tedious
|
|
364
|
+
|
|
365
|
+
❌ Array.filter is O(n*m):
|
|
366
|
+
|
|
367
|
+
```javascript
|
|
368
|
+
const words = ['apple', 'app', 'apply', 'application'];
|
|
369
|
+
const matches = words.filter(w => w.startsWith('app'));
|
|
370
|
+
// For 1M words: checks 1M words ❌
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
✅ Trie prefix matching is O(m + k):
|
|
374
|
+
|
|
375
|
+
```javascript
|
|
376
|
+
const trie = new Trie(words);
|
|
377
|
+
const matches = trie.getWords('appl');
|
|
378
|
+
// O(5 + 4) = 9 operations ✅
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
383
|
+
|
|
384
|
+
## 🎯 Decision Guide: Choose the Right Data Structure
|
|
385
|
+
|
|
386
|
+
```
|
|
387
|
+
Need frequent head/tail operations?
|
|
388
|
+
↓
|
|
389
|
+
Yes → Deque (O(1) shift/unshift)
|
|
390
|
+
No → Continue
|
|
391
|
+
|
|
392
|
+
Need sorted + fast queries?
|
|
393
|
+
↓
|
|
394
|
+
Yes → RedBlackTree (O(log n) search)
|
|
395
|
+
No → Continue
|
|
396
|
+
|
|
397
|
+
Need priority handling?
|
|
398
|
+
↓
|
|
399
|
+
Yes → PriorityQueue (O(log n) add)
|
|
400
|
+
No → Continue
|
|
401
|
+
|
|
402
|
+
Need prefix matching?
|
|
403
|
+
↓
|
|
404
|
+
Yes → Trie (O(m + k) search)
|
|
405
|
+
No → Continue
|
|
406
|
+
|
|
407
|
+
Need graph algorithms?
|
|
408
|
+
↓
|
|
409
|
+
Yes → DirectedGraph / UndirectedGraph
|
|
410
|
+
No → Continue
|
|
411
|
+
|
|
412
|
+
Need range queries on an indexed sequence?
|
|
413
|
+
↓
|
|
414
|
+
Yes → SegmentTree (O(log n) query + update, supports sum/min/max/gcd/custom)
|
|
415
|
+
Only need prefix sums? → BinaryIndexedTree (simpler, less memory)
|
|
416
|
+
No → Continue
|
|
417
|
+
|
|
418
|
+
Need a sorted key-value map?
|
|
419
|
+
↓
|
|
420
|
+
Yes → TreeMap (guaranteed O(log n) via Red-Black Tree)
|
|
421
|
+
Want simpler implementation, same API? → SkipList (O(log n) average, probabilistic)
|
|
422
|
+
No → Use Array
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
---
|
|
426
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
427
|
+
|
|
428
|
+
## Next Steps
|
|
429
|
+
|
|
430
|
+
**Understand the basics?**
|
|
431
|
+
→ [See real-world examples](/guide/guides.md)
|
|
432
|
+
|
|
433
|
+
**Want to use immediately?**
|
|
434
|
+
→ [Full API Docs](https://data-structure-typed-docs.vercel.app/)
|
|
435
|
+
|
|
436
|
+
**Curious about performance?**
|
|
437
|
+
→ [Read performance comparison](/guide/performance.md)
|
|
438
|
+
|
|
439
|
+
**Want to know how it's implemented?**
|
|
440
|
+
→ [See architecture details](/guide/architecture.md)
|
|
441
|
+
|
|
442
|
+
---
|
|
443
|
+
keywords: [typescript data structures concepts, comparator, iterator protocol, generics, uniform API]
|
|
444
|
+
|
|
445
|
+
**Related:**
|
|
446
|
+
|
|
447
|
+
- [OVERVIEW.md](/guide/overview.md) - API / structures / methods
|
|
448
|
+
- [GUIDES.md](/guide/guides.md) - Leaderboard / LRU / Queue / real-world examples
|
|
449
|
+
- [ARCHITECTURE.md](/guide/architecture.md) - Design / JIT / internal abstractions
|
|
450
|
+
- [PERFORMANCE.md](/guide/performance.md) - Benchmarks / comparisons
|
|
451
|
+
- [INTEGRATIONS.md](/guide/integrations.md) - React / Nest / Express
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FAQ — Frequently Asked Questions
|
|
3
|
+
sidebar_label: "FAQ"description: Common questions about data-structure-typed — TreeMap in JavaScript, priority queues, rank queries, bundle size, and more.
|
|
4
|
+
keywords: [typescript data structures, treemap javascript, priority queue typescript, sorted set javascript, rank query, faq]
|
|
5
|
+
sidebar_position: 7
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# FAQ
|
|
9
|
+
|
|
10
|
+
## Does JavaScript have a TreeMap or TreeSet?
|
|
11
|
+
|
|
12
|
+
Not natively. JavaScript's `Map` and `Set` are hash-based and unordered. This library provides `TreeMap` and `TreeSet` backed by Red-Black Trees — offering sorted iteration, `floor`/`ceiling`/`higher`/`lower` lookups, and `getRank`/`getByRank`/`rangeByRank` queries.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { TreeMap } from 'data-structure-typed';
|
|
16
|
+
|
|
17
|
+
const map = new TreeMap<number, string>();
|
|
18
|
+
map.set(3, 'c');
|
|
19
|
+
map.set(1, 'a');
|
|
20
|
+
map.set(2, 'b');
|
|
21
|
+
|
|
22
|
+
// Sorted iteration (by key)
|
|
23
|
+
for (const [key, value] of map) {
|
|
24
|
+
console.log(key, value); // 1 'a', 2 'b', 3 'c'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// NavigableMap operations
|
|
28
|
+
map.floor(2.5); // [2, 'b'] — largest key ≤ 2.5
|
|
29
|
+
map.ceiling(1.5); // [2, 'b'] — smallest key ≥ 1.5
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## When should I use a Heap instead of sorting an array?
|
|
33
|
+
|
|
34
|
+
When you need to repeatedly access the smallest or largest element. Sorting an array is O(n log n) every time you add an element. A Heap gives you O(log n) insert and O(1) access to the top element.
|
|
35
|
+
|
|
36
|
+
**Use Heap when:**
|
|
37
|
+
- Building a priority queue or task scheduler
|
|
38
|
+
- Finding top-k elements from a stream
|
|
39
|
+
- Implementing Dijkstra's algorithm
|
|
40
|
+
- Any scenario where you repeatedly need the min/max
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { MinHeap } from 'data-structure-typed';
|
|
44
|
+
|
|
45
|
+
const tasks = new MinHeap<number>([5, 1, 3, 7, 2]);
|
|
46
|
+
tasks.poll(); // 1 (O(log n), not O(n log n))
|
|
47
|
+
tasks.add(0);
|
|
48
|
+
tasks.peek(); // 0
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Does this library support rank and range queries?
|
|
52
|
+
|
|
53
|
+
Yes. Enable with `{ enableOrderStatistic: true }` on any tree-based structure:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { RedBlackTree } from 'data-structure-typed';
|
|
57
|
+
|
|
58
|
+
const tree = new RedBlackTree<number>([10, 20, 30, 40, 50], {
|
|
59
|
+
enableOrderStatistic: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
tree.getRank(30); // 2 — two elements precede 30 in tree order
|
|
63
|
+
tree.getByRank(0); // 10 — first element in tree order
|
|
64
|
+
tree.rangeByRank(1, 3); // [20, 30, 40] — positions 1 through 3
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Works with `TreeMap`, `TreeSet`, `TreeMultiMap`, and `TreeMultiSet` too.
|
|
68
|
+
|
|
69
|
+
## Is it faster than native arrays for ordered operations?
|
|
70
|
+
|
|
71
|
+
For ordered insert + lookup: **yes, significantly**.
|
|
72
|
+
|
|
73
|
+
| Operation | Sorted Array | Red-Black Tree |
|
|
74
|
+
|-----------|-------------|----------------|
|
|
75
|
+
| Insert (maintain order) | O(n) | O(log n) |
|
|
76
|
+
| Find by key | O(log n) | O(log n) |
|
|
77
|
+
| Find min/max | O(1) | O(log n) |
|
|
78
|
+
| Delete by key | O(n) | O(log n) |
|
|
79
|
+
| Get kth element | O(1) | O(log n) |
|
|
80
|
+
|
|
81
|
+
For 10,000+ elements, the O(n) insert cost of arrays becomes a bottleneck. Trees maintain O(log n) regardless of size.
|
|
82
|
+
|
|
83
|
+
See [PERFORMANCE.md](https://github.com/zrwusa/data-structure-typed/blob/main/docs/PERFORMANCE.md) for benchmark results.
|
|
84
|
+
|
|
85
|
+
## Can I use this in React / Node.js / browser?
|
|
86
|
+
|
|
87
|
+
Yes. The library ships ESM, CJS, and UMD builds. It works in:
|
|
88
|
+
|
|
89
|
+
- **Node.js** (any version supporting ES2015+)
|
|
90
|
+
- **Browsers** (via bundler or UMD script tag)
|
|
91
|
+
- **React / Next.js / Vue / Angular** (import normally)
|
|
92
|
+
- **Deno / Bun** (ESM compatible)
|
|
93
|
+
|
|
94
|
+
Zero dependencies means no compatibility concerns.
|
|
95
|
+
|
|
96
|
+
## What data structures are included?
|
|
97
|
+
|
|
98
|
+
| Category | Structures |
|
|
99
|
+
|----------|-----------|
|
|
100
|
+
| Trees | RedBlackTree, AVLTree, BST, TreeMap, TreeSet, TreeMultiMap, TreeMultiSet |
|
|
101
|
+
| Heaps | Heap, MinHeap, MaxHeap, MinPriorityQueue, MaxPriorityQueue |
|
|
102
|
+
| Queues | Queue, Deque |
|
|
103
|
+
| Lists | SinglyLinkedList, DoublyLinkedList, SkipList |
|
|
104
|
+
| Hashing | HashMap |
|
|
105
|
+
| Graphs | DirectedGraph, UndirectedGraph |
|
|
106
|
+
| Strings | Trie |
|
|
107
|
+
| Arrays | SegmentTree, BinaryIndexedTree (Fenwick Tree), Matrix |
|
|
108
|
+
| Basic | Stack |
|
|
109
|
+
|
|
110
|
+
## Is this library production-ready?
|
|
111
|
+
|
|
112
|
+
Yes.
|
|
113
|
+
|
|
114
|
+
- **2600+ tests**, 99%+ code coverage
|
|
115
|
+
- **Zero dependencies**
|
|
116
|
+
- **Type-safe** — full TypeScript generics
|
|
117
|
+
- **Actively maintained** — regular releases
|
|
118
|
+
- Every release passes typecheck, lint, and full test suite via CI
|
|
119
|
+
|
|
120
|
+
## How does this compare to js-sdsl?
|
|
121
|
+
|
|
122
|
+
| Feature | data-structure-typed | js-sdsl |
|
|
123
|
+
|---------|---------------------|---------|
|
|
124
|
+
| Data structures | 20+ | ~6 |
|
|
125
|
+
| API style | Unified Array-like | Mixed |
|
|
126
|
+
| Order-statistic (getRank/getByRank) | ✅ | ❌ |
|
|
127
|
+
| Tree-shaking subpaths | ✅ | ❌ |
|
|
128
|
+
| Maintenance | Active (2026) | Inactive |
|
|
129
|
+
| Bundle (full) | ~143KB min | ~45KB min |
|
|
130
|
+
|
|
131
|
+
`data-structure-typed` is broader and more actively maintained. js-sdsl is smaller if you only need a few structures.
|
|
132
|
+
|
|
133
|
+
## What is the bundle size?
|
|
134
|
+
|
|
135
|
+
| Import | Size (ESM) |
|
|
136
|
+
|--------|-----------|
|
|
137
|
+
| Full bundle | 598KB |
|
|
138
|
+
| `data-structure-typed/binary-tree` | 315KB |
|
|
139
|
+
| `data-structure-typed/graph` | 127KB |
|
|
140
|
+
| `data-structure-typed/linked-list` | 93KB |
|
|
141
|
+
| `data-structure-typed/queue` | 91KB |
|
|
142
|
+
| `data-structure-typed/heap` | 36KB |
|
|
143
|
+
| `data-structure-typed/priority-queue` | 30KB |
|
|
144
|
+
| `data-structure-typed/hash` | 29KB |
|
|
145
|
+
| `data-structure-typed/matrix` | 28KB |
|
|
146
|
+
| `data-structure-typed/trie` | 27KB |
|
|
147
|
+
| `data-structure-typed/stack` | 18KB |
|
|
148
|
+
|
|
149
|
+
UMD bundle: ~143KB minified. `sideEffects: false` enables full tree-shaking with modern bundlers.
|
|
150
|
+
|
|
151
|
+
## How do I build a leaderboard with this library?
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { TreeMap } from 'data-structure-typed';
|
|
155
|
+
|
|
156
|
+
const leaderboard = new TreeMap<number, string>(
|
|
157
|
+
[[100, 'Alice'], [250, 'Bob'], [180, 'Charlie']],
|
|
158
|
+
{ comparator: (a, b) => b - a, enableOrderStatistic: true }
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// Top 3 players (descending score order)
|
|
162
|
+
leaderboard.rangeByRank(0, 2);
|
|
163
|
+
// → [[250, 'Bob'], [180, 'Charlie'], [100, 'Alice']]
|
|
164
|
+
|
|
165
|
+
// What rank is score 180?
|
|
166
|
+
leaderboard.getRank(180); // 1 (0-indexed, second position)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## How do I build autocomplete with a Trie?
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { Trie } from 'data-structure-typed';
|
|
173
|
+
|
|
174
|
+
const trie = new Trie(['apple', 'app', 'application', 'banana', 'band']);
|
|
175
|
+
|
|
176
|
+
trie.getWords('app'); // ['app', 'apple', 'application']
|
|
177
|
+
trie.getWords('ban'); // ['banana', 'band']
|
|
178
|
+
trie.hasPrefix('app'); // true
|
|
179
|
+
trie.has('apple'); // true
|
|
180
|
+
```
|