0.1.0 • Published 8 years ago

lp-tree-node v0.1.0

Weekly downloads
5
License
ISC
Repository
github
Last release
8 years ago

lp-tree-node

A node structure extends from d3-node

APIs

append (node: Node) => this: Node

Append a new Node or SubTree to this

var Node = require('lp-tree-node');
var data1 = [
  {"name": "Eve"},
  {"name": "Cain",  "parent": "Eve"},
  {"name": "Seth",  "parent": "Eve"},
  {"name": "Enos",  "parent": "Seth"},
  {"name": "Noam",  "parent": "Seth"},
  {"name": "Abel",  "parent": "Eve"},
  {"name": "Awan",  "parent": "Eve"},
  {"name": "Enoch", "parent": "Awan"},
  {"name": "Azura", "parent": "Eve"}
];
var data2 = [
  {"id": "Evan"},
  {"id": "Yoo", "parentId": "Evan"},
  {"id": "Yuka", "parentId": "Evan"}
];
var rootTree = Node.createTree()
	.id(d => d.name)	// define how to get [id] from data (default data.id)
	.parentId(d => d.parent)	// define how to get [parentId] from data (default data.parentId)
	.create(data1);		// bind data

var subTree = Node.createTree()
	.create(data2);

var targetNode = rootTree.findBefore(d => d.id === 'Azura');	// get the parent node
targetNode.append(subTree);	// append the [subTree] to [targetNode]

Notice:

  • After the operation, all nodes in the root tree will update. (renew height, depth, value fields)

remove () => this: Node

Remove a Node from its parent

findBefore (exp: (node: Node) => boolean): Node

Get the first Node which make exp to be true in pre-order traversal

findAfter (exp: (node: Node) => boolean): Node

Get the first Node which make exp to be true in post-order traversal