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,645 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_label: "OVERVIEW"
|
|
3
|
+
description: "Complete overview of all 20+ data structures: trees, heaps, graphs, queues, linked lists, hash maps, and more."
|
|
4
|
+
title: "Data Structures Overview — Trees, Heaps, Graphs, Queues"
|
|
5
|
+
keywords: [typescript data structures overview, red black tree, heap, priority queue, trie, graph, deque, treemap, treeset]
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# OVERVIEW
|
|
9
|
+
|
|
10
|
+
A quick-reference guide to all structures, common operations, and usage patterns. For complete API details with method signatures and examples, see the **[Full API Docs](https://data-structure-typed-docs.vercel.app/)**.
|
|
11
|
+
|
|
12
|
+
**[Back to README](/.md) • [API Docs](https://data-structure-typed-docs.vercel.app/) • [Real-World Examples](/guide/guides.md) • [Performance](/guide/performance.md)**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Table of Contents
|
|
17
|
+
|
|
18
|
+
1. [Quick Reference Table](#quick-reference-table)
|
|
19
|
+
2. [All Data Structures](#all-data-structures)
|
|
20
|
+
3. [CRUD Operations](#crud-operations)
|
|
21
|
+
4. [Common Methods](#common-methods)
|
|
22
|
+
5. [TypeScript Support](#typescript-support)
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Quick Reference Table
|
|
27
|
+
|
|
28
|
+
| Data Structure | Best Use Case | Time Complexity | Space |
|
|
29
|
+
|-------------------|---------------------------|--------------------------|--------|
|
|
30
|
+
| **Array** | Direct indexed access | O(n) insert/delete | O(n) |
|
|
31
|
+
| **Linked List** | Dynamic size, fast insert | O(n) search, O(1) insert | O(n) |
|
|
32
|
+
| **Stack** | Undo/redo, DFS | O(1) all | O(n) |
|
|
33
|
+
| **Queue** | FIFO processing | O(1) all | O(n) |
|
|
34
|
+
| **Deque** | Head/tail operations | O(1) all | O(n) |
|
|
35
|
+
| **Binary Tree** | Hierarchical data | O(n) avg | O(n) |
|
|
36
|
+
| **BST** | Sorted search | O(log n) avg | O(n) |
|
|
37
|
+
| **RedBlackTree** | Guaranteed sorted | O(log n) guaranteed | O(n) |
|
|
38
|
+
| **AVL Tree** | Balanced sorted | O(log n) guaranteed | O(n) |
|
|
39
|
+
| **Heap** | Priority queue | O(log n) add/remove | O(n) |
|
|
40
|
+
| **PriorityQueue** | Task scheduling | O(log n) add/poll | O(n) |
|
|
41
|
+
| **Trie** | Prefix search | O(m+k) search | O(26n) |
|
|
42
|
+
| **Graph** | Networks, paths | Varies | O(V+E) |
|
|
43
|
+
| **SkipList** | Sorted KV (probabilistic) | O(log n) avg all ops | O(n log n) |
|
|
44
|
+
| **SegmentTree** | Range queries (sum/min/max/custom) | O(log n) query/update | O(n) |
|
|
45
|
+
| **BinaryIndexedTree** | Prefix sums, frequency counting | O(log n) query/update | O(n) |
|
|
46
|
+
| **Matrix** | 2D grid arithmetic | O(n²) add, O(n³) multiply | O(n²) |
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## All Data Structures
|
|
51
|
+
|
|
52
|
+
### Stack Structure
|
|
53
|
+
|
|
54
|
+
#### Stack
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { Stack } from 'data-structure-typed';
|
|
58
|
+
|
|
59
|
+
const stack = new Stack<number>([1, 2]);
|
|
60
|
+
stack.push(3); // add to bottom
|
|
61
|
+
const top = stack.pop(); // Remove from top - O(1)
|
|
62
|
+
const peek = stack.peek(); // View top
|
|
63
|
+
stack.print(); // [1, 2]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Linear Structures
|
|
69
|
+
|
|
70
|
+
#### Queue
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { Queue } from 'data-structure-typed';
|
|
74
|
+
|
|
75
|
+
const queue = new Queue<number>([1, 2]);
|
|
76
|
+
queue.push(3); // add to back
|
|
77
|
+
const first = queue.shift(); // Remove from front - O(1)
|
|
78
|
+
const length = queue.length; // Current length
|
|
79
|
+
queue.print(); // [2, 3]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Deque
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { Deque } from 'data-structure-typed';
|
|
86
|
+
|
|
87
|
+
const deque = new Deque<number>([1, 2]);
|
|
88
|
+
deque.push(3); // Add to back
|
|
89
|
+
deque.unshift(0); // Add to front
|
|
90
|
+
deque.pop(); // Remove from back - O(1)
|
|
91
|
+
deque.shift(); // Remove from front - O(1)
|
|
92
|
+
deque.print(); // [1, 2]
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Linked Lists
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { SinglyLinkedList, DoublyLinkedList } from 'data-structure-typed';
|
|
99
|
+
|
|
100
|
+
const singly = new SinglyLinkedList<number>([1, 2]);
|
|
101
|
+
const doubly = new DoublyLinkedList<number>([1, 2]);
|
|
102
|
+
|
|
103
|
+
singly.push(3); // Add to end
|
|
104
|
+
singly.addAt(1, 99); // Insert at index - O(n)
|
|
105
|
+
singly.deleteAt(2); // Delete at index - O(n)
|
|
106
|
+
singly.print(); // [1, 99, 3]
|
|
107
|
+
|
|
108
|
+
doubly.push(3); // Add to end
|
|
109
|
+
doubly.addAt(1, 99); // Insert at index - O(n)
|
|
110
|
+
doubly.deleteAt(2); // Delete at index - O(n)
|
|
111
|
+
doubly.print(); // [1, 99, 3]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Tree Structures
|
|
115
|
+
|
|
116
|
+
#### Binary Search Tree (BST)
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { BST } from 'data-structure-typed';
|
|
120
|
+
|
|
121
|
+
const bst = new BST<number>([1, 3, 5, 8, 6, 2, 4, 7]);
|
|
122
|
+
bst.add(9); // Add elements
|
|
123
|
+
bst.has(5); // Check existence - O(log n) avg
|
|
124
|
+
bst.delete(1); // Remove - O(log n) avg
|
|
125
|
+
bst.print(); // Visual representation
|
|
126
|
+
// ___4___
|
|
127
|
+
// / \
|
|
128
|
+
// 2_ _6_
|
|
129
|
+
// \ / \
|
|
130
|
+
// 3 5 7_
|
|
131
|
+
// \
|
|
132
|
+
// 8_
|
|
133
|
+
// \
|
|
134
|
+
// 9
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Red-Black Tree
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { RedBlackTree } from 'data-structure-typed';
|
|
141
|
+
|
|
142
|
+
const rbTree = new RedBlackTree<number, string>([[1, 'Alice'], [2, 'Bob'], [3, 'Chris']]);
|
|
143
|
+
rbTree.set(4, 'Dan'); // Add key-value
|
|
144
|
+
rbTree.get(1); // 'Alice'
|
|
145
|
+
rbTree.delete(2); // Remove - O(log n) guaranteed
|
|
146
|
+
console.log([...rbTree.values()]); // ['Alice', 'Chris', 'Dan'] Automatically sorted
|
|
147
|
+
rbTree.print()
|
|
148
|
+
// _3_
|
|
149
|
+
// / \
|
|
150
|
+
// 1 4
|
|
151
|
+
|
|
152
|
+
// Order-Statistic mode — O(log n) rank queries
|
|
153
|
+
const ost = new RedBlackTree<number, string>([[1, 'A'], [2, 'B'], [3, 'C']], { enableOrderStatistic: true });
|
|
154
|
+
ost.getByRank(0); // 1 (smallest key)
|
|
155
|
+
ost.getRank(2); // 1 (one element before key 2)
|
|
156
|
+
ost.rangeByRank(0, 1); // [1, 2] (first two elements)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### AVL Tree
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import {AVLTree} from 'data-structure-typed';
|
|
163
|
+
|
|
164
|
+
const avl = new AVLTree<number>([5, 4, 3, 8, 1]);
|
|
165
|
+
avl.add(9);
|
|
166
|
+
avl.isAVLBalanced(); // Check balance
|
|
167
|
+
avl.delete(3); // Auto-rebalances
|
|
168
|
+
avl.print()
|
|
169
|
+
// ___5_
|
|
170
|
+
// / \
|
|
171
|
+
// 1_ 8_
|
|
172
|
+
// \ \
|
|
173
|
+
// 4 9
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Heap & Priority Queue
|
|
177
|
+
|
|
178
|
+
#### Heap
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { MinHeap, MaxHeap } from 'data-structure-typed';
|
|
182
|
+
|
|
183
|
+
const minHeap = new MinHeap<number>([5, 3, 8]);
|
|
184
|
+
minHeap.add(1); // Add element - O(log n)
|
|
185
|
+
const min = minHeap.poll(); // Get minimum - O(log n)
|
|
186
|
+
const peek = minHeap.peek(); // View minimum - O(1)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### Priority Queue
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { MaxPriorityQueue } from 'data-structure-typed';
|
|
193
|
+
|
|
194
|
+
const pq = new MaxPriorityQueue<Task>();
|
|
195
|
+
pq.add({ id: 1, priority: 5 }); // Add - O(log n)
|
|
196
|
+
const task = pq.poll(); // Remove highest - O(log n)
|
|
197
|
+
const size = pq.size; // Current size
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Special Structures
|
|
201
|
+
|
|
202
|
+
#### Trie (Prefix Tree)
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { Trie } from 'data-structure-typed';
|
|
206
|
+
|
|
207
|
+
const trie = new Trie(['apple', 'app', 'banana']);
|
|
208
|
+
trie.add('apply');
|
|
209
|
+
trie.getWords('app'); // ['apple', 'apply', 'app'] - O(m+k)
|
|
210
|
+
trie.has('apple'); // true
|
|
211
|
+
trie.hasPrefix('ap'); // true
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
#### Graph
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
import { DirectedGraph, UndirectedGraph } from 'data-structure-typed';
|
|
218
|
+
|
|
219
|
+
const graph = new DirectedGraph<string>();
|
|
220
|
+
graph.addVertex('A');
|
|
221
|
+
graph.addVertex('B');
|
|
222
|
+
graph.addEdge('A', 'B', 1); // Add edge with weight
|
|
223
|
+
graph.hasEdge('A', 'B'); // true
|
|
224
|
+
const {
|
|
225
|
+
distMap, distPaths, preMap, seen, paths, minDist, minPath
|
|
226
|
+
} = graph.dijkstra('A', 'B', true, true)!;
|
|
227
|
+
const order = graph.topologicalSort();
|
|
228
|
+
console.log(distMap)
|
|
229
|
+
console.log(distPaths)
|
|
230
|
+
console.log(preMap)
|
|
231
|
+
console.log(seen)
|
|
232
|
+
console.log(paths)
|
|
233
|
+
console.log(minDist)
|
|
234
|
+
console.log(minPath) // Shortest path
|
|
235
|
+
console.log(order) // DAG order
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## CRUD Operations
|
|
241
|
+
|
|
242
|
+
### Create (Add)
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
const tree = new RedBlackTree<number, string>();
|
|
246
|
+
tree.set(1, 'Alice');
|
|
247
|
+
tree.setMany([[2, 'Bob'], [3, 'Charlie']]);
|
|
248
|
+
|
|
249
|
+
// Heap
|
|
250
|
+
const heap = new MaxHeap<number>([10, 20]);
|
|
251
|
+
heap.add(15);
|
|
252
|
+
|
|
253
|
+
// Trie
|
|
254
|
+
const trie = new Trie(['hello']);
|
|
255
|
+
trie.add('world');
|
|
256
|
+
|
|
257
|
+
// Graph
|
|
258
|
+
const graph = new DirectedGraph<string>();
|
|
259
|
+
graph.addVertex('A');
|
|
260
|
+
graph.addEdge('A', 'B', 1);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Read (Query)
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
// Tree
|
|
267
|
+
tree.get(1); // 'Alice'
|
|
268
|
+
tree.has(1); // true
|
|
269
|
+
tree.size; // Number of elements
|
|
270
|
+
|
|
271
|
+
// Heap
|
|
272
|
+
heap.peek(); // Highest priority element
|
|
273
|
+
heap.size; // Current size
|
|
274
|
+
|
|
275
|
+
// Trie
|
|
276
|
+
trie.getWords('hello'); // true
|
|
277
|
+
trie.hasPrefix('hel'); // true
|
|
278
|
+
|
|
279
|
+
// Graph
|
|
280
|
+
graph.hasVertex('A'); // true
|
|
281
|
+
graph.hasEdge('A', 'B'); // true
|
|
282
|
+
graph.getNeighbors('A'); // Connected vertices
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Update (Modify)
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// Tree
|
|
289
|
+
tree.set(1, 'Alice Updated'); // Update value
|
|
290
|
+
tree.delete(1); // Remove
|
|
291
|
+
|
|
292
|
+
// Heap
|
|
293
|
+
heap.pop(); // Remove highest
|
|
294
|
+
|
|
295
|
+
// Graph
|
|
296
|
+
graph.deleteEdge('A', 'B'); // Remove edge
|
|
297
|
+
graph.deleteVertex('A'); // Remove vertex
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Delete
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
// All structures support:
|
|
304
|
+
structure.clear(); // Remove all elements
|
|
305
|
+
structure.delete(key); // Remove specific
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## Common Methods
|
|
311
|
+
|
|
312
|
+
### Available on All Structures
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
// Iteration
|
|
316
|
+
structure.forEach((value, key) => {
|
|
317
|
+
});
|
|
318
|
+
for (const item of structure) {
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Conversion
|
|
322
|
+
[...structure]; // Spread
|
|
323
|
+
Array.from(structure); // Array conversion
|
|
324
|
+
|
|
325
|
+
// Array methods
|
|
326
|
+
structure.map((v, k) => v * 2);
|
|
327
|
+
structure.filter((v, k) => v > 5);
|
|
328
|
+
structure.reduce((acc, v) => acc + v, 0);
|
|
329
|
+
structure.find((v, k) => v === 5);
|
|
330
|
+
structure.some((v, k) => v > 10);
|
|
331
|
+
structure.every((v, k) => v > 0);
|
|
332
|
+
|
|
333
|
+
// Properties
|
|
334
|
+
structure.size; // Element count
|
|
335
|
+
structure.isEmpty(); // Check empty
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Structure-Specific Methods
|
|
339
|
+
|
|
340
|
+
#### Trees
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
tree.height; // Tree height
|
|
344
|
+
tree.isAVLBalanced(); // Balance check
|
|
345
|
+
tree.getNode(key); // Get node object
|
|
346
|
+
tree.getHeight(key); // Node height
|
|
347
|
+
tree.getLeftMost(); // Leftmost node
|
|
348
|
+
tree.getRightMost(); // Rightmost node
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### Deque
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
deque.peekFirst(); // View front
|
|
355
|
+
deque.peekLast(); // View back
|
|
356
|
+
deque.pollFirst(); // Remove front - O(1)
|
|
357
|
+
deque.pollLast(); // Remove back - O(1)
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Graph
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
graph.topologicalSort(); // DAG order
|
|
364
|
+
graph.dijkstra(start, end); // Shortest path
|
|
365
|
+
graph.dfs(vertex); // Depth-first traversal
|
|
366
|
+
graph.bfs(vertex); // Breadth-first traversal
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## TypeScript Support
|
|
372
|
+
|
|
373
|
+
### Full Generic Support
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
// Custom type safety
|
|
377
|
+
const tree = new RedBlackTree<number, User>();
|
|
378
|
+
tree.set(1, { name: 'Alice', age: 30 });
|
|
379
|
+
|
|
380
|
+
const value = tree.get(1); // Type: User | undefined
|
|
381
|
+
|
|
382
|
+
// Automatic inference
|
|
383
|
+
const numbers = new Deque<number>([1, 2]);
|
|
384
|
+
numbers.push(3);
|
|
385
|
+
|
|
386
|
+
// Custom comparators
|
|
387
|
+
const descending = new RedBlackTree<number, string>([], {
|
|
388
|
+
comparator: (a, b) => b - a // Sort descending
|
|
389
|
+
});
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### Type Safety Examples
|
|
393
|
+
|
|
394
|
+
```typescript
|
|
395
|
+
interface Task {
|
|
396
|
+
id: string;
|
|
397
|
+
priority: number;
|
|
398
|
+
action: Promise<void>;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const pq = new MaxPriorityQueue<Task>(
|
|
402
|
+
{
|
|
403
|
+
comparator: (a, b) => a.priority - b.priority
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
pq.add({
|
|
408
|
+
id: '1', priority: 5, action: async () => {
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// Type checking catches errors
|
|
413
|
+
const task = pq.poll();
|
|
414
|
+
if (task) {
|
|
415
|
+
// task is guaranteed to be Task
|
|
416
|
+
await task.action;
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## Complexity Chart
|
|
423
|
+
|
|
424
|
+
| Operation | Array | LinkedList | BST | RBTree | Heap | Trie |
|
|
425
|
+
|-----------|-------|------------|----------|----------|----------|------|
|
|
426
|
+
| Access | O(1) | O(n) | O(log n) | O(log n) | O(n) | N/A |
|
|
427
|
+
| Search | O(n) | O(n) | O(log n) | O(log n) | O(n) | O(m) |
|
|
428
|
+
| Insert | O(n) | O(1) | O(log n) | O(log n) | O(log n) | O(m) |
|
|
429
|
+
| Delete | O(n) | O(1) | O(log n) | O(log n) | O(log n) | O(m) |
|
|
430
|
+
|
|
431
|
+
---
|
|
432
|
+
|
|
433
|
+
## Common Patterns
|
|
434
|
+
|
|
435
|
+
### Iterator Pattern
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
const tree = new RedBlackTree([5, 2, 8]);
|
|
439
|
+
|
|
440
|
+
// Works everywhere
|
|
441
|
+
const arr = [...tree.keys()]; // Spread
|
|
442
|
+
const set = new Set(tree.keys()); // Set constructor
|
|
443
|
+
for (const val of tree.values()) {
|
|
444
|
+
} // for...of
|
|
445
|
+
const [first, ...rest] = tree.keys(); // Destructuring
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### Filtering Pattern
|
|
449
|
+
|
|
450
|
+
```typescript
|
|
451
|
+
const tree = new RedBlackTree([
|
|
452
|
+
[1, { active: true }],
|
|
453
|
+
[2, { active: false }],
|
|
454
|
+
[3, { active: true }]
|
|
455
|
+
]);
|
|
456
|
+
|
|
457
|
+
const inactive = tree
|
|
458
|
+
.filter((val) => val?.active ?? false)
|
|
459
|
+
.map((val, key) => [key, !val]);
|
|
460
|
+
|
|
461
|
+
console.log(...inactive);
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
### Sorting Pattern
|
|
465
|
+
|
|
466
|
+
```typescript
|
|
467
|
+
const data = [64, 34, 25, 12, 22, 11, 90];
|
|
468
|
+
const sorted = [...new RedBlackTree(data).keys()]; // Instant sort!
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
---
|
|
472
|
+
|
|
473
|
+
## SkipList & SkipListSet
|
|
474
|
+
|
|
475
|
+
Probabilistic sorted containers. Interchangeable with `TreeMap`/`TreeSet`.
|
|
476
|
+
|
|
477
|
+
```typescript
|
|
478
|
+
import { SkipList } from 'data-structure-typed';
|
|
479
|
+
|
|
480
|
+
// Same API as TreeMap — drop-in replacement
|
|
481
|
+
const sl = new SkipList<number, string>([[3, 'c'], [1, 'a'], [2, 'b']]);
|
|
482
|
+
sl.set(4, 'd'); // upsert — returns this (chainable)
|
|
483
|
+
sl.get(2); // 'b'
|
|
484
|
+
sl.has(5); // false
|
|
485
|
+
sl.delete(1); // true
|
|
486
|
+
|
|
487
|
+
// Navigation
|
|
488
|
+
sl.first(); // [2, 'b']
|
|
489
|
+
sl.last(); // [4, 'd']
|
|
490
|
+
sl.ceiling(2); // [2, 'b'] — smallest >= 2
|
|
491
|
+
sl.floor(3); // [3, 'c'] — largest <= 3
|
|
492
|
+
sl.higher(2); // [3, 'c'] — strictly > 2
|
|
493
|
+
sl.lower(3); // [2, 'b'] — strictly < 3
|
|
494
|
+
sl.rangeSearch([2, 4]); // [[2,'b'],[3,'c'],[4,'d']]
|
|
495
|
+
sl.pollFirst(); // [2, 'b'] — remove+return first
|
|
496
|
+
|
|
497
|
+
// Iteration (sorted order)
|
|
498
|
+
for (const [k, v] of sl) console.log(k, v);
|
|
499
|
+
[...sl.keys()]; // [3, 4]
|
|
500
|
+
[...sl.values()]; // ['c', 'd']
|
|
501
|
+
|
|
502
|
+
// Functional
|
|
503
|
+
sl.filter((v, k) => k > 2).toArray(); // [[3,'c'],[4,'d']]
|
|
504
|
+
sl.map((v, k) => [k * 10, v]); // new SkipList
|
|
505
|
+
sl.reduce((acc, v) => acc + v!, ''); // 'cd'
|
|
506
|
+
|
|
507
|
+
// Custom comparator
|
|
508
|
+
const reversed = new SkipList<number, string>([], {
|
|
509
|
+
comparator: (a, b) => b - a
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// From objects via toEntryFn
|
|
513
|
+
type User = { id: number; name: string };
|
|
514
|
+
const users = new SkipList<number, User, User>(data, {
|
|
515
|
+
toEntryFn: u => [u.id, u]
|
|
516
|
+
});
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
---
|
|
520
|
+
|
|
521
|
+
## SegmentTree
|
|
522
|
+
|
|
523
|
+
Range queries with any associative merge operation.
|
|
524
|
+
|
|
525
|
+
```typescript
|
|
526
|
+
import { SegmentTree } from 'data-structure-typed';
|
|
527
|
+
|
|
528
|
+
// Convenience factories (covers 90% of use cases)
|
|
529
|
+
const sumTree = SegmentTree.sum([1, 3, 5, 7, 9]);
|
|
530
|
+
const minTree = SegmentTree.min([5, 2, 8, 1, 9]);
|
|
531
|
+
const maxTree = SegmentTree.max([5, 2, 8, 1, 9]);
|
|
532
|
+
|
|
533
|
+
// Range query O(log n)
|
|
534
|
+
sumTree.query(1, 3); // 15 (3+5+7)
|
|
535
|
+
minTree.query(0, 4); // 1
|
|
536
|
+
maxTree.query(0, 2); // 8
|
|
537
|
+
|
|
538
|
+
// Point update O(log n)
|
|
539
|
+
sumTree.update(2, 10); // replaces 5 with 10
|
|
540
|
+
sumTree.query(1, 3); // 20 (3+10+7)
|
|
541
|
+
|
|
542
|
+
// Single element access O(1)
|
|
543
|
+
sumTree.get(2); // 10
|
|
544
|
+
|
|
545
|
+
// Custom merge (gcd, product, etc.)
|
|
546
|
+
const gcd = (a: number, b: number): number => b === 0 ? a : gcd(b, a % b);
|
|
547
|
+
const gcdTree = new SegmentTree([12, 8, 6, 18], { merger: gcd, identity: 0 });
|
|
548
|
+
gcdTree.query(0, 3); // 2
|
|
549
|
+
|
|
550
|
+
// Binary search on tree (ACL-style)
|
|
551
|
+
// maxRight(l, pred): find max r where pred(query(l, r)) is true
|
|
552
|
+
sumTree.maxRight(0, s => s <= 10); // rightmost index where prefix ≤ 10
|
|
553
|
+
|
|
554
|
+
// Standard interface
|
|
555
|
+
[...sumTree]; // leaf values as array
|
|
556
|
+
sumTree.toArray(); // same
|
|
557
|
+
sumTree.size; // 5
|
|
558
|
+
sumTree.clone(); // independent copy
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
---
|
|
562
|
+
|
|
563
|
+
## BinaryIndexedTree (Fenwick Tree)
|
|
564
|
+
|
|
565
|
+
Prefix sums and point updates in O(log n). Lighter than SegmentTree; use when you only need sums.
|
|
566
|
+
|
|
567
|
+
```typescript
|
|
568
|
+
import { BinaryIndexedTree } from 'data-structure-typed';
|
|
569
|
+
|
|
570
|
+
// Construct from size or array
|
|
571
|
+
const bit = new BinaryIndexedTree(6);
|
|
572
|
+
const bit2 = new BinaryIndexedTree([1, 3, 5, 7, 9, 11]);
|
|
573
|
+
|
|
574
|
+
// Point update: add delta
|
|
575
|
+
bit2.update(2, 4); // index 2 += 4 → value becomes 9
|
|
576
|
+
|
|
577
|
+
// Point set: absolute value
|
|
578
|
+
bit2.set(0, 100); // index 0 = 100
|
|
579
|
+
|
|
580
|
+
// Point query
|
|
581
|
+
bit2.get(2); // 9
|
|
582
|
+
|
|
583
|
+
// Prefix sum [0..i]
|
|
584
|
+
bit2.query(3); // sum of [0..3]
|
|
585
|
+
|
|
586
|
+
// Range sum [start..end]
|
|
587
|
+
bit2.queryRange(1, 3); // sum of [1..3]
|
|
588
|
+
|
|
589
|
+
// Binary search — requires non-negative values
|
|
590
|
+
bit2.lowerBound(10); // smallest i where prefix sum [0..i] >= 10
|
|
591
|
+
bit2.upperBound(10); // smallest i where prefix sum [0..i] > 10
|
|
592
|
+
|
|
593
|
+
// Standard interface
|
|
594
|
+
[...bit2]; // point values as array
|
|
595
|
+
bit2.toArray(); // same
|
|
596
|
+
bit2.size; // 6
|
|
597
|
+
bit2.clone();
|
|
598
|
+
bit2.clear();
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
---
|
|
602
|
+
|
|
603
|
+
## Matrix
|
|
604
|
+
|
|
605
|
+
2D grid arithmetic. Correct, minimal — not competing with NumPy.
|
|
606
|
+
|
|
607
|
+
```typescript
|
|
608
|
+
import { Matrix } from 'data-structure-typed';
|
|
609
|
+
|
|
610
|
+
// Construction
|
|
611
|
+
const m = new Matrix([[1, 2, 3], [4, 5, 6]]);
|
|
612
|
+
Matrix.zeros(3, 4); // 3×4 zero matrix
|
|
613
|
+
Matrix.identity(3); // 3×3 identity matrix
|
|
614
|
+
Matrix.from([[1, 2], [3, 4]]); // from plain array
|
|
615
|
+
|
|
616
|
+
// Element access
|
|
617
|
+
m.get(0, 1); // 2
|
|
618
|
+
m.set(0, 1, 99); // returns boolean
|
|
619
|
+
m.size; // [2, 3]
|
|
620
|
+
m.rows; // 2
|
|
621
|
+
m.cols; // 3
|
|
622
|
+
|
|
623
|
+
// Arithmetic (returns new Matrix)
|
|
624
|
+
a.add(b);
|
|
625
|
+
a.subtract(b);
|
|
626
|
+
a.multiply(b); // matrix multiplication
|
|
627
|
+
a.dot(b); // dot product
|
|
628
|
+
a.transpose(); // supports rectangular matrices
|
|
629
|
+
a.inverse(); // square matrices only
|
|
630
|
+
|
|
631
|
+
// Standard interface
|
|
632
|
+
[...m]; // array of rows (copies)
|
|
633
|
+
m.toArray(); // deep copy as number[][]
|
|
634
|
+
m.flatten(); // [1,2,3,4,5,6] row-major
|
|
635
|
+
m.forEach((v, r, c) => ...);
|
|
636
|
+
m.map(v => v * 2); // new Matrix
|
|
637
|
+
m.clone(); // independent copy
|
|
638
|
+
m.isEmpty(); // true if 0 rows or 0 cols
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
---
|
|
642
|
+
|
|
643
|
+
**Need details?** Check [GUIDES.md](/guide/guides.md) for real-world examples.
|
|
644
|
+
|
|
645
|
+
**Performance curious?** See [PERFORMANCE.md](/guide/performance.md) for benchmarks.
|