npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trees-array-based

v2.3.1

Published

A trees datastructure based on arrays

Downloads

3

Readme

A trees datastructure based on arrays

The objective of this module is to provide a basic and generic Tree data structure and some of its derivatives.

This module is made using common js.

You can use this module in the following environments:

  • browser
  • node commonjs
  • es6

What you get from this module

  • This package has three main object classes for now.
  • There are two different tree datastructures implemented.

TreeNode

The nodes of the tree. You can use this class to create your own Tree structure based on arrays. Its also possible to use objects of this class in the Tree datastructure.

The complexity of dealing with this class is mostly abstracted.

Tree

A generic Tree structure. This is the more typical type of trees.

NameHierarchicalTree

A Tree data structure that provides a hierarchy to the way that nodes are inserted based on the name of the nodes. Example: "father" "father.child" Here father.child is the child of the father and will be inserted as a child of the father node. This Tree does'nt accepts repeated names.

How to import

CommonJS

const TreeNode = require("trees-array-based").TreeNode;
const Tree = require("trees-array-based").Tree;
const NameHierarchicalTree = require("trees-array-based").NameHierarchicalTree;

ES6 module syntax

import {TreeNode, Tree, NameHierarchicalTree} from "trees-array-based"

Browser import the min.js file.

<script type="text/javascript" src="trees-array-based.min.js"> 

How to use

TreeNode

TreeNode is a perfectly independent class that can be use to create your own datastructure. TreeNode has mainly tree things:

  • Name: It identifies the node. Depending on the Tree datastructure can be repeated or not.
  • Value: This is simply the value on the node. It can have any value, undefined and null included.
  • Children: The children of this node. Unless you are using TreeNode directly you won't have contact with this.

Methods:

  • addChild(treeNodeChildren)
  • removeChild(object)
  • getChildren(name)
  • getChild(name)
  • setValue(value)
  • getValue()
  • isEqual()
  • setParent(parent)
  • getParent()
  • setName(name)
  • getName()
  • hasChildren(node)

Example:

const TreeNode = require("trees-array-based").TreeNode;

let node1 = new TreeNode("testValue1");
let node2 = new TreeNode("testValue2",1);
let node3 = new TreeNode("testValue3",[1,2,3]);
let node4 = new TreeNode("testValue4");

node1.addChild(node2);
node2.addChild(node3);
node3.addChild(node4);

node2.removeChild(node3);

Tree

Tree is the most generic part of this module. It abstracts the work with TreeNodes. Nodes can be repeated.

Methods:

  • addChild(object, nodeParent, value)
  • removeChild(object)
  • getChildrenOf(object)
  • getNode(object)
  • getValue(nodeName)
  • isEqual(nodeOne, nodeTwo)

You can use directly TreeNode objects.

const Tree = require("trees-array-based").Tree;
const TreeNode = require("trees-array-based").TreeNode;

let tree = new Tree();

let node1 = new TreeNode("testValue1");
let node2 = new TreeNode("testValue2");
let node3 = new TreeNode("testValue3");
let node4 = new TreeNode("testValue4");

tree.addChild(node1);
tree.addChild(node2);

tree.addChild(node3, node2);
tree.addChild(node4, node2);
tree.addChild(node4, node3);

tree.removeChild(node3);
tree.removeChild("testValue4");

Is also posible to create the children using addChild without importing the TreeNode class. Using names.

const Tree = require("trees-array-based").Tree;
let tree = new Tree();

tree.addChild("node1");
tree.addChild("node2");

tree.addChild("node3", "node2");
tree.addChild("node4", "node2");
tree.addChild("node4", "node3");

tree.removeChild("node3");
tree.removeChild("testValue4");

Avoid using both aproaches as it has not been tested.

NameHierarchicalTree

NameHierarchicalTree creates a tree based on a hierarchical name structure using a separator of choice. The default separators are dots.

Example

const NameHierarchicalTree = require("trees-array-based").NameHierarchicalTree;
let nameTree = new NameHierarchicalTree("@");

nameTree.addChild("parent");
nameTree.addChild("parent@child1");

Example

parent

This are the children:

parent.child1 parent.child2

Methods:

  • addChild(childrenName, value)
  • removeChild(childrenName)
  • getChildrenOf(childrenName)
  • getNode(nodeName)
  • getValue(nodeName)
  • isEqual(nodeOne, nodeTwo)

Example:

const NameHierarchicalTree = require("trees-array-based").NameHierarchicalTree;

let nameTree = new NameHierarchicalTree();

nameTree.addChild("parent");
nameTree.addChild("parent.child1");
nameTree.addChild("parent.child2");

nameTree.removeChild("parent.child1");

nameTree.getChildrenOf("parent");

let treeNode = nameTree.getNode("parent");