task-three-tree-lib
v1.0.2
Published
This is a simple tree library made for the purpose of an assignment. It is written in typescript and contains main functionality of trees.
Downloads
2
Readme
task-three-tree-lib
This package is meant to provide a support for trees in typescript. To get started:
npm i task-three-tree-lib
import * as Tree from 'task-three-tree-lib';
- The Tree type is made up of disjoint union classes - Leaf and Branch.
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;
}
}
The library consists of following functionality:
- Type Guards - isLeaf() and isBranch()
- size() - counts total number of branches and leaves.
- max() - Finds the max number in a tree of numbers.
- depth() - Finds the longest path from root to leaf.
- map() - Maps the leafs of a tree of type A to a tree of type B using a user supplied function.
- filter() - Filters out tree leaves based on filter condition provided as a function.
- zip() - combines elements of two trees if they are on the same level, omits doing so otherwise.
- Please follow this link to find the source code.