https://github.com/binarymax/jstrees
Javascript tree data structures
https://github.com/binarymax/jstrees
Last synced: 6 months ago
JSON representation
Javascript tree data structures
- Host: GitHub
- URL: https://github.com/binarymax/jstrees
- Owner: binarymax
- License: mit
- Created: 2011-11-05T14:36:40.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2015-11-14T12:10:13.000Z (about 10 years ago)
- Last Synced: 2024-04-15T12:19:44.297Z (almost 2 years ago)
- Language: JavaScript
- Homepage: http://binarymax.com/tree.html
- Size: 121 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#jsTrees
Javascript Tree-themed algorithms
##Tree Generator Example
Tree generator uses the tree API and accompanying settings.js to let the user generate simple trees on an html canvas.
A live example is here: http://www.binarymax.com/tree.html
##tree.js API
### makeNode : Creates a root node.
All nodes (including root) have the following prototype functions:
* addChild(obj) : Converts an object into a node, and appends it as a child.
* traverse(callback) : Traverses the tree, leaf nodes first down to root, and executes a callback for each.
* traverse2(callback) : Traverses the tree, root first up to leaf nodes, and executes a callback for each.
### arrayToTree : Converts a flat array to a tree data structure
###Sample input:
```
var ary = [
{"Level":1,"Name":"a"},
{"Level":2,"Name":"b"},
{"Level":2,"Name":"c"},
{"Level":3,"Name":"d"},
{"Level":3,"Name":"e"},
{"Level":4,"Name":"f"},
{"Level":2,"Name":"g"},
{"Level":1,"Name":"h"},
{"Level":2,"Name":"i"},
{"Level":3,"Name":"j"},
{"Level":4,"Name":"k"},
{"Level":3,"Name":"l"},
{"Level":1,"Name":"m"},
{"Level":2,"Name":"n"}
];
var tree = Tree.arrayToTree(ary,function(obj){ return parseInt(obj.Level)||0; });
var str = "";
tree.traverse(function(obj){
str+="\n";
for(var i=0;i