Giter VIP home page Giter VIP logo

n-ary's Introduction

N ary tree

n-ary tree(also known as k-ary or k-way tree) implementation in JavaScript(TypeScript).

Pipeline npm (tag) npm bundle size

Installation

  • Using npm
$ npm i n-ary-tree
  • Using yarn
$ yarn add n-ary-tree

Tree node structures

User-defined tree node fields have supported by all available methods. Just keep the last parameter is a structure like:

type TreeNodeFields<N> = Partial<{
  value: keyof N
  children: keyof N
}>

Default structure fields

{
  value: 'value',
  children: 'children'
}

By default, we will use value field as the value of tree node, children field as the children of tree node.

Examples

{
  value: 'val', // or other field string in the tree node
  children: 'descendants' // or other field string in the tree node
}

val field will be regarded as node actual value, descendants field in the tree node will be regraded as node children field.

APIs

findNodes

Get all matched nodes.

export function findNodes<N extends Record<string, any>, V = any>(
  root: N,
  targets: V[],
  fields?: TreeNodeFields<N>
): FindNodesResult<N, V>
import { findNodes } from 'n-ary-tree'

interface DefaultTreeNode {
  value: number
  children?: DefaultTreeNode[]
}

const tree: DefaultTreeNode = {
  value: 1,
  children: [
    {
      value: 11,
      children: [
        {
          value: 111
        }
      ]
    },
    {
      value: 12,
      children: [
        {
          value: 121
        }
      ]
    }
  ]
}

findNodes(tree, [111])
// -> {
//      nodes: [{ value: 111 }],
//      values: [111]
//    }
findNodes(tree, [111], { value: 'value', children: 'children' })
// Equivalent to findNodes(tree, [111]), { value: 'value', children: 'children' }
// is default preset.

findPathNodes

Get nodes sequences if tree path exist.

import { findPathNodes } from 'n-ary-tree'

findPathNodes(tree, [1, 11, 111])
/**
 * [
 *   { value: 1, children: ... },
 *   { value: 11, children: ... },
 *   { value: 111, children: ...}
 * ]
 */

findPathNodes(tree, [1, 9])
/**
 * [
 *   { value: 1, children: ... }
 * ]
 */

findPath

Find the latest matched path.

import { findPath } from 'n-ary-tree'

findPath(tree, 121)
/**
 * [
 *   { value: 1, children: ... },
 *   { value: 12, children: ... },
 *   { value: 121, children: ...}
 * ]
 */
findPath(tree, 22) // []

findAllPaths

Find all matched paths.

import { findAllPaths } from 'n-ary-tree'

const tree = {
  value: 1,
  children: [
    {
      value: 111
    },
    {
      value: 11,
      children: [
        {
          value: 111
        }
      ]
    },
    {
      value: 12,
      children: [
        {
          value: 121
        }
      ]
    }
  ]
}

findAllPaths(tree, [111])
/**
 * [
 *    [
 *      { value: 1, children: ... },
 *      { value: 111, children: ... },
 *    ],
 *    [
 *      { value: 1, children: ... },
 *      { value: 11, children: ... },
 *      { value: 111, children: ...}
 *    ]
 * ]
 */

Traversal

We have support 3 kinds of common tree traversal methods: level-order(BFS), pre-order(DFS), post-order(DFS).

levelorder

const tree = {
  val: 1,
  children: [
    {
      val: 2
    }
  ]
}
levelorder(tree, { value: 'val' })
// [[1], [2]]

preorder

const tree = {
  value: 1,
  descendants: [
    {
      value: 2
    }
  ]
}
preorder(tree, { children: 'descendants' })
// [1, 2]

postorder

const tree = {
  val: 1,
  descendants: [
    {
      val: 2
    }
  ]
}
postorder(tree, { value: 'val', children: 'descendants' })
// [2, 1]

License

MIT © Liu Bowen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.