Skip to content

Latest commit

 

History

History
85 lines (53 loc) · 1.89 KB

File metadata and controls

85 lines (53 loc) · 1.89 KB

Note

This is an outdated, experiment project, from my early coding days.
Have improved significantly, since then.
(https://xiggfi.web.app)


blue tree

v0.7

Tree data structure in javascript.
This implementation is based on closures.
A prototype based implementation is (https://github.com/lignx/peas.js)

Tested with basement tests (tests in the browser. Click the "test" button.)

Documentation

Nodes are create with:

var new_node = tree.node( item )

The item parameter, is the node payload, assigned to node.item

A list of properties and methods of nodes objects is provided:

nodes properties

  • node.top parent node

  • node.next next sibling or null

  • node.prev previous sibling or null

  • node.item Anything that is assigned as the node payload

  • node.sub.n number of childs

  • node.sub.first first child or null

  • node.sub.last last child or null

nodes methods

  • node.sub.add( subnode ) adds subnode as last child of node, then returns subnode

  • node.sub.at( index ) returns the child at index position (zero index)

  • node.sub.insert( subnode, i ) Insert subnode as i (zero index) child of node. Returns subnode.

  • node.rip() Pulls node object from its tree, and returns it (node). Other nodes of the tree are left in consistent state.

  • node.walk( action ) Calls "action" function on all the nodes below "node". Descend recursively on subnodes of subnodes. On each case, the function will get as first and only parameter, the current node that is acting upon.

  • node.sub.each( action ) Calls "action" function on all the direct subnodes of "node", only the first level of depth. No recursion. On each case, the function will get as first and only parameter, the current node that is acting upon.

  • node.flat() Returns an array with references to all nodes in the tree