mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-10 08:04:05 +00:00
675 lines
31 KiB
Markdown
675 lines
31 KiB
Markdown
# Data Structure Typed
|
|
|
|
![NPM](https://img.shields.io/npm/l/data-structure-typed)
|
|
![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed)
|
|
![npm](https://img.shields.io/npm/dw/data-structure-typed)
|
|
![eslint](https://aleen42.github.io/badges/src/eslint.svg)
|
|
![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/data-structure-typed)
|
|
![npm bundle size](https://img.shields.io/bundlephobia/min/data-structure-typed)
|
|
![npm](https://img.shields.io/npm/v/data-structure-typed)
|
|
|
|
|
|
Data Structures of Javascript & TypeScript.
|
|
|
|
Do you envy C++ with [STL](), Python with [collections](), and Java with [java.util]() ? Well, no need to envy anymore! JavaScript and TypeScript now have [data-structure-typed]().
|
|
|
|
Now you can use this library in Node.js and browser environments in CommonJS(require export.modules = ), ESModule(import export), Typescript(import export), UMD(var Queue = dataStructureTyped.Queue)
|
|
|
|
|
|
[//]: # (![Branches](https://img.shields.io/badge/branches-55.47%25-red.svg?style=flat))
|
|
|
|
[//]: # (![Statements](https://img.shields.io/badge/statements-67%25-red.svg?style=flat))
|
|
|
|
[//]: # (![Functions](https://img.shields.io/badge/functions-66.38%25-red.svg?style=flat))
|
|
|
|
[//]: # (![Lines](https://img.shields.io/badge/lines-68.6%25-red.svg?style=flat))
|
|
|
|
|
|
## Built-in classic algorithms
|
|
|
|
DFS(Depth-First Search), DFSIterative, BFS(Breadth-First Search), morris, Bellman-Ford Algorithm, Dijkstra's Algorithm,
|
|
Floyd-Warshall Algorithm, Tarjan's Algorithm.
|
|
|
|
## Installation and Usage
|
|
|
|
### npm
|
|
|
|
```bash
|
|
npm i data-structure-typed --save
|
|
```
|
|
|
|
### yarn
|
|
|
|
```bash
|
|
yarn add data-structure-typed
|
|
```
|
|
|
|
```js
|
|
import {
|
|
BinaryTree, Graph, Queue, Stack, PriorityQueue, BST, Trie, DoublyLinkedList,
|
|
AVLTree, MinHeap, SinglyLinkedList, DirectedGraph, TreeMultiset,
|
|
DirectedVertex, AVLTreeNode
|
|
} from 'data-structure-typed';
|
|
```
|
|
|
|
### CDN
|
|
|
|
```html
|
|
|
|
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
```
|
|
|
|
```js
|
|
const {Heap} = dataStructureTyped;
|
|
const {
|
|
BinaryTree, Graph, Queue, Stack, PriorityQueue, BST, Trie, DoublyLinkedList,
|
|
AVLTree, MinHeap, SinglyLinkedList, DirectedGraph, TreeMultiset,
|
|
DirectedVertex, AVLTreeNode
|
|
} = dataStructureTyped;
|
|
```
|
|
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/binary-tree-array-to-binary-tree.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/binary-tree-dfs-in-order.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/avl-tree-test.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/tree-multiset-test.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/matrix-cut-off-tree-for-golf.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/directed-graph-test.webp)
|
|
![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/map-graph-test.webp)
|
|
|
|
## API docs & Examples
|
|
|
|
[API Docs](https://data-structure-typed-docs.vercel.app)
|
|
|
|
[Live Examples](https://vivid-algorithm.vercel.app)
|
|
|
|
<a href="https://github.com/zrwusa/vivid-algorithm" target="_blank">Examples Repository</a>
|
|
|
|
## Code Snippet
|
|
|
|
### Binary Search Tree (BST) snippet
|
|
|
|
#### TS
|
|
|
|
```ts
|
|
import {BST, BSTNode} from 'data-structure-typed';
|
|
|
|
const bst = new BST();
|
|
bst.add(11);
|
|
bst.add(3);
|
|
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
bst.size === 16; // true
|
|
bst.has(6); // true
|
|
const node6 = bst.get(6); // BSTNode
|
|
bst.getHeight(6) === 2; // true
|
|
bst.getHeight() === 5; // true
|
|
bst.getDepth(6) === 3; // true
|
|
|
|
bst.getLeftMost()?.id === 1; // true
|
|
|
|
bst.delete(6);
|
|
bst.get(6); // null
|
|
bst.isAVLBalanced(); // true
|
|
bst.bfs()[0] === 11; // true
|
|
|
|
const objBST = new BST<BSTNode<{id: number, keyA: number}>>();
|
|
objBST.add(11, {id: 11, keyA: 11});
|
|
objBST.add(3, {id: 3, keyA: 3});
|
|
|
|
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
|
|
objBST.delete(11);
|
|
```
|
|
|
|
#### JS
|
|
|
|
```js
|
|
const {BST, BSTNode} = require('data-structure-typed');
|
|
|
|
const bst = new BST();
|
|
bst.add(11);
|
|
bst.add(3);
|
|
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
bst.size === 16; // true
|
|
bst.has(6); // true
|
|
const node6 = bst.get(6);
|
|
bst.getHeight(6) === 2; // true
|
|
bst.getHeight() === 5; // true
|
|
bst.getDepth(6) === 3; // true
|
|
const leftMost = bst.getLeftMost();
|
|
leftMost?.id === 1; // true
|
|
expect(leftMost?.id).toBe(1);
|
|
bst.delete(6);
|
|
bst.get(6); // null
|
|
bst.isAVLBalanced(); // true or false
|
|
const bfsIDs = bst.bfs();
|
|
bfsIDs[0] === 11; // true
|
|
expect(bfsIDs[0]).toBe(11);
|
|
|
|
const objBST = new BST();
|
|
objBST.add(11, {id: 11, keyA: 11});
|
|
objBST.add(3, {id: 3, keyA: 3});
|
|
|
|
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
|
|
objBST.delete(11);
|
|
|
|
const avlTree = new AVLTree();
|
|
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
avlTree.isAVLBalanced(); // true
|
|
avlTree.delete(10);
|
|
avlTree.isAVLBalanced(); // true
|
|
```
|
|
|
|
### AVLTree snippet
|
|
|
|
#### TS
|
|
|
|
```ts
|
|
import {AVLTree} from 'data-structure-typed';
|
|
|
|
const avlTree = new AVLTree();
|
|
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
avlTree.isAVLBalanced(); // true
|
|
avlTree.delete(10);
|
|
avlTree.isAVLBalanced(); // true
|
|
```
|
|
|
|
#### JS
|
|
|
|
```js
|
|
const {AVLTree} = require('data-structure-typed');
|
|
|
|
const avlTree = new AVLTree();
|
|
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
avlTree.isAVLBalanced(); // true
|
|
avlTree.delete(10);
|
|
avlTree.isAVLBalanced(); // true
|
|
```
|
|
|
|
### Directed Graph simple snippet
|
|
|
|
#### TS or JS
|
|
|
|
```ts
|
|
import {DirectedGraph} from 'data-structure-typed';
|
|
|
|
const graph = new DirectedGraph();
|
|
|
|
graph.addVertex('A');
|
|
graph.addVertex('B');
|
|
|
|
graph.hasVertex('A'); // true
|
|
graph.hasVertex('B'); // true
|
|
graph.hasVertex('C'); // false
|
|
|
|
graph.addEdge('A', 'B');
|
|
graph.hasEdge('A', 'B'); // true
|
|
graph.hasEdge('B', 'A'); // false
|
|
|
|
graph.deleteEdgeSrcToDest('A', 'B');
|
|
graph.hasEdge('A', 'B'); // false
|
|
|
|
graph.addVertex('C');
|
|
|
|
graph.addEdge('A', 'B');
|
|
graph.addEdge('B', 'C');
|
|
|
|
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
```
|
|
|
|
### Undirected Graph snippet
|
|
|
|
#### TS or JS
|
|
|
|
```ts
|
|
import {UndirectedGraph} from 'data-structure-typed';
|
|
|
|
const graph = new UndirectedGraph();
|
|
graph.addVertex('A');
|
|
graph.addVertex('B');
|
|
graph.addVertex('C');
|
|
graph.addVertex('D');
|
|
graph.deleteVertex('C');
|
|
graph.addEdge('A', 'B');
|
|
graph.addEdge('B', 'D');
|
|
|
|
const dijkstraResult = graph.dijkstra('A');
|
|
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
```
|
|
|
|
## Data Structures
|
|
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Data Structure</th>
|
|
<th>Unit Test</th>
|
|
<th>Performance Test</th>
|
|
<th>API Documentation</th>
|
|
<th>Implemented</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>Binary Tree</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Binary Search Tree (BST)</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BST.html"><span>BST</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>AVL Tree</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTree.html"><span>AVLTree</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Red Black Tree</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/RedBlackTree.html"><span>AVLTree</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Tree Multiset</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultiset.html"><span>TreeMultiset</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Segment Tree</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTree.html"><span>SegmentTree</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Binary Indexed Tree</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryIndexedTree.html"><span>BinaryIndexedTree</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Graph</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractGraph.html"><span>AbstractGraph</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Directed Graph</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedGraph.html"><span>DirectedGraph</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Undirected Graph</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedGraph.html"><span>UndirectedGraph</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Linked List</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>SinglyLinkedList</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Singly Linked List</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>SinglyLinkedList</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Doubly Linked List</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedList.html"><span>DoublyLinkedList</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Queue</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Queue.html"><span>Queue</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Object Deque</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/ObjectDeque.html"><span>ObjectDeque</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Array Deque</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/ArrayDeque.html"><span>ArrayDeque</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Stack</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Stack.html"><span>Stack</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Coordinate Set</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/CoordinateSet.html"><span>CoordinateSet</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Coordinate Map</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/CoordinateMap.html"><span>CoordinateMap</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Heap</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Heap.html"><span>Heap</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Priority Queue</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/PriorityQueue.html"><span>PriorityQueue</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Max Priority Queue</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MaxPriorityQueue.html"><span>MaxPriorityQueue</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Min Priority Queue</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MinPriorityQueue.html"><span>MinPriorityQueue</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Trie</td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Trie.html"><span>Trie</span></a></td>
|
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
|
|
|
|
### Standard library data structure comparison
|
|
|
|
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Data Structure Typed</th>
|
|
<th>C++ STL</th>
|
|
<th>java.util</th>
|
|
<th>Python collections</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>Array<E></td>
|
|
<td>vector<T></td>
|
|
<td>ArrayList<E></td>
|
|
<td>list</td>
|
|
</tr>
|
|
<tr>
|
|
<td>DoublyLinkedList<E></td>
|
|
<td>list<T></td>
|
|
<td>LinkedList<E></td>
|
|
<td>deque</td>
|
|
</tr>
|
|
<tr>
|
|
<td>SinglyLinkedList<E></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Set<E></td>
|
|
<td>set<T></td>
|
|
<td>HashSet<E></td>
|
|
<td>set</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Map<K, V></td>
|
|
<td>map<K, V></td>
|
|
<td>HashMap<K, V></td>
|
|
<td>dict</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Map<K, V></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>OrderedDict</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>Queue<E></td>
|
|
<td>queue<T></td>
|
|
<td>Queue<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>PriorityQueue<E></td>
|
|
<td>priority_queue<T></td>
|
|
<td>PriorityQueue<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Heap<V></td>
|
|
<td>priority_queue<T></td>
|
|
<td>PriorityQueue<E></td>
|
|
<td>heapq</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Stack<E></td>
|
|
<td>stack<T></td>
|
|
<td>Stack<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Deque<E></td>
|
|
<td>deque<T></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Trie</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>HashMap<K, V></td>
|
|
<td>unordered_map<K, V></td>
|
|
<td>HashMap<K, V></td>
|
|
<td>defaultdict</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>multiset<T></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>multimap<K, V></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>BinaryTree<K, V></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>BST<K, V></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>DirectedGraph<V, E></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>UndirectedGraph<V, E></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>unordered_multiset</td>
|
|
<td>-</td>
|
|
<td>Counter</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>LinkedHashSet<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
<td>LinkedHashMap<K, V></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>AVLTree<E></td>
|
|
<td>-</td>
|
|
<td>TreeSet<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>AVLTree<K, V></td>
|
|
<td>-</td>
|
|
<td>TreeMap<K, V></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>AVLTree<E></td>
|
|
<td>set</td>
|
|
<td>TreeSet<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>unordered_multimap<K, V></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>bitset<N></td>
|
|
<td>-</td>
|
|
<td>-</td>
|
|
</tr>
|
|
<tr>
|
|
<td>-</td>
|
|
<td>unordered_set<T></td>
|
|
<td>HashSet<E></td>
|
|
<td>-</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
|
|
## Code design
|
|
|
|
### Adhere to ES6 standard naming conventions for APIs.
|
|
|
|
Standardize API conventions by using 'add' and 'delete' for element manipulation methods in all data structures.
|
|
|
|
Opt for concise and clear method names, avoiding excessive length while ensuring explicit intent.
|
|
|
|
### Object-oriented programming(OOP)
|
|
|
|
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly
|
|
inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the
|
|
optimal approach to data structure design.
|
|
|
|
## Benchmark
|
|
|
|
[//]: # (Start of Replace Section)
|
|
<div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>30.74</td><td>32.53</td><td>7.33e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>67.99</td><td>14.71</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>39.82</td><td>25.11</td><td>4.59e-4</td></tr><tr><td>10,000 get</td><td>26.70</td><td>37.46</td><td>2.61e-4</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>12.91</td><td>77.44</td><td>5.52e-4</td></tr><tr><td>1,000 add & delete randomly</td><td>15.90</td><td>62.88</td><td>1.24e-4</td></tr><tr><td>1,000 addMany</td><td>10.72</td><td>93.27</td><td>1.52e-4</td></tr><tr><td>1,000 get</td><td>18.39</td><td>54.39</td><td>2.28e-4</td></tr><tr><td>1,000 dfs</td><td>71.58</td><td>13.97</td><td>0.00</td></tr><tr><td>1,000 bfs</td><td>57.62</td><td>17.35</td><td>0.00</td></tr><tr><td>1,000 morris</td><td>37.17</td><td>26.90</td><td>2.13e-4</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>bst</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>30.50</td><td>32.79</td><td>0.00</td></tr><tr><td>10,000 add & delete randomly</td><td>68.35</td><td>14.63</td><td>8.92e-4</td></tr><tr><td>10,000 addMany</td><td>29.41</td><td>34.00</td><td>9.96e-4</td></tr><tr><td>10,000 get</td><td>28.27</td><td>35.37</td><td>5.46e-4</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add randomly</td><td>82.19</td><td>12.17</td><td>0.01</td></tr><tr><td>100,000 add & 1000 delete randomly</td><td>99.74</td><td>10.03</td><td>0.03</td></tr><tr><td>100,000 getNode</td><td>78.27</td><td>12.78</td><td>0.04</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9687.71</td><td>1.14e-5</td></tr><tr><td>1,000 addEdge</td><td>7.42</td><td>134.68</td><td>0.00</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.05e+4</td><td>9.33e-6</td></tr><tr><td>1,000 getEdge</td><td>20.29</td><td>49.29</td><td>0.00</td></tr><tr><td>tarjan</td><td>190.48</td><td>5.25</td><td>0.01</td></tr><tr><td>tarjan all</td><td>190.72</td><td>5.24</td><td>0.00</td></tr><tr><td>topologicalSort</td><td>161.21</td><td>6.20</td><td>0.02</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add & pop</td><td>4.70</td><td>212.55</td><td>5.89e-4</td></tr><tr><td>10,000 fib add & pop</td><td>366.96</td><td>2.73</td><td>0.02</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 unshift</td><td>226.42</td><td>4.42</td><td>0.04</td></tr><tr><td>1,000,000 unshift & shift</td><td>163.14</td><td>6.13</td><td>0.01</td></tr><tr><td>1,000,000 insertBefore</td><td>343.18</td><td>2.91</td><td>0.08</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 push & pop</td><td>217.34</td><td>4.60</td><td>0.01</td></tr><tr><td>10,000 insertBefore</td><td>248.51</td><td>4.02</td><td>0.00</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>max-priority-queue</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>11.65</td><td>85.81</td><td>3.52e-4</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>224.57</td><td>4.45</td><td>0.04</td></tr><tr><td>1,000,000 shift</td><td>25.15</td><td>39.77</td><td>0.00</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>43.52</td><td>22.98</td><td>0.01</td></tr><tr><td>1,000,000 push & shift</td><td>79.71</td><td>12.55</td><td>0.00</td></tr></table></div>
|
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
|
|
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>59.28</td><td>16.87</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>106.79</td><td>9.36</td><td>0.00</td></tr></table></div>
|
|
</div>
|
|
|
|
[//]: # (End of Replace Section) |