@mishieck/graph-js
v0.4.0
Published
A TypeScript library for graph data structures.
Maintainers
Readme
GraphJs
A TypeScript library for graph data structures.
Features
Data Structures
The data structures available include:
Traversal
The nodes support the following traversal algorithms:
Installation
NPM
npm install @mishieck/graph-jsBun
bun install @mishieck/graph-jsUsage
import { GraphNode, Traversal } from "@mishieck/graph-js";
const root = new GraphNode(1, []);
const childLeft = new GraphNode(2, []);
const childRight = new GraphNode(3, []);
const grandChildLeftLeft = new GraphNode(4, []);
const grandChildLeftRight = new GraphNode(5, []);
const grandChildRightLeft = new GraphNode(6, []);
const grandChildRightRight = new GraphNode(7, []);
childLeft.neighbors = [grandChildLeftLeft, grandChildLeftRight];
childRight.neighbors = [grandChildRightLeft, grandChildRightRight];
root.neighbors = [childLeft, childRight];
let data = [...root.traverse(Traversal.LevelOrder)].map(({ data }) => data);
console.log(data); // [1, 2, 3, 4, 5, 6, 7]
data = [...root.traverse(Traversal.PostOrder)].map(({ data }) => data);
console.log(data); // [4, 5, 2, 6, 7, 3, 1]
data = [...root.traverse(Traversal.PreOrder)].map(({ data }) => data);
console.log(data); // [1, 2, 4, 5, 3, 6, 7]