Tree::Easy - An easy, simple & fast N-ary tree datatype
Version 0.01
use Tree::Easy; my $foo = Tree::Easy->new(); $foo->data('foo'); $foo->push_new('bar'); my $bar = $foo->search('bar') or die q{Couldn't find bar!}; # You can also use references as data $bar->push_new(['baz']); my $baz = $foo->search( sub { eval { shift->data->[0] eq 'baz' }} ) or die q{Couldn't find baz!};
Tree::Easy is a very minimal tree class. It was designed to be easy to use and quick. It's one of the fastest pure-perl tree modules and easiest in my horribly biased opinion.
A Tree::Easy object is a blessed arrayref. This array contains the children of the node. You can modify it directly if you dare. These children are also Tree::Easy objects. Each node is therefore a tree if it has children making a recursive structure. The child node is completely unaware of its parent node.
Data is stored privately in a inside-out object fashion. Data can only be accessed using the node's data method.
Tree::Easy does not check to make sure you do not make an invalid tree. You can make cycles and have one node appear twice in the tree. This violates the concept of a tree. I might add a simple check later if the need arises.
Arguments given in brackets are optional.
my $tree = Tree::Easy->new(); my $tree = Tree::Easy->new( 'root' );
Creates a new tree or node depending on your point of view. Stores data into the new node if supplied any.
my $data = $tree->data; $tree->data( 'foo' ); $tree->data( [ 'foo' ] ); $tree->data( { foo => 'bar' } );
Retrieves or stores data assigned to the node.
my $tree_copy = $tree->clone;
Creates a copy of the given tree. This also creates a shallow copy of every node's data (only one level deep).
my $node = Tree::Easy->new('foo'); $tree->insert_node($node)
$new_node = $tree->push_new('right');
$new_node = $tree->unshift_new('left');
$new_node = $tree->push_node( Tree::Easy->new('right') );
$new_node = $tree->push_node( Tree::Easy->new('left') );
@new_nodes = $tree->npush( @datum_or_nodes )
This calls push_node for each argument that is a Tree::Easy object, and calls push_new for each argument that is anything else.
The new nodes created are returned.
@new_nodes = $tree->unshift( @datum_or_nodes )
This calls unshift_node for each argument that is a Tree::Easy object, and calls unshift_new for each argument that is anything else.
my $node = $tree->remove_node(0);
Pretty obvious.
$root->$traverse( { printf "%s\n", shift->data }, -1 );
Code to run on each node, it is passed the node as its only argument.
Whether to traverse in prefix (-1), infix (0), or postfix (1).
my $found = $root->search( { shift->data == 'findme' }, 'dfs' );
Code to run on each node, it is passed the node as its only argument. Return 1 when you find what you need.
Whether to do a breadth-first-search (bfs) or depth-first-search (dfs).
When you return a true value, the search function returns the reference to that node you were just visiting.
Returns the height of the tree.
Dump the tree. The tree structure is shown similar to the unix 'tree' command. The data of each node is dumped with dump_node_data.
dump_node_data
Dumps the node data using Data::Dumper. dumper uses this method.
dumper
Justin Davis, <jrcd83 at gmail.com>
<jrcd83 at gmail.com>
Email any bugs to me at the above address. Or use the thing below.
Please report any bugs or feature requests to bug-tree-tiny at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tree-Easy. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-tree-tiny at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Tree::Easy
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Easy
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Tree-Easy
CPAN Ratings
http://cpanratings.perl.org/d/Tree-Easy
Search CPAN
http://search.cpan.org/dist/Tree-Easy/
Tree::Fast - not as fast ;)
Tree::Simple - not as simple ;)
Tree::DAG_Node - pretty advanced trees.
Tree::Node - very very fast XS tree module, scalars for data.
Theres a ton more on CPAN, mostly more complex.
Copyright 2009 Justin Davis, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Tree::Easy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tree::Easy
CPAN shell
perl -MCPAN -e shell install Tree::Easy
For more information on module installation, please visit the detailed CPAN module installation guide.