{"id":18356495,"url":"https://github.com/lbwa/n-ary","last_synced_at":"2025-04-06T12:32:16.467Z","repository":{"id":56325655,"uuid":"304364540","full_name":"lbwa/n-ary","owner":"lbwa","description":"n-ary tree(also known as k-ary or k-way tree) implementation in JavaScript(TypeScript).","archived":false,"fork":false,"pushed_at":"2021-05-18T16:01:42.000Z","size":320,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-15T00:09:08.098Z","etag":null,"topics":["algorithms","data-structures","n-ary","n-ary-tree","tree"],"latest_commit_sha":null,"homepage":"https://github.com/lbwa/n-ary","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lbwa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://paypal.me/typeof"]}},"created_at":"2020-10-15T15:11:20.000Z","updated_at":"2023-03-08T15:03:49.000Z","dependencies_parsed_at":"2022-08-15T16:40:42.524Z","dependency_job_id":null,"html_url":"https://github.com/lbwa/n-ary","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbwa%2Fn-ary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbwa%2Fn-ary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbwa%2Fn-ary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbwa%2Fn-ary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lbwa","download_url":"https://codeload.github.com/lbwa/n-ary/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247484474,"owners_count":20946388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","data-structures","n-ary","n-ary-tree","tree"],"created_at":"2024-11-05T22:10:26.699Z","updated_at":"2025-04-06T12:32:15.918Z","avatar_url":"https://github.com/lbwa.png","language":"TypeScript","funding_links":["https://paypal.me/typeof"],"categories":[],"sub_categories":[],"readme":"# N ary tree\n\n[n-ary tree](https://en.wikipedia.org/wiki/M-ary_tree)(also known as `k-ary` or `k-way` tree) implementation in `JavaScript(TypeScript)`.\n\n![Pipeline](https://github.com/lbwa/n-ary/workflows/Pipeline/badge.svg) \u003cimg alt=\"npm (tag)\" src=\"https://img.shields.io/npm/v/n-ary-tree/latest?style=flat-square\"\u003e \u003cimg alt=\"npm bundle size\" src=\"https://img.shields.io/bundlephobia/minzip/n-ary-tree?style=flat-square\"\u003e\n\n\u003c!-- TOC --\u003e\n\n- [N ary tree](#n-ary-tree)\n  - [Installation](#installation)\n  - [Tree node structures](#tree-node-structures)\n    - [Default structure fields](#default-structure-fields)\n    - [Examples](#examples)\n  - [APIs](#apis)\n    - [findNodes](#findnodes)\n    - [findPathNodes](#findpathnodes)\n    - [findPath](#findpath)\n    - [findAllPaths](#findallpaths)\n    - [Traversal](#traversal)\n      - [levelorder](#levelorder)\n      - [preorder](#preorder)\n      - [postorder](#postorder)\n  - [License](#license)\n\n\u003c!-- /TOC --\u003e\n\n## Installation\n\n- Using npm\n\n```bash\n$ npm i n-ary-tree\n```\n\n- Using yarn\n\n```bash\n$ yarn add n-ary-tree\n```\n\n## Tree node structures\n\nUser-defined tree node fields have supported by all available methods. Just keep the last parameter is a structure like:\n\n```ts\ntype TreeNodeFields\u003cN\u003e = Partial\u003c{\n  value: keyof N\n  children: keyof N\n}\u003e\n```\n\n### Default structure fields\n\n```ts\n{\n  value: 'value',\n  children: 'children'\n}\n```\n\nBy default, we will use `value` field as the value of tree node, `children` field as the children of tree node.\n\n### Examples\n\n```ts\n{\n  value: 'val', // or other field string in the tree node\n  children: 'descendants' // or other field string in the tree node\n}\n```\n\n`val` field will be regarded as node actual value, `descendants` field in the tree node will be regraded as node children field.\n\n## APIs\n\n### findNodes\n\nGet all matched nodes.\n\n```ts\nexport function findNodes\u003cN extends Record\u003cstring, any\u003e, V = any\u003e(\n  root: N,\n  targets: V[],\n  fields?: TreeNodeFields\u003cN\u003e\n): FindNodesResult\u003cN, V\u003e\n```\n\n```ts\nimport { findNodes } from 'n-ary-tree'\n\ninterface DefaultTreeNode {\n  value: number\n  children?: DefaultTreeNode[]\n}\n\nconst tree: DefaultTreeNode = {\n  value: 1,\n  children: [\n    {\n      value: 11,\n      children: [\n        {\n          value: 111\n        }\n      ]\n    },\n    {\n      value: 12,\n      children: [\n        {\n          value: 121\n        }\n      ]\n    }\n  ]\n}\n\nfindNodes(tree, [111])\n// -\u003e {\n//      nodes: [{ value: 111 }],\n//      values: [111]\n//    }\nfindNodes(tree, [111], { value: 'value', children: 'children' })\n// Equivalent to findNodes(tree, [111]), { value: 'value', children: 'children' }\n// is default preset.\n```\n\n### findPathNodes\n\nGet nodes sequences if tree path exist.\n\n```ts\nimport { findPathNodes } from 'n-ary-tree'\n\nfindPathNodes(tree, [1, 11, 111])\n/**\n * [\n *   { value: 1, children: ... },\n *   { value: 11, children: ... },\n *   { value: 111, children: ...}\n * ]\n */\n\nfindPathNodes(tree, [1, 9])\n/**\n * [\n *   { value: 1, children: ... }\n * ]\n */\n```\n\n### findPath\n\nFind the latest matched path.\n\n```ts\nimport { findPath } from 'n-ary-tree'\n\nfindPath(tree, 121)\n/**\n * [\n *   { value: 1, children: ... },\n *   { value: 12, children: ... },\n *   { value: 121, children: ...}\n * ]\n */\nfindPath(tree, 22) // []\n```\n\n### findAllPaths\n\nFind all matched paths.\n\n```ts\nimport { findAllPaths } from 'n-ary-tree'\n\nconst tree = {\n  value: 1,\n  children: [\n    {\n      value: 111\n    },\n    {\n      value: 11,\n      children: [\n        {\n          value: 111\n        }\n      ]\n    },\n    {\n      value: 12,\n      children: [\n        {\n          value: 121\n        }\n      ]\n    }\n  ]\n}\n\nfindAllPaths(tree, [111])\n/**\n * [\n *    [\n *      { value: 1, children: ... },\n *      { value: 111, children: ... },\n *    ],\n *    [\n *      { value: 1, children: ... },\n *      { value: 11, children: ... },\n *      { value: 111, children: ...}\n *    ]\n * ]\n */\n```\n\n### Traversal\n\nWe have support 3 kinds of common tree traversal methods: [level-order(BFS)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search_/_level_order), [pre-order(DFS)](\u003chttps://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)\u003e), [post-order(DFS)](\u003chttps://en.wikipedia.org/wiki/Tree_traversal#Post-order_(LRN)\u003e).\n\n#### levelorder\n\n```ts\nconst tree = {\n  val: 1,\n  children: [\n    {\n      val: 2\n    }\n  ]\n}\nlevelorder(tree, { value: 'val' })\n// [[1], [2]]\n```\n\n#### preorder\n\n```ts\nconst tree = {\n  value: 1,\n  descendants: [\n    {\n      value: 2\n    }\n  ]\n}\npreorder(tree, { children: 'descendants' })\n// [1, 2]\n```\n\n#### postorder\n\n```ts\nconst tree = {\n  val: 1,\n  descendants: [\n    {\n      val: 2\n    }\n  ]\n}\npostorder(tree, { value: 'val', children: 'descendants' })\n// [2, 1]\n```\n\n## License\n\n[MIT](./LICENSE) © [Liu Bowen](https://github.com/lbwa)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbwa%2Fn-ary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flbwa%2Fn-ary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbwa%2Fn-ary/lists"}