Tree of Life
This package aims to make creating and handling data structures a breeze.
current data structures supported:
- Binary Trees
- Singly Linked lists
Version
1.1.0 - Added functionality for singly linked lists. 1.0.0 - Initial release only tree functionality implemented.
Install
npm i -s tol-1.1.0
Build
clone the repo, no builing is necessary currently.
requirements:
- Gulp-Cli
Usage
Tol has two primary functions currenty that allow users to create and maintain data structures.
CreateTree
const tol = require('tol');
//Creating a new tree.
cont myTree = tol.createTree();
//Creating a new branch. This functionality is still in the air.
myTree.createBranch(branch_id, sorting_function);
//set a branch to a new head
myTree.updateBranch(1, {data: 23, left: null, right: null});
const branch1 = myTree.getBranch(branch_id);
//set a custom node
const newNode = function(data, id){
this.data = data;
this.id = id;
this.left = null;
this.right = null;
}
//set branch to use new custom node, addNode will automatically handle new parameters
branch1.setCustomNode(newNode);
branch1.addNode(data, id);
//Adding a single node to a branch.
branch1.addNode(data);
//Adding multiple nodes to the same branch only accepts arrays currently.
branch1.addNodes([...array]);
CreateLinkedList
const tol = require('tol');
//create an unpopulated linked list;
myList = tol.createLinkedList();
//add a node to the list
myList.addNode(1);
//this function can be overloaded if a custom node is created.
//add multiple nodes to the list accepts arrays of items as inputs
myList.addNodes([1,2,4,5,6,7]);
//getter functions
myList.getHead(); //returns the current head node
myList.getTail(); //returns the current tail node
myList.getLength(); //returns the current length of the list
//The list has a built in function for inserting into the head location instead of the end
myList.insertHead(node); //takes a node as an argument
myList.setHead(node); //overwrites the current head.
//Replacing a node based in index is also taken care of
myList.replaceNode(node, index); //also takes a node object
//inserting a node at a specific index is also supported
myList.insertNode(node, index);
//deleting a node is also supported
myList.deleteNode(index);
//adding at the end of the list is also easy
//this can be used to create a circular linked list however addnode will nolonger work properly
//allows for overloading for custom nodes
myList.addToTail(data, ...rest);
//allows user to set custom node will fail if list has already had a node added to it.
const newNode = function(data, id){
return{
data,
id,
next: null;
}
}
myList.setCustomNode(newNode);
//this sets the customNode variable to true and sets the lists internal node to the node structure passed
//adding new node after setting custom node
myList.addNode(1, '23');
//all functions that require a node to be passed can now use the user defined node as such
myList.insertNode(new newNode(2, '23'), 1);