diff --git a/README.md b/README.md index e8df0d6..f2260bc 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,190 @@ ## Brief Javascript & TypeScript Data Structure Library. + +## Algorithms list only a few out, you can discover more in API docs + +DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm + +# How +## install + +### yarn + +```bash +yarn add data-structure-typed +``` + +### npm + +```bash +npm install data-structure-typed +``` + +### Binary Search Tree (BST) snippet + +#### TS +```typescript + 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); + 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.remove(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.remove(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.remove(10); + avlTree.isAVLBalanced(); // true + +``` +#### JS +```javascript + 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.remove(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.remove(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.remove(10); + avlTree.isAVLBalanced(); // true + +``` + +### Directed Graph simple snippet + +#### TS or JS +```typescript +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.removeEdgeSrcToDest('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 +```typescript +import {UndirectedGraph} from 'data-structure-typed'; + + const graph = new UndirectedGraph(); + graph.addVertex('A'); + graph.addVertex('B'); + graph.addVertex('C'); + graph.addVertex('D'); + graph.removeVertex('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'] +``` + +[API Docs](https://data-structure-typed-docs.vercel.app) + +[Live Examples](https://data-structure-typed-examples.vercel.app) + + +![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/dfs-pre-order.webp) + +![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/test-graphs.webp) + +![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/cut-off-trees-for-golf.webp) + +![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/parenthesis-check.webp) + +Live Examples + + +## API docs + + +[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples)) + +Examples Repository + + Meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures @@ -199,187 +383,6 @@ wide range of data structures -## Algorithms list only a few out, you can discover more in API docs - -DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm - -# How -## install - -### yarn - -```bash -yarn add data-structure-typed -``` - -### npm - -```bash -npm install data-structure-typed -``` - -### Binary Search Tree (BST) snippet - -#### TS -```typescript - 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); - 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.remove(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.remove(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.remove(10); - avlTree.isAVLBalanced(); // true - -``` -#### JS -```javascript - 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.remove(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.remove(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.remove(10); - avlTree.isAVLBalanced(); // true - -``` - -### Directed Graph simple snippet - -#### TS or JS -```typescript -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.removeEdgeSrcToDest('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 -```typescript -import {UndirectedGraph} from 'data-structure-typed'; - - const graph = new UndirectedGraph(); - graph.addVertex('A'); - graph.addVertex('B'); - graph.addVertex('C'); - graph.addVertex('D'); - graph.removeVertex('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'] -``` - -[API Docs](https://data-structure-typed-docs.vercel.app) - -[Live Examples](https://data-structure-typed-examples.vercel.app) - - -![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/dfs-pre-order.webp) - -![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/test-graphs.webp) - -![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/cut-off-trees-for-golf.webp) - -![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/parenthesis-check.webp) - -Live Examples - - -## API docs - - -[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples)) - -Examples Repository # Why diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index d3a7977..4c9fc89 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -140,44 +140,6 @@ export abstract class AbstractBinaryTreeNode = AbstractBinaryTreeNode> implements IAbstractBinaryTree { @@ -273,6 +235,32 @@ export abstract class AbstractBinaryTree = AVLTreeNodeNested> extends BSTNode implements IAVLTreeNode { - override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { - return new AVLTreeNode(id, val, count) as FAMILY; - } } export class AVLTree = AVLTreeNode> extends BST implements IAVLTree { diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index a006695..c55d832 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -13,20 +13,6 @@ import {IBinaryTree, IBinaryTreeNode} from '../interfaces/binary-tree'; export class BinaryTreeNode = BinaryTreeNodeNested> extends AbstractBinaryTreeNode implements IBinaryTreeNode { - /** - * The function creates a new binary tree node with an optional value and count, and returns it as a specified type. - * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type - * `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes. - * @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the - * value stored in the node. - * @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value - * appears in the binary tree node. - * @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type. - */ - createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { - return new BinaryTreeNode(id, val, count) as FAMILY; - } - } export class BinaryTree = BinaryTreeNode> extends AbstractBinaryTree implements IBinaryTree { diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 84dd1d6..9892391 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -11,19 +11,6 @@ import {BinaryTree, BinaryTreeNode} from './binary-tree'; import {IBST, IBSTNode} from '../interfaces'; export class BSTNode = BSTNodeNested> extends BinaryTreeNode implements IBSTNode { - /** - * The function creates a new binary search tree node with the specified id, value, and count. - * @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely - * identify each node in the tree. - * @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an - * optional parameter, meaning it can be omitted when calling the "createNode" function. - * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary - * search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method. - * @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type. - */ - override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { - return new BSTNode(id, val, count) as FAMILY; - } } export class BST = BSTNode> extends BinaryTree implements IBST { diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index d384c58..9d04a87 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -18,19 +18,6 @@ export class RBTreeNode = RBTreeNo this._color = value; } - /** - * The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object. - * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely - * identify each node in the tree. - * @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T - * (generic type) or null. - * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree - * node. - * @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type. - */ - override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { - return new RBTreeNode(id, val, count) as FAMILY; - } // private override _parent: RBNode | null; // override set parent(v: RBNode | null) { diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 7fceb64..a7d98e2 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -10,19 +10,6 @@ import {ITreeMultiset, ITreeMultisetNode} from '../interfaces'; import {AVLTree, AVLTreeNode} from './avl-tree'; export class TreeMultisetNode = TreeMultisetNodeNested> extends AVLTreeNode implements ITreeMultisetNode { - /** - * The function creates a new node in a binary tree with an optional value and count. - * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely - * identify each node in the tree. - * @param {T} [val] - The `val` parameter represents the value to be stored in the node. It is an optional parameter, - * meaning it can be omitted when calling the `createNode` method. - * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree - * node. It is an optional parameter, so it can be omitted when calling the `createNode` method. - * @returns The method is returning a new instance of the TreeMultisetNode class, casted as the FAMILY type. - */ - override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { - return new TreeMultisetNode(id, val, count) as FAMILY; - } } /** diff --git a/src/data-structures/interfaces/abstract-binary-tree.ts b/src/data-structures/interfaces/abstract-binary-tree.ts index 391b1bd..62315f7 100644 --- a/src/data-structures/interfaces/abstract-binary-tree.ts +++ b/src/data-structures/interfaces/abstract-binary-tree.ts @@ -13,8 +13,6 @@ import {AbstractBinaryTreeNode} from '../binary-tree'; export interface IAbstractBinaryTreeNode> { - createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY; - get id(): BinaryTreeNodeId set id(v: BinaryTreeNodeId) @@ -44,10 +42,6 @@ export interface IAbstractBinaryTreeNode> { @@ -77,6 +71,8 @@ export interface IAbstractBinaryTree> extends IBinaryTreeNode { - createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY } export interface IBST> extends IBinaryTree { diff --git a/src/data-structures/interfaces/rb-tree.ts b/src/data-structures/interfaces/rb-tree.ts index 35db390..afb7559 100644 --- a/src/data-structures/interfaces/rb-tree.ts +++ b/src/data-structures/interfaces/rb-tree.ts @@ -3,7 +3,6 @@ import {IBST, IBSTNode} from './bst'; import {BinaryTreeNodeId} from '../types'; export interface IRBTreeNode> extends IBSTNode { - createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY } export interface IRBTree> extends IBST {