Class to transform and manipulate tree like structures

Constructors

Methods

  • Method to add new node to tree (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node adding

    • parentId: any

      identifier of parent node, null if new node should be on root level

    • childData: any

      data of new node

    • Rest...anotherChildData: any[]

      data of new additional nodes

    Returns void

  • Method to delete node in tree by given id (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node deleting

    • id: any

      identifier of node to delete

    Returns any

  • Method to update node by id with given data in tree (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node editing

    • id: any

      identifier of node to be updated

    • data: any

      new data of node (you should also pass children if you want to keep it)

    Returns void

  • Method to find all nodes in tree structure by given callback function

    Parameters

    • tree: any[]

      tree structure to search in

    • fn: ((item: any) => boolean)

      callback function to find all nodes

        • (item): boolean
        • Parameters

          • item: any

          Returns boolean

    Returns any

    all found nodes

    utils.filter(tree, item => item.id === myId);
    
  • Method to find node in tree structure by given callback function

    Parameters

    • tree: any[]

      tree structure to search in

    • fn: ((item: any) => boolean)

      callback function to find node

        • (item): boolean
        • Parameters

          • item: any

          Returns boolean

    Returns any

    found node

    utils.find(tree, item => item.id === myId);
    
  • Method to get node in tree structure by given id

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    found node

  • Method to get ancestors of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    all found parent nodes

  • Method to get breath of subtree (the number of leaves in subtree)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    breath of subtree

  • Method to get degree of node (for a given node, its number of children. A leaf, by definition, has degree zero)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    degree of node

  • Method to get depth of node (the depth of a node is the length of the path to its root i.e., its root path)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    depth of node

  • Method to get descendant nodes of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    all found children nodes

  • Method to get distance between 2 nodes

    Parameters

    • tree: any[]

      tree structure

    • id1: any

      identifier of first node

    • id2: any

      identifier of second node

    Returns number

    distance between 2 nodes, returns -1, if there is no connection between nodes

  • Method to get height of node (the height of a node is the length of the longest downward path to a leaf from that node)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    height of node

  • Method to get children of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    children nodes of node

  • Method to get leafs of subtree from given node

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    leafs of nodes

  • Method to get level of node (the level of a node is the number of edges along the unique path between it and the root node)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    level of node

  • Method to get neighbours (neighbour is parent or child) of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    neighbours of node

  • Method to get nodes in tree at specific level

    Parameters

    • tree: any[]

      tree structure to search in

    • level: number

      desired level

    Returns any[]

    all nodes, that are on specific level

  • Method to get parent of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    found parent node, otherwise null

  • Method to get nodes that are part of path from root Alias for getAncestors method

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    all nodes that are part of path (ordered from root)

  • Method to get siblings of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    siblings of node

  • Method to get size of subtree (number of nodes in the subtree)

    Parameters

    • tree: any[]

      tree structure

    • id: any

      identifier of node

    Returns number

    size of subtree

  • Method to get subtree from given node (children of node) Alias for getChildren method

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    subtree

  • Method to get degree of tree (the degree of a tree is the maximum degree of a node in the tree)

    Parameters

    • tree: any[]

      tree structure

    Returns number

    degree of tree

  • Method to get width on level in tree (the number of nodes in a level)

    Parameters

    • tree: any[]

      tree structure

    • level: number

      desired level

    Returns number

    width on desired level

  • Convert list to tree like structure

    Parameters

    • list: any[]

      list of objects, objects need to have id (as you configured, or 'id' by default) and parentId property (as you configured, or 'parentId' by default)

    • rootParentId: any = null

      id of root parent nodes (if not specified, root nodes are nodes with parentId of null)

    Returns any[]

    tree structure

  • Convert tree like structure to list

    Parameters

    • tree: any[]

      tree of objects, objects need to have children (as you configured, or 'children' by default) and parentId property (as you configured, or 'parentId' by default)

    Returns any[]

    list