Justin Davis > Tree-Easy > Tree::Easy

Download:
Tree-Easy-0.01.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.01   Source  

NAME ^

Tree::Easy - An easy, simple & fast N-ary tree datatype

VERSION ^

Version 0.01

SYNOPSIS ^

  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!};

DESCRIPTION ^

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.

REPRESENTATION ^

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.

WARNING ^

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.

METHODS ^

Arguments given in brackets are optional.

new [$data]

  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.

data [$newdata]

  my $data = $tree->data;
  $tree->data( 'foo' );
  $tree->data( [ 'foo' ] );
  $tree->data( { foo => 'bar' } );

Retrieves or stores data assigned to the node.

clone

  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).

insert_node $node [$where=-1]

  my $node = Tree::Easy->new('foo');
  $tree->insert_node($node)

push_new $data

  $new_node = $tree->push_new('right');

unshift_new $data

  $new_node = $tree->unshift_new('left');

push_node $node

  $new_node = $tree->push_node( Tree::Easy->new('right') );

unshift_node $node

  $new_node = $tree->push_node( Tree::Easy->new('left') );

npush [@args]

  @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.

nunshift [@args]

  @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.

The new nodes created are returned.

remove_node $which

  my $node = $tree->remove_node(0);

pop_node

Pretty obvious.

shift_node

Pretty obvious.

traverse $code_ref [$how=0]

  $root->$traverse( { printf "%s\n", shift->data }, -1 );
$coderef

Code to run on each node, it is passed the node as its only argument.

$how

Whether to traverse in prefix (-1), infix (0), or postfix (1).

search $code_ref [$how='dfs']

  my $found = $root->search( { shift->data == 'findme' }, 'dfs' );
$code_ref

Code to run on each node, it is passed the node as its only argument. Return 1 when you find what you need.

$how

Whether to do a breadth-first-search (bfs) or depth-first-search (dfs).

$found

When you return a true value, the search function returns the reference to that node you were just visiting.

get_height

Returns the height of the tree.

dumper

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.

AUTHOR ^

Justin Davis, <jrcd83 at gmail.com>

BUGS ^

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.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc Tree::Easy

You can also look for information at:

SEE ALSO ^

COPYRIGHT & LICENSE ^

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.

syntax highlighting: