View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Rob Kinyon > Tree-1.01 > Tree::Fast



Annotate this POD


Open  0
View/Report Bugs
Module Version: 1.01   Source   Latest Release: Tree-1.10


Tree::Fast - the fastest possible implementation of a tree in pure Perl


  my $tree = Tree->new( 'root' );
  my $child = Tree->new( 'child' );
  $tree->add_child( {}, $child );

  $tree->add_child( { at => 0 }, Tree->new( 'first child' ) );
  $tree->add_child( { at => -1 }, Tree->new( 'last child' ) );

  my @children = $tree->children;
  my @some_children = $tree->children( 0, 2 );

  $tree->remove_child( 0 );

  my @nodes = $tree->traverse( $tree->POST_ORDER );

  my $traversal = $tree->traverse( $tree->POST_ORDER );
  while ( my $node = $traversal->() ) {
      # Do something with $node here

  my $clone = $tree->clone;
  my $mirror = $tree->clone->mirror;


This is meant to be the core implementation for Tree, stripped down as much as possible. There is no error-checking, bounds-checking, event-handling, convenience methods, or anything else of the sort. If you want something fuller- featured, please look at Tree, which is a wrapper around Tree::Fast.




This will return a Tree object. It will accept one parameter which, if passed, will become the value (accessible by value()). All other parameters will be ignored.

If you call $tree->new([$value]), it will instead call clone(), then set the value of the clone to $value.


This will return a clone of $tree. The clone will be a root tree, but all children will be cloned.

If you call Tree->clone([$value]), it will instead call new().

NOTE: the value is merely a shallow copy. This means that all references will be kept.


add_child($options, @nodes)

This will add all the @nodes as children of $tree. $options is a required hashref that specifies options for add_child(). The optional parameters are:

  • at

    This specifies the index to add @nodes at. If specified, this will be passed into splice(). The only exceptions are if this is 0, it will act as an unshift(). If it is unset or undefined, it will act as a push().

remove_child($options, @nodes)

This will remove all the @nodes from the children of $tree. You can either pass in the actual child object you wish to remove, the index of the child you wish to remove, or a combination of both.

$options is a required hashref that specifies parameters for remove_child(). Currently, no parameters are used.


This will modify the tree such that it is a mirror of what it was before. This means that the order of all children is reversed.

NOTE: This is a destructive action. It will modify the tree's internal structure. If you wish to get a mirror, yet keep the original tree intact, use my $mirror = $tree->clone->mirror;

traverse( [$order] )

When called in list context (my @traversal = $tree->traverse()), this will return a list of the nodes in the given traversal order. When called in scalar context (my $traversal = $tree->traverse()), this will return a closure that will, over successive calls, iterate over the nodes in the given traversal order. When finished it will return false.

The default traversal order is pre-order.

The various traversal orders do the following steps:

  • Pre-order (aka Prefix traversal)

    This will return the node, then the first sub tree in pre-order traversal, then the next sub tree, etc.

    Use $tree->PRE_ORDER as the $order.

  • Post-order (aka Prefix traversal)

    This will return the each sub-tree in post-order traversal, then the node.

    Use $tree->POST_ORDER as the $order.

  • Level-order (aka Prefix traversal)

    This will return the node, then the all children of the node, then all grandchildren of the node, etc.

    Use $tree->LEVEL_ORDER as the $order.



If you call $self->parent on a root node, it will return a Tree::Null object. This is an implementation of the Null Object pattern optimized for usage with Forest. It will evaluate as false in every case (using overload) and all methods called on it will return a Tree::Null object.



Please see the relevant sections of Tree.


Please see the relevant sections of Tree.



Rob Kinyon <>

Stevan Little <>

Thanks to Infinity Interactive for generously donating our time.


Copyright 2004, 2005 by Infinity Interactive, Inc.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: