data-structure-typed/README.md

776 lines
27 KiB
Markdown
Raw Normal View History

2023-08-16 16:50:14 +00:00
# What
2023-08-11 11:21:34 +00:00
2023-08-16 16:50:14 +00:00
## Brief
Javascript & TypeScript Data Structure Library.
Meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a
wide range of data structures
## Data Structures
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed
Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack,
Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
# How
2023-08-12 15:23:09 +00:00
2023-08-11 11:21:34 +00:00
## install
### yarn
```bash
yarn add data-structure-typed
```
### npm
```bash
npm install data-structure-typed
```
2023-08-16 16:53:27 +00:00
## Live Examples
[//]: # ([Live Examples](https://data-structure-typed-examples.vercel.app))
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
2023-08-15 17:35:16 +00:00
2023-08-16 16:50:14 +00:00
### Binary Search Tree (BST) snippet
```typescript
import {BST, BSTNode} from 'data-structure-typed';
const tree = new BST();
expect(tree).toBeInstanceOf(BST);
const ids = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
tree.addMany(ids);
expect(tree.root).toBeInstanceOf(BSTNode);
if (tree.root) expect(tree.root.id).toBe(11);
expect(tree.count).toBe(16);
expect(tree.has(6)).toBe(true);
const node6 = tree.get(6);
expect(node6 && tree.getHeight(node6)).toBe(2);
expect(node6 && tree.getDepth(node6)).toBe(3);
const nodeId10 = tree.get(10, 'id');
expect(nodeId10?.id).toBe(10);
const nodeVal9 = tree.get(9, 'val');
expect(nodeVal9?.id).toBe(9);
const nodesByCount1 = tree.getNodes(1, 'count');
expect(nodesByCount1.length).toBe(16);
const leftMost = tree.getLeftMost();
expect(leftMost?.id).toBe(1);
const node15 = tree.get(15);
const minNodeBySpecificNode = node15 && tree.getLeftMost(node15);
expect(minNodeBySpecificNode?.id).toBe(12);
const subTreeSum = node15 && tree.subTreeSum(node15);
expect(subTreeSum).toBe(70);
const lesserSum = tree.lesserSum(10);
expect(lesserSum).toBe(45);
expect(node15).toBeInstanceOf(BSTNode);
if (node15 instanceof BSTNode) {
const subTreeAdd = tree.subTreeAdd(node15, 1, 'count');
expect(subTreeAdd).toBeDefined();
}
const node11 = tree.get(11);
expect(node11).toBeInstanceOf(BSTNode);
if (node11 instanceof BSTNode) {
const allGreaterNodesAdded = tree.allGreaterNodesAdd(node11, 2, 'count');
expect(allGreaterNodesAdded).toBeDefined();
}
const dfsInorderNodes = tree.DFS('in', 'node');
expect(dfsInorderNodes[0].id).toBe(1);
expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
tree.balance();
expect(tree.isBalanced()).toBe(true);
const bfsNodesAfterBalanced = tree.BFS('node');
expect(bfsNodesAfterBalanced[0].id).toBe(8);
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
const removed11 = tree.remove(11, true);
expect(removed11).toBeInstanceOf(Array);
expect(removed11[0]).toBeDefined();
expect(removed11[0].deleted).toBeDefined();
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
expect(tree.isAVLBalanced()).toBe(true);
expect(node15 && tree.getHeight(node15)).toBe(2);
const removed1 = tree.remove(1, true);
expect(removed1).toBeInstanceOf(Array);
expect(removed1[0]).toBeDefined();
expect(removed1[0].deleted).toBeDefined();
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(4);
// The code for removing these nodes (4, 10, 15, 5, 13, 3, 8, 6, 7, 9, 14) in sequence has been omitted.
expect(tree.isAVLBalanced()).toBe(false);
const bfsIDs = tree.BFS();
expect(bfsIDs[0]).toBe(2);
expect(bfsIDs[1]).toBe(12);
expect(bfsIDs[2]).toBe(16);
const bfsNodes = tree.BFS('node');
expect(bfsNodes[0].id).toBe(2);
expect(bfsNodes[1].id).toBe(12);
expect(bfsNodes[2].id).toBe(16);
```
### Directed Graph simple snippet
2023-08-11 04:02:16 +00:00
2023-08-16 16:50:14 +00:00
```typescript
import {DirectedGraph, DirectedVertex, DirectedEdge, VertexId} from 'data-structure-typed';
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;
beforeEach(() => {
graph = new DirectedGraph();
});
it('should add vertices', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
expect(graph.hasVertex(vertex1)).toBe(true);
expect(graph.hasVertex(vertex2)).toBe(true);
});
it('should add edges', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
const edge = new DirectedEdge('A', 'B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.hasEdge('A', 'B')).toBe(true);
expect(graph.hasEdge('B', 'A')).toBe(false);
});
it('should remove edges', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
const edge = new DirectedEdge('A', 'B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.removeEdge(edge)).toBe(edge);
expect(graph.hasEdge('A', 'B')).toBe(false);
});
it('should perform topological sort', () => {
const vertexA = new DirectedVertex('A');
const vertexB = new DirectedVertex('B');
const vertexC = new DirectedVertex('C');
const edgeAB = new DirectedEdge('A', 'B');
const edgeBC = new DirectedEdge('B', 'C');
graph.addVertex(vertexA);
graph.addVertex(vertexB);
graph.addVertex(vertexC);
graph.addEdge(edgeAB);
graph.addEdge(edgeBC);
const topologicalOrder = graph.topologicalSort();
if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']);
});
```
### Directed Graph complex snippet
```typescript
import {DirectedGraph, DirectedVertex, DirectedEdge, VertexId} from 'data-structure-typed';
class MyVertex extends DirectedVertex {
private _data: string;
get data(): string {
return this._data;
}
set data(value: string) {
this._data = value;
}
constructor(id: VertexId, data: string) {
super(id);
this._data = data;
}
}
class MyEdge extends DirectedEdge {
private _data: string;
get data(): string {
return this._data;
}
set data(value: string) {
this._data = value;
}
constructor(v1: VertexId, v2: VertexId, weight: number, data: string) {
super(v1, v2, weight);
this._data = data;
}
}
describe('DirectedGraph Test3', () => {
const myGraph = new DirectedGraph<MyVertex, MyEdge>();
it('should test graph operations', () => {
const vertex1 = new MyVertex(1, 'data1');
const vertex2 = new MyVertex(2, 'data2');
const vertex3 = new MyVertex(3, 'data3');
const vertex4 = new MyVertex(4, 'data4');
const vertex5 = new MyVertex(5, 'data5');
const vertex6 = new MyVertex(6, 'data6');
const vertex7 = new MyVertex(7, 'data7');
const vertex8 = new MyVertex(8, 'data8');
const vertex9 = new MyVertex(9, 'data9');
myGraph.addVertex(vertex1);
myGraph.addVertex(vertex2);
myGraph.addVertex(vertex3);
myGraph.addVertex(vertex4);
myGraph.addVertex(vertex5);
myGraph.addVertex(vertex6);
myGraph.addVertex(vertex7);
myGraph.addVertex(vertex8);
myGraph.addVertex(vertex9);
myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
expect(myGraph.getEdge(1, 2)).toBeTruthy();
expect(myGraph.getEdge(2, 1)).toBeTruthy();
expect(myGraph.getEdge(1, '100')).toBeFalsy();
myGraph.removeEdgeBetween(1, 2);
expect(myGraph.getEdge(1, 2)).toBeFalsy();
myGraph.addEdge(new MyEdge(3, 1, 3, 'edge-data-3-1'));
myGraph.addEdge(new MyEdge(1, 9, 19, 'edge-data1-9'));
myGraph.addEdge(new MyEdge(9, 7, 97, 'edge-data9-7'));
myGraph.addEdge(new MyEdge(7, 9, 79, 'edge-data7-9'));
myGraph.addEdge(new MyEdge(1, 4, 14, 'edge-data1-4'));
myGraph.addEdge(new MyEdge(4, 7, 47, 'edge-data4-7'));
myGraph.addEdge(new MyEdge(1, 2, 12, 'edge-data1-2'));
myGraph.addEdge(new MyEdge(2, 3, 23, 'edge-data2-3'));
myGraph.addEdge(new MyEdge(3, 5, 35, 'edge-data3-5'));
myGraph.addEdge(new MyEdge(5, 7, 57, 'edge-data5-7'));
myGraph.addEdge(new MyEdge(7, 3, 73, 'edge-data7-3'));
const topologicalSorted = myGraph.topologicalSort();
expect(topologicalSorted).toBeNull();
const minPath1to7 = myGraph.getMinPathBetween(1, 7);
expect(minPath1to7).toBeInstanceOf(Array);
if (minPath1to7 && minPath1to7.length > 0) {
expect(minPath1to7).toHaveLength(3);
expect(minPath1to7[0]).toBeInstanceOf(MyVertex);
expect(minPath1to7[0].id).toBe(1);
expect(minPath1to7[1].id).toBe(9);
expect(minPath1to7[2].id).toBe(7);
}
const fordResult1 = myGraph.bellmanFord(1);
expect(fordResult1).toBeTruthy();
expect(fordResult1.hasNegativeCycle).toBeUndefined();
const {distMap, preMap, paths, min, minPath} = fordResult1;
expect(distMap).toBeInstanceOf(Map);
expect(distMap.size).toBe(9);
expect(distMap.get(vertex1)).toBe(0);
expect(distMap.get(vertex2)).toBe(12);
expect(distMap.get(vertex3)).toBe(35);
expect(distMap.get(vertex4)).toBe(14);
expect(distMap.get(vertex5)).toBe(70);
expect(distMap.get(vertex6)).toBe(Infinity);
expect(distMap.get(vertex7)).toBe(61);
expect(distMap.get(vertex8)).toBe(Infinity);
expect(distMap.get(vertex9)).toBe(19);
expect(preMap).toBeInstanceOf(Map);
expect(preMap.size).toBe(0);
expect(paths).toBeInstanceOf(Array);
expect(paths.length).toBe(0);
expect(min).toBe(Infinity);
expect(minPath).toBeInstanceOf(Array);
const floydResult = myGraph.floyd();
expect(floydResult).toBeTruthy();
if (floydResult) {
const {costs, predecessor} = floydResult;
expect(costs).toBeInstanceOf(Array);
expect(costs.length).toBe(9);
expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]);
expect(costs[1]).toEqual([20, 32, 23, 34, 58, Infinity, 81, Infinity, 39]);
expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]);
expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]);
expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]);
expect(costs[5]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]);
expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]);
expect(costs[7]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]);
expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]);
expect(predecessor).toBeInstanceOf(Array);
expect(predecessor.length).toBe(9);
expect(predecessor[0]).toEqual([vertex2, null, vertex2, null, vertex3, null, vertex4, null, null]);
expect(predecessor[1]).toEqual([null, vertex1, null, vertex1, vertex3, null, vertex4, null, vertex1]);
expect(predecessor[5]).toEqual([null, null, null, null, null, null, null, null, null]);
expect(predecessor[7]).toEqual([null, null, null, null, null, null, null, null, null]);
expect(predecessor[8]).toEqual([vertex7, vertex7, vertex7, vertex7, vertex7, null, null, null, vertex7]);
}
const dijkstraRes12tt = myGraph.dijkstra(1, 2, true, true);
expect(dijkstraRes12tt).toBeTruthy();
if (dijkstraRes12tt) {
const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes12tt;
expect(distMap).toBeInstanceOf(Map);
expect(distMap.size).toBe(9);
expect(distMap.get(vertex1)).toBe(0);
expect(distMap.get(vertex2)).toBe(12);
expect(distMap.get(vertex3)).toBe(Infinity);
expect(distMap.get(vertex4)).toBe(14);
expect(distMap.get(vertex5)).toBe(Infinity);
expect(distMap.get(vertex6)).toBe(Infinity);
expect(distMap.get(vertex7)).toBe(Infinity);
expect(distMap.get(vertex8)).toBe(Infinity);
expect(distMap.get(vertex9)).toBe(19);
expect(minDist).toBe(12);
expect(minPath).toBeInstanceOf(Array);
expect(minPath.length).toBe(2);
expect(minPath[0]).toBe(vertex1);
expect(minPath[1]).toBe(vertex2);
expect(paths).toBeInstanceOf(Array);
expect(paths.length).toBe(9);
expect(paths[0]).toBeInstanceOf(Array);
expect(paths[0][0]).toBe(vertex1);
expect(paths[1]).toBeInstanceOf(Array);
expect(paths[1][0]).toBe(vertex1);
expect(paths[1][1]).toBe(vertex2);
expect(paths[2]).toBeInstanceOf(Array);
expect(paths[2][0]).toBe(vertex3);
expect(paths[3]).toBeInstanceOf(Array);
expect(paths[3][0]).toBe(vertex1);
expect(paths[3][1]).toBe(vertex4);
expect(paths[4]).toBeInstanceOf(Array);
expect(paths[4][0]).toBe(vertex5);
expect(paths[5]).toBeInstanceOf(Array);
expect(paths[5][0]).toBe(vertex6);
expect(paths[6]).toBeInstanceOf(Array);
expect(paths[6][0]).toBe(vertex7);
expect(paths[7]).toBeInstanceOf(Array);
expect(paths[7][0]).toBe(vertex8);
expect(paths[8]).toBeInstanceOf(Array);
expect(paths[8][0]).toBe(vertex1);
expect(paths[8][1]).toBe(vertex9);
}
});
});
```
## API docs
2023-08-14 01:01:54 +00:00
[//]: # ([api docs]&#40;https://data-structure-typed-docs.vercel.app/&#41;)
2023-08-14 01:01:54 +00:00
<nav class="tsd-navigation"><a href="https://data-structure-typed-docs.vercel.app/modules.html" class="current"><span>data-<wbr/>structure-<wbr/>typed</span></a>
2023-08-14 00:59:00 +00:00
<ul class="tsd-small-nested-navigation">
2023-08-14 01:03:05 +00:00
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/enums/CP.html"><span>CP</span></a></li>)
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/enums/FamilyPosition.html"><span>Family<wbr/>Position</span></a></li>)
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/enums/LoopType.html"><span>Loop<wbr/>Type</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTree.html"><span>AVLTree</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTreeNode.html"><span>AVLTree<wbr/>Node</span></a></li>
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/classes/AaTree.html"><span>Aa<wbr/>Tree</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractEdge.html"><span>Abstract<wbr/>Edge</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractGraph.html"><span>Abstract<wbr/>Graph</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractVertex.html"><span>Abstract<wbr/>Vertex</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/ArrayDeque.html"><span>Array<wbr/>Deque</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/BST.html"><span>BST</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/BSTNode.html"><span>BSTNode</span></a></li>
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/classes/BTree.html"><span>BTree</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryIndexedTree.html"><span>Binary<wbr/>Indexed<wbr/>Tree</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary<wbr/>Tree</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTreeNode.html"><span>Binary<wbr/>Tree<wbr/>Node</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Character.html"><span>Character</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/CoordinateMap.html"><span>Coordinate<wbr/>Map</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/CoordinateSet.html"><span>Coordinate<wbr/>Set</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Deque.html"><span>Deque</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedEdge.html"><span>Directed<wbr/>Edge</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedGraph.html"><span>Directed<wbr/>Graph</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedVertex.html"><span>Directed<wbr/>Vertex</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedList.html"><span>Doubly<wbr/>Linked<wbr/>List</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedListNode.html"><span>Doubly<wbr/>Linked<wbr/>List<wbr/>Node</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Heap.html"><span>Heap</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Matrix2D.html"><span>Matrix2D</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/MatrixNTI2D.html"><span>MatrixNTI2D</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/MaxHeap.html"><span>Max<wbr/>Heap</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/MaxPriorityQueue.html"><span>Max<wbr/>Priority<wbr/>Queue</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/MinHeap.html"><span>Min<wbr/>Heap</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/MinPriorityQueue.html"><span>Min<wbr/>Priority<wbr/>Queue</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Navigator.html"><span>Navigator</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/ObjectDeque.html"><span>Object<wbr/>Deque</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/PriorityQueue.html"><span>Priority<wbr/>Queue</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Queue.html"><span>Queue</span></a></li>
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/classes/RBTree.html"><span>RBTree</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTree.html"><span>Segment<wbr/>Tree</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTreeNode.html"><span>Segment<wbr/>Tree<wbr/>Node</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>Singly<wbr/>Linked<wbr/>List</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedListNode.html"><span>Singly<wbr/>Linked<wbr/>List<wbr/>Node</span></a></li>
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/classes/SplayTree.html"><span>Splay<wbr/>Tree</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Stack.html"><span>Stack</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultiSet.html"><span>Tree<wbr/>Multi<wbr/>Set</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Trie.html"><span>Trie</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/TrieNode.html"><span>Trie<wbr/>Node</span></a></li>
[//]: # (<li><a href="https://data-structure-typed-docs.vercel.app/classes/TwoThreeTree.html"><span>Two<wbr/>Three<wbr/>Tree</span></a></li>)
2023-08-14 00:59:00 +00:00
<li><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedEdge.html"><span>Undirected<wbr/>Edge</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedGraph.html"><span>Undirected<wbr/>Graph</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedVertex.html"><span>Undirected<wbr/>Vertex</span></a></li>
<li><a href="https://data-structure-typed-docs.vercel.app/classes/Vector2D.html"><span>Vector2D</span></a></li></ul></nav>
2023-08-16 14:48:14 +00:00
[//]: # ([Examples Repository]&#40;https://github.com/zrwusa/data-structure-typed-examples&#41;)
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
2023-08-16 16:50:14 +00:00
# Why
2023-08-16 14:48:14 +00:00
## Complexities
### performance of Big O
2023-08-11 04:02:16 +00:00
<table>
2023-08-11 11:21:34 +00:00
<thead>
<tr>
<th>Big O Notation</th>
<th>Type</th>
<th>Computations for 10 elements</th>
<th>Computations for 100 elements</th>
<th>Computations for 1000 elements</th>
</tr>
2023-08-11 11:21:34 +00:00
</thead>
<tbody>
<tr>
<td><strong>O(1)</strong></td>
<td>Constant</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><strong>O(log N)</strong></td>
<td>Logarithmic</td>
<td>3</td>
<td>6</td>
<td>9</td>
</tr>
<tr>
<td><strong>O(N)</strong></td>
<td>Linear</td>
<td>10</td>
<td>100</td>
<td>1000</td>
</tr>
<tr>
<td><strong>O(N log N)</strong></td>
<td>n log(n)</td>
<td>30</td>
<td>600</td>
<td>9000</td>
</tr>
<tr>
<td><strong>O(N^2)</strong></td>
<td>Quadratic</td>
<td>100</td>
<td>10000</td>
<td>1000000</td>
</tr>
<tr>
<td><strong>O(2^N)</strong></td>
<td>Exponential</td>
<td>1024</td>
<td>1.26e+29</td>
<td>1.07e+301</td>
</tr>
<tr>
<td><strong>O(N!)</strong></td>
<td>Factorial</td>
<td>3628800</td>
<td>9.3e+157</td>
<td>4.02e+2567</td>
</tr>
2023-08-11 11:21:34 +00:00
</tbody>
</table>
### Data Structure Complexity
2023-08-11 04:02:16 +00:00
<table>
<thead>
<tr>
<th>Data Structure</th>
<th>Access</th>
<th>Search</th>
<th>Insertion</th>
<th>Deletion</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Array</strong></td>
<td>1</td>
<td>n</td>
<td>n</td>
<td>n</td>
<td></td>
</tr>
<tr>
<td><strong>Stack</strong></td>
<td>n</td>
<td>n</td>
<td>1</td>
<td>1</td>
<td></td>
</tr>
<tr>
<td><strong>Queue</strong></td>
<td>n</td>
<td>n</td>
<td>1</td>
<td>1</td>
<td></td>
</tr>
<tr>
<td><strong>Linked List</strong></td>
<td>n</td>
<td>n</td>
<td>1</td>
<td>n</td>
<td></td>
</tr>
<tr>
<td><strong>Hash Table</strong></td>
<td>-</td>
<td>n</td>
<td>n</td>
<td>n</td>
<td>In case of perfect hash function costs would be O(1)</td>
</tr>
<tr>
<td><strong>Binary Search Tree</strong></td>
<td>n</td>
<td>n</td>
<td>n</td>
<td>n</td>
<td>In case of balanced tree costs would be O(log(n))</td>
</tr>
<tr>
<td><strong>B-Tree</strong></td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td></td>
</tr>
<tr>
<td><strong>Red-Black Tree</strong></td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td></td>
</tr>
<tr>
<td><strong>AVL Tree</strong></td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td>log(n)</td>
<td></td>
</tr>
<tr>
<td><strong>Bloom Filter</strong></td>
<td>-</td>
<td>1</td>
<td>1</td>
<td>-</td>
<td>False positives are possible while searching</td>
</tr>
</tbody>
2023-08-11 11:21:34 +00:00
</table>
2023-08-11 04:02:16 +00:00
### Sorting Complexity
2023-08-11 04:02:16 +00:00
<table>
<thead>
<tr>
<th>Name</th>
<th>Best</th>
<th>Average</th>
<th>Worst</th>
<th>Memory</th>
<th>Stable</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Bubble sort</strong></td>
<td>n</td>
<td>n<sup>2</sup></td>
<td>n<sup>2</sup></td>
<td>1</td>
<td>Yes</td>
<td></td>
</tr>
<tr>
<td><strong>Insertion sort</strong></td>
<td>n</td>
<td>n<sup>2</sup></td>
<td>n<sup>2</sup></td>
<td>1</td>
<td>Yes</td>
<td></td>
</tr>
<tr>
<td><strong>Selection sort</strong></td>
<td>n<sup>2</sup></td>
<td>n<sup>2</sup></td>
<td>n<sup>2</sup></td>
<td>1</td>
<td>No</td>
<td></td>
</tr>
<tr>
<td><strong>Heap sort</strong></td>
<td>n&nbsp;log(n)</td>
<td>n&nbsp;log(n)</td>
<td>n&nbsp;log(n)</td>
<td>1</td>
<td>No</td>
<td></td>
</tr>
<tr>
<td><strong>Merge sort</strong></td>
<td>n&nbsp;log(n)</td>
<td>n&nbsp;log(n)</td>
<td>n&nbsp;log(n)</td>
<td>n</td>
<td>Yes</td>
<td></td>
</tr>
<tr>
<td><strong>Quick sort</strong></td>
<td>n&nbsp;log(n)</td>
<td>n&nbsp;log(n)</td>
<td>n<sup>2</sup></td>
<td>log(n)</td>
<td>No</td>
<td>Quicksort is usually done in-place with O(log(n)) stack space</td>
</tr>
<tr>
<td><strong>Shell sort</strong></td>
<td>n&nbsp;log(n)</td>
<td>depends on gap sequence</td>
<td>n&nbsp;(log(n))<sup>2</sup></td>
<td>1</td>
<td>No</td>
<td></td>
</tr>
<tr>
<td><strong>Counting sort</strong></td>
<td>n + r</td>
<td>n + r</td>
<td>n + r</td>
<td>n + r</td>
<td>Yes</td>
<td>r - biggest number in array</td>
</tr>
<tr>
<td><strong>Radix sort</strong></td>
<td>n * k</td>
<td>n * k</td>
<td>n * k</td>
<td>n + k</td>
<td>Yes</td>
<td>k - length of longest key</td>
</tr>
</tbody>
</table>
2023-08-11 04:02:16 +00:00
2023-08-16 16:50:14 +00:00
![overview diagram](src/assets/overview-diagram-of-data-structures.png)
2023-06-27 16:32:34 +00:00
2023-06-27 16:42:28 +00:00
![complexities](src/assets/complexities-diff.jpg)
2023-06-27 16:32:34 +00:00
![complexities of data structures](src/assets/data-structure-complexities.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/bst-rotation.gif)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/avl-tree-inserting.gif)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan.webp)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list-pros-cons.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix-pros-cons.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/dfs-can-do.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list-pros-cons.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/max-flow.jpg)
2023-08-17 01:09:38 +00:00
![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/mst.jpg)
2023-08-17 01:09:38 +00:00
[//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-articulation-point-bridge.png&#41;)
2023-08-17 01:09:38 +00:00
[//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-complicate-simple.png&#41;)
2023-08-17 01:09:38 +00:00
[//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-strongly-connected-component.png&#41;)