Tree::Simple(3pm) | User Contributed Perl Documentation | Tree::Simple(3pm) |
Tree::Simple - A simple tree object
use Tree::Simple; # make a tree root my $tree = Tree::Simple->new("0", Tree::Simple->ROOT); # explicitly add a child to it $tree->addChild(Tree::Simple->new("1")); # specify the parent when creating # an instance and it adds the child implicitly my $sub_tree = Tree::Simple->new("2", $tree); # chain method calls $tree->getChild(0)->addChild(Tree::Simple->new("1.1")); # add more than one child at a time $sub_tree->addChildren( Tree::Simple->new("2.1"), Tree::Simple->new("2.2") ); # add siblings $sub_tree->addSibling(Tree::Simple->new("3")); # insert children a specified index $sub_tree->insertChild(1, Tree::Simple->new("2.1a")); # clean up circular references $tree->DESTROY();
Alternately, to avoid calling Tree::Simple->new(...) just to add a node:
use Tree::Simple; use Data::TreeDumper; # Provides DumpTree(). # --------------- my($root) = Tree::Simple->new('Root', Tree::Simple->ROOT); $root->generateChild('Child 1.0'); $root->generateChild('Child 2.0'); $root->getChild(0)->generateChild('Grandchild 1.1'); print DumpTree($root); $root->DESTROY;
This module in an fully object-oriented implementation of a simple n-ary tree. It is built upon the concept of parent-child relationships, so therefore every Tree::Simple object has both a parent and a set of children (who themselves may have children, and so on). Every Tree::Simple object also has siblings, as they are just the children of their immediate parent.
It is can be used to model hierarchal information such as a file-system, the organizational structure of a company, an object inheritance hierarchy, versioned files from a version control system or even an abstract syntax tree for use in a parser. It makes no assumptions as to your intended usage, but instead simply provides the structure and means of accessing and traversing said structure.
This module uses exceptions and a minimal Design By Contract style. All method arguments are required unless specified in the documentation, if a required argument is not defined an exception will usually be thrown. Many arguments are also required to be of a specific type, for instance the $parent argument to the constructor must be a Tree::Simple object or an object derived from Tree::Simple, otherwise an exception is thrown. This may seems harsh to some, but this allows me to have the confidence that my code works as I intend, and for you to enjoy the same level of confidence when using this module. Note however that this module does not use any Exception or Error module, the exceptions are just strings thrown with "die".
I consider this module to be production stable, it is based on a module which has been in use on a few production systems for approx. 2 years now with no issue. The only difference is that the code has been cleaned up a bit, comments added and the thorough tests written for its public release. I am confident it behaves as I would expect it to, and is (as far as I know) bug-free. I have not stress-tested it under extreme duress, but I do not so much intend for it to be used in that type of situation. If this module cannot keep up with your Tree needs, i suggest switching to one of the modules listed in the "OTHER TREE MODULES" section below.
my $tree = Tree::Simple->new("root")->addChild(Tree::Simple->new("child one"));
Or the more complex:
my $tree = Tree::Simple->new("root")->addChild( Tree::Simple->new("1.0")->addChild( Tree::Simple->new("1.0.1") ) );
This method also accepts a numeric $index and removes the child found at that index within the list of children. The $index is bounds checked, if this condition fail, an exception is thrown.
When a child is removed, it results in the shifting up of all children after it, and the removed child is returned. The removed child is properly disconnected from the tree and all its references to its old parent are removed. However, in order to properly clean up and circular references the removed child might have, it is advised to call the "DESTROY" method. See the "CIRCULAR REFERENCES" section for more information.
NOTE: There is no "removeSibling" method as I felt it was probably a bad idea. The same effect can be achieved by manual upwards traversal.
See also </getSiblingCount>.
Warning: This method includes the invocant, so it is not really all siblings but rather all children of the parent!
See also </getAllSiblings>.
Warning: This differs from scalar(parent->getAllSiblings() ) just above, which for some reason includes the invocant. I cannot change getAllSiblings() now for a module first released in 2004.
NOTE: A "ROOT" tree has the depth of -1. This be because Tree::Simple assumes that a root node will usually not contain data, but just be an anchor for the data-containing branches. This may not be intuitive in all cases, so I mention it here.
Returns 1 if the invocant is the first child in the parental list of children. Otherwise returns 0.
Returns 1 if the invocant is the last child in the parental list of children. Otherwise returns 0.
Here is an example of a traversal function that will print out the hierarchy as a tabbed in list.
$tree->traverse(sub { my ($_tree) = @_; my $tag = $_tree->getNodeValue(); print (("\t" x $_tree->getDepth()), $tag, "\n"); return 'ABORT' if 'foo' eq $tag; });
Here is an example of a traversal function that will print out the hierarchy in an XML-style format.
$tree->traverse(sub { my ($_tree) = @_; print ((' ' x $_tree->getDepth()), '<', $_tree->getNodeValue(),'>',"\n"); }, sub { my ($_tree) = @_; print ((' ' x $_tree->getDepth()), '</', $_tree->getNodeValue(),'>',"\n"); });
Note that aborting traverse is not recommended when using $postfunc because post-function will not be called for any nodes after aborting which might lead to less than predictable results.
NOTE: This is also no longer a recursive method which get's it's value on demand, but a value stored in the Tree::Simple object itself, hopefully making it much more efficient and usable.
I have also created a number of Visitor objects and packaged them into the Tree::Simple::VisitorFactory.
Cloning a tree can be an extremely expensive operation for large trees, so we provide two options for cloning, a deep clone and a shallow clone.
When a Tree::Simple object is cloned, the node is deep-copied in the following manner. If we find a normal scalar value (non-reference), we simply copy it. If we find an object, we attempt to call "clone" on it, otherwise we just copy the reference (since we assume the object does not want to be cloned). If we find a SCALAR, REF reference we copy the value contained within it. If we find a HASH or ARRAY reference we copy the reference and recursively copy all the elements within it (following these exact guidelines). We also do our best to assure that circular references are cloned only once and connections restored correctly. This cloning will not be able to copy CODE, RegExp and GLOB references, as they are pretty much impossible to clone. We also do not handle "tied" objects, and they will simply be copied as plain references, and not re-"tied".
Because of perl's reference counting scheme and how that interacts with circular references, if you want an object to be properly reaped you should manually call "DESTROY". This is especially necessary if your object has any children. See the section on "CIRCULAR REFERENCES" for more information.
I would not normally document private methods, but in case you need to subclass Tree::Simple, here they are.
I have revised the model by which Tree::Simple deals with circular references. In the past all circular references had to be manually destroyed by calling DESTROY. The call to DESTROY would then call DESTROY on all the children, and therefore cascade down the tree. This however was not always what was needed, nor what made sense, so I have now revised the model to handle things in what I feel is a more consistent and sane way.
Circular references are now managed with the simple idea that the parent makes the decisions for the child. This means that child-to-parent references are weak, while parent-to-child references are strong. So if a parent is destroyed it will force all the children to detach from it, however, if a child is destroyed it will not be detached from the parent.
By default, you are still required to call DESTROY in order for things to happen. However I have now added the option to use weak references, which alleviates the need for the manual call to DESTROY and allows Tree::Simple to manage this automatically. This is accomplished with a compile time setting like this:
use Tree::Simple 'use_weak_refs';
And from that point on Tree::Simple will use weak references to
allow for
reference counting to clean things up properly.
For those who are unfamiliar with weak references, and how they affect the reference counts, here is a simple illustration. First is the normal model that Tree::Simple uses:
+---------------+ | Tree::Simple1 |<---------------------+ +---------------+ | | parent | | | children |-+ | +---------------+ | | | | | +---------------+ | +->| Tree::Simple2 | | +---------------+ | | parent |-+ | children | +---------------+
Here, Tree::Simple1 has a reference count of 2 (one for the original variable it is assigned to, and one for the parent reference in Tree::Simple2), and Tree::Simple2 has a reference count of 1 (for the child reference in Tree::Simple1).
Now, with weak references:
+---------------+ | Tree::Simple1 |....................... +---------------+ : | parent | : | children |-+ : <--[ weak reference ] +---------------+ | : | : | +---------------+ : +->| Tree::Simple2 | : +---------------+ : | parent |.. | children | +---------------+
Now Tree::Simple1 has a reference count of 1 (for the variable it is assigned to) and 1 weakened reference (for the parent reference in Tree::Simple2). And Tree::Simple2 has a reference count of 1, just as before.
None that I am aware of. The code is pretty thoroughly tested (see "CODE COVERAGE" below) and is based on an (non-publicly released) module which I had used in production systems for about 3 years without incident. Of course, if you find a bug, let me know, and I will be sure to fix it.
I use Devel::Cover to test the code coverage of my tests, below is the Devel::Cover report on the test suite.
---------------------------- ------ ------ ------ ------ ------ ------ ------ File stmt branch cond sub pod time total ---------------------------- ------ ------ ------ ------ ------ ------ ------ Tree/Simple.pm 99.6 96.0 92.3 100.0 97.0 95.5 98.0 Tree/Simple/Visitor.pm 100.0 96.2 88.2 100.0 100.0 4.5 97.7 ---------------------------- ------ ------ ------ ------ ------ ------ ------ Total 99.7 96.1 91.1 100.0 97.6 100.0 97.9 ---------------------------- ------ ------ ------ ------ ------ ------ ------
I have written a number of other modules which use or augment this module, they are describes below and available on CPAN.
Also, the author of Data::TreeDumper and I have worked together to make sure that Tree::Simple and his module work well together. If you need a quick and handy way to dump out a Tree::Simple hierarchy, this module does an excellent job (and plenty more as well).
I have also recently stumbled upon some packaged distributions of Tree::Simple for the various Unix flavors. Here are some links:
There are a few other Tree modules out there, here is a quick comparison between Tree::Simple and them. Obviously I am biased, so take what I say with a grain of salt, and keep in mind, I wrote Tree::Simple because I could not find a Tree module that suited my needs. If Tree::Simple does not fit your needs, I recommend looking at these modules. Please note that I am only listing Tree::* modules I am familiar with here, if you think I have missed a module, please let me know. I have also seen a few tree-ish modules outside of the Tree::* namespace, but most of them are part of another distribution (HTML::Tree, Pod::Tree, etc) and are likely specialized in purpose.
All this said, I am not a huge fan of the API either, I prefer the gender neutral approach in Tree::Simple to the mother/daughter style of Tree::DAG_Node. I also feel very strongly that Tree::DAG_Node is trying to do much more than makes sense in a single module, and is offering too many ways to do the same or similar things.
However, of all the Tree::* modules out there, Tree::DAG_Node seems to be one of the favorites, so it may be worth investigating.
This module is similar in intent to Tree::Simple. It implements a tree with n branches and has polymorphic node containers. It implements much of the same methods as Tree::Simple and a few others on top of that, but being based on a C library, is not very OO. In most of the method calls the $self argument is not used and the second argument $node is. Tree::Simple is a much more OO module than Tree::Nary, so while they are similar in functionality they greatly differ in implementation style.
<https://github.com/ronsavage/Tree-Simple>.
Bugs should be reported via the CPAN bug tracker at
<https://github.com/ronsavage/Tree-Simple/issues>
Stevan Little, <stevan@iinteractive.com>
Rob Kinyon, <rob@iinteractive.com>
Ron Savage <ron@savage.net.au> has taken over maintenance as of V 1.19.
Copyright 2004-2006 by Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-10-13 | perl v5.34.0 |