My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Tree  

Featured
Updated Jun 20, 2010 by maxkugland@gmail.com

I often work with tree structures. The tree package provides a nice infrastructure for that.

	var t : Tree = new Tree(new Node("root"));
	t.root.add(new Node("child-1"));
	t.root.add(new Node("child-2")).add(new Node("child-2-1"));
	t.root.add(new Node("child-3"));

Any INode can take an optional data object. This enables to build tree structures for any kind of object.

	trace(new Node("child-1", {name:"something"}).data.something);

The Tree class contains a couple of static helper methods which provide interesting opportunities.

Create a Node Tree from xml:

	var xml : XML = <t>
				<n id='1'>
					<n id='4' />
					<n id='5'>
						<n id='7' />
					</n>
				</n>
			</t>;
	
	var tree Tree = Tree.fromXml(new Tree(null), xml, Node);

Clone a Tree:

	var clone : Tree = new Tree(Tree.clone(tree.root));

Visit all nodes of a Tree:

	Tree.visit(tree.root, function(n : INode) : void {
		trace("hello: " + n.id + " data: " + n.data);
	});

Retrieve any INode by it's id:

	var node : INode = Tree.getNodeById("5", tree.root);

Sign in to add a comment
Powered by Google Project Hosting