A Binary Tree Module implementation for add, delete, order and search nodes with any content
npm install tbin-treesh
npm install tbin-tree --save
`
Usage
$3
#### Javascript ES5 Example
`javascript
var BinaryTree = require("tbin-tree").BinaryTree;
var myBinaryTree = new BinaryTree();
// First param the value (Number) , and second optional argument T the content.
myBinaryTree.insert(5, "Hello World!");
myBinaryTree.insert(15, "Nel");
myBinaryTree.insert(2, "Bye World!");
var myNode = myBinaryTree.search(15);
myNode.getContent(); // => { value: 15, content:"Nel"}
// Without arguments the default is showBy="Value"
myBinaryTree.inorder({ showBy: "Content" });
/**
* [
* { value: 2, content: 'Bye World!' },
* { value: 5, content: 'Hello World!' },
* { value: 15, content: 'Nel'}
* ]
*/
`
#### ES6 | TS Example
`typescript
import { BinaryTree, Node } from "tbin-tree";
const myBinaryTree = new BinaryTree();
myBinaryTree.insert(5, "Hello World!");
myBinaryTree.insert(15, "Nel");
myBinaryTree.insert(2, "Bye World!");
const myNode: Node = myBinaryTree.search(15);
myNode.getContent();
`
Documentation
$3
`javascript
const { BinaryTree } = require("tbin-tree");
`
$3
We could initialize the Binary Tree by with two optional arguments to initialize the root Node:
- value : _Number_ (The unique identifier of a Node in base of this value the Binary Tree is ordered)
- content : _T_ (Any value that we want to store in the root Node)
`javascript
const { BinaryTree } = require("tbin-tree");
const myBinaryTree = new BinaryTree(5, "foo");
const mySecondaryBinaryTree = new BinaryTree();
`
$3
We must use the first argument the value, the content is optional by default is null, repeating a value will throw an Error
`javascript
myBinaryTree.insert(11);
myBinaryTree.insert(13, "Im the Content :D!");
`
$3
Takes the target value to delete the node, reorganize the Binary Tree if we remove a Node with childs.
`javascript
myBinaryTree.remove(11);
`
$3
Takes the target value and returns the nodes if match, if there is no Node return null
`javascript
myBinaryTree.search(11); // => Node
`
$3
The class has 3 ways of traversing the Binary Tree.
- Inorder
- PostOrder
- Preorder
It returns an array of Nodes, its content or value.
All takes an optional object argument, with the key = showBy='Value' as default.
Also can show by: "Content", "Node"
`javascript
myBinaryTree.inorder({ showBy: "Content" });
/**
* [
* { value: 2, content: 'Bye World!' },
* { value: 5, content: 'Hello World!' },
* { value: 15, content: 'Nel' }
* ]
*/
myBinaryTree.inorder();
// [2,5,15]
``