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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@antrikshsaini/tree-npm

v1.1.0

Published

Tree package for practice

Readme

Tree npm package

Installation

use command npm install tree-npm-lib to install package

Some Explanation

                      A Tree

                      Branch
                  ┌─────┬─────┐
         ┌────────┼──●  │  ●──┼───────┐
         │        └─────┴─────┘       │
         ↓         left  right        ↓
       Branch                       Branch

Type Declaration

type Tree<A> = Leaf<A> | Branch<A>;

class Leaf<A> {
    tag: 'leaf' = 'leaf';
    readonly value: A;

    constructor(value: A) {
        this.value = value;
    }
}

class Branch<A> {
    tag: 'branch' = 'branch';
    readonly left: Tree<A>;
    readonly right: Tree<A>;

    constructor(left: Tree<A>, right: Tree<A>) {
        this.left = left;
        this.right = right;
    }
}

class BranchOptional<A> {
    tag: 'branchOptional' = 'branchOptional';
    readonly left?: TreeOptional<A>;
    readonly right?: TreeOptional<A>;

    constructor(option: { left?: TreeOptional<A>, right?: TreeOptional<A> }) {
        this.left = option.left;
        this.right = option.right;
    }
}

type TreeOptional<A> = Leaf<A> | BranchOptional<A>

const isBranchOptional = <A>(t: TreeOptional<A>): t is BranchOptional<A> => {
    return t.tag === 'branchOptional';
};

Type Guards


const isLeaf = <A>(t: Tree<A>): t is Leaf<A> => {
    return t.tag === 'leaf';
};
const isBranch = <A>(t: Tree<A>): t is Branch<A> => {
    return t.tag === 'branch';
};

Some Cool Functions

Size

Signature-> size: Tree -> number

Find size of Tree, Total of number of leaf and branch

const tree: Tree<number> = {
  tag: 'branch',
  left: {tag: 'leaf', value: 8},
  right: {
    tag: 'branch',
    left: {tag: 'leaf', value: 5},
    right: {tag: 'leaf', value: 9},
  },
};
console.log(size(tree)); // 3

Max

Signature-> max: Tree -> number

In a Tree of type number, find Maximum leaf value

console.log(max(tree)); //9

Depth

Signature-> depth: Tree -> number

In a Tree, find the maximum path length from the root node to any leaf

console.log(depth(tree)); // 2

Map

Signature-> map: ((A)-> B) -> Tree -> Tree

Creates new Tree, after applying a function to each element in the tree

const tree: Tree<number> = {
  tag: 'branch',
  left: {tag: 'leaf', value: 2},
  right: {
    tag: 'branch',
    left: {tag: 'leaf', value: 3},
    right: {tag: 'leaf', value: 4},
  },
};
console.log(map((i) => i + 1, tree));

console.log(mapCurry((i: number) => i + 1)(tree));

//Output
Branch { tag: 'branch',
    left: Leaf { tag: 'leaf', value: 3 },
    right: Branch {
        tag: 'branch',
        left: Leaf { tag: 'leaf', value: 4 },
        right: Leaf { tag: 'leaf', value: 5 }
        }
    }

Filter

Signature-> filter : ((Tree)=> boolean) => Tree | undefined => Tree | undefined

Creates a tree or return undefined, based on the condition (basically remove leaf node)

const tree: Tree<number> = {
    tag: 'branch',
    left: { tag: 'leaf', value: 2 },
    right: {
        tag: 'branch',
        left: { tag: 'leaf', value: 5 },
        right: { tag: 'leaf', value: 4 },
    },
};
console.log(filter((i) => i === 5, tree))

//Output
Branch {
    tag: 'branch',
    left: Leaf { tag: 'leaf', value: 2 },
    right:
     Branch {
       tag: 'branch',
       left: undefined,
       right: Leaf { tag: 'leaf', value: 4 } } 
       }

Zip

Signature-> zip: Tree -> Tree -> Tree<Array<A|B>> | undefined

creates a new tree out of two supplied trees by pairing up equally-positioned items from both trees. Both trees should be of same structure otherwise it would be undefined

const tree: Tree<number> = {
    tag: 'branch',
    left: { tag: 'leaf', value: 2 },
    right: {
        tag: 'branch',
        left: { tag: 'leaf', value: 5 },
        right: { tag: 'leaf', value: 4 },
    },
};

console.log(JSON.stringify(zip(tree, tree), null, 2));

// Output
{
  "tag": "branch",
  "left": {
    "tag": "leaf",
    "value": [2,2]
  },
  "right": {
    "tag": "branch",
    "left": {
      "tag": "leaf",
      "value": [5,5]
    },
    "right": {
      "tag": "leaf",
      "value": [4,4]
    }
  }
}