ES2015 Class versions of a Node and Tree
npm install structz
or yarn add structz
ES Module
import { Node, Tree } from 'structz';
CJS
const Structz = require('structz'),
Node = Structz.Node,
Tree = Structz.Tree;
After importing the library, you can create as many nodes as you'd like.
let rootSoilNode = new Node('soil');
let fertilizerChildNode = new Node('fertilizer');
let waterChildNode = new Node('h2o');
The new Node()
constructor accepts an argument of any Javascript type.
You can append child nodes to a root node like so:
rootSoilNode.append(fertilizerChildNode);
rootSoilNode.append(waterChildNode);
You can detect if a Node
is a root or not as well.
rootSoilNode.isRoot(); // -> true
waterChildNode.isRoot(); // -> false
Accessing child nodes is simple.
rootSoilNode.children // returns [fertilizerChildNode, waterChildNode]
You can perform immutable operations on the Node
data structure using the Tree
class. You instantiate it with a root Node.
let rootNode = new Node(1);
rootNode.append(new Node(2));
let newRootNode = Tree.map(rootNode, (node) => {
return node + 10;
}); // this returns a `new Tree()` with the newly created `Node` as the `.root` value.
newRootNode.root // returns Node of modified type
// in this case, our new structure looks like:
[11] (root)
|
[12] (child)
See docs/index.html
for up to date documentation.
Class representing a Node data structure type
Kind: global class
- Node
- .children ⇒
Array
- .value ⇒
*
- .value
- .isRoot() ⇒
boolean
- .hasChildren() ⇒
boolean
- .append(child) ⇒
Node
- .toString() ⇒
string
- .children ⇒
Returns an array of child nodes.
Kind: instance property of Node
Returns the value of the this.attributes.
Kind: instance property of Node
Sets the value of this.attributes.
Kind: instance property of Node
Param |
---|
attribute |
Detects if this Node is a parent node or not.
Kind: instance method of Node
Detects if this node has children or not.
Kind: instance method of Node
node.append(child) ⇒ Node
Appends a child Node to this Node.
Kind: instance method of Node
Param |
---|
child |
Returns a String that represents this Node.
Kind: instance method of Node
- Tree
Class representing a Tree data structure type that contains a Root node
Class representing a Tree data structure type that contains a Root node
Kind: global class
- Tree
- new Tree(root)
- instance
- .root ⇒
*
- .root ⇒
- static
Instantiates a Node as a Binary Tree
Param |
---|
root |
Returns the root of the Node.
Kind: instance property of Tree
Performs a function on a Node with the provided function and returns the output. Optionally accepts a Tree to perform this otherwise a new Tree is instantiated and returned (breaks immutability).
Kind: static method of Tree
Param | Default |
---|---|
node | |
func | |
tree |
|
> structz@1.0.0 test ~/github/structz
> nyc mocha
A Node data structure
✓ Can instantiate a new Node
✓ Returns [] children if empty
✓ Returns [] children if not empty
✓ successfully can append a child
✓ returns false if there are no children
✓ returns true if there are children
✓ can return its own value
✓ can set its own value
✓ can output itself as a String
A Tree data structure type that contains a Root node data structure
✓ can instantiate itself
✓ returns the root Node
✓ maps over a Node
12 passing (12ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 88.46 | 71.43 | 91.67 | 92 | |
Node.js | 80 | 0 | 87.5 | 85.71 | 23,24 |
Tree.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
- 1.0.0 - Preparing initial release, Updating to support CJS + ES Modules
- 0.2.4 - Updated JSDoc Generation
- 0.2.3 - Updated README with starter guide
- 0.2.2 - Docs updated
- 0.2.1 - Corrected bug with exports
- 0.2.0 - Changed package structure for modularity
- 0.1.1 - Added Test Coverage
- 0.1.0 - Initial release.