|
NAMETree::Binary - An Object Oriented Binary Tree for PerlSYNOPSISThis program ships as scripts/traverse.1.pl:#!/usr/bin/env perl use strict; use warnings; use Tree::Binary; use Tree::Binary::Visitor::BreadthFirstTraversal; use Tree::Binary::Visitor::InOrderTraversal; use Tree::Binary::Visitor::PreOrderTraversal; use Tree::Binary::Visitor::PostOrderTraversal; # --------------- # A tree representaion of the expression: # ( (2 + 2) * (4 + 5) ) my($btree) = Tree::Binary -> new('*') -> setLeft ( Tree::Binary -> new('+') -> setLeft(Tree::Binary->new('2') ) -> setRight(Tree::Binary->new('2') ) ) -> setRight ( Tree::Binary->new('+') -> setLeft(Tree::Binary->new('4') ) -> setRight(Tree::Binary->new('5') ) ); # Or shown visually: # +---(*)---+ # | | # +-(+)-+ +-(+)-+ # | | | | # (2) (2) (4) (5) # There is no method which will display the above, # but a crude tree-printer follows. my($parent_depth); $btree -> traverse ( sub { my($tree) = @_; print "\t" x $tree -> getDepth, $tree -> getNodeValue, "\n"; } ); # Get a InOrder visitor. my($visitor) = Tree::Binary::Visitor::InOrderTraversal -> new; $btree -> accept($visitor); # Print the expression in infix order. print join(' ', $visitor -> getResults), "\n"; # Prints '2 + 2 * 4 + 5'. # Get a PreOrder visitor. $visitor = Tree::Binary::Visitor::PreOrderTraversal -> new; $btree -> accept($visitor); # Print the expression in prefix order. print join(' ', $visitor -> getResults), "\n"; # Prints '* + 2 2 + 4 5'. # Get a PostOrder visitor. $visitor = Tree::Binary::Visitor::PostOrderTraversal -> new; $btree -> accept($visitor); # Print the expression in postfix order. print join(' ', $visitor -> getResults), "\n"; # Prints '2 2 + 4 5 + *'. # Get a BreadthFirst visitor. $visitor = Tree::Binary::Visitor::BreadthFirstTraversal -> new; $btree -> accept($visitor); # Print the expression in breadth first order. print join(' ', $visitor -> getResults), "\n"; # Prints '* + + 2 2 4 5'. # Be sure to clean up all circular references. # Of course, since we're exiting immediately, this particular program # does not need such a defensive manoeuvre. $btree -> DESTROY(); If printing the tree is important, you are better off using Tree::DAG_Node <https://metacpan.org/pod/Tree::DAG_Node#tree2string-options-some_tree>. DESCRIPTIONThis module is a fully object oriented implementation of a binary tree. Binary trees are a specialized type of tree which has only two possible branches, a left branch and a right branch. While it is possible to use an n-ary tree, like Tree::Simple, to fill most of your binary tree needs, a true binary tree object is just easier to mantain and use.Binary Tree objects are especially useful (to me anyway) when building parse trees of things like mathematical or boolean expressions. They can also be used in games for such things as descisions trees. Binary trees are a well studied data structure and there is a wealth of information on the web about them. 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 $tree argument to both the "setLeft" and "setRight" methods, must be a Tree::Binary object or an object derived from Tree::Binary, 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". This object uses a number of methods copied from another module of mine, Tree::Simple. Users of that module will find many similar methods and behaviors. However, it did not make sense for Tree::Binary to be derived from Tree::Simple, as there are a number of methods in Tree::Simple that just wouldn't make sense in Tree::Binary. So, while I normally do not approve of cut-and-paste code reuse, it was what made the most sense in this case. METHODS
Mutators
Accessors
Informational
Recursive Methods
Misc. Methods
CIRCULAR REFERENCESPerl uses reference counting to manage the destruction of objects, and this can cause problems with circularly referencing object like Tree::Binary. In order to properly manage your circular references, it is nessecary to manually call the "DESTROY" method on a Tree::Binary instance. Here is some example code:# create a root my $root = Tree::Binary->new() { # create a lexical scope # create a subtree (with a child) my $subtree = Tree::Binary->new("1") ->setRight( Tree::Binary->new("1.1") ); # add the subtree to the root $root->setLeft($subtree); # ... do something with your trees # remove the first child $root->removeLeft(); } At this point you might expect perl to reap $subtree since it has been removed from the $root and is no longer available outside the lexical scope of the block. However, since $subtree itself has a subtree, its reference count is still (at least) one and perl will not reap it. The solution to this is to call the "DESTROY" method manually at the end of the lexical block, this will result in the breaking of all relations with the DESTROY-ed object and allow that object to be reaped by perl. Here is a corrected version of the above code. # create a root my $root = Tree::Binary->new() { # create a lexical scope # create a subtree (with a child) my $subtree = Tree::Binary->new("1") ->setRight( Tree::Binary->new("1.1") ); # add the subtree to the root $root->setLeft($subtree); # ... do something with your trees # remove the first child and capture it my $removed = $root->removeLeft(); # now force destruction of the removed child $removed->DESTROY(); } As you can see if the corrected version we used a new variable to capture the removed tree, and then explicitly called "DESTROY" upon it. Only when a removed subtree has no children (it is a leaf node) can you safely ignore the call to "DESTROY". It is even nessecary to call "DESTROY" on the root node if you want it to be reaped before perl exits, this is especially important in long running environments like mod_perl. OTHER TREE MODULESAs crazy as it might seem, there are no pure (non-search) binary tree implementations on CPAN (at least not that I could find). I found several balanced trees of one kind or another (see the "OTHER TREE MODULES" section of the Tree::Binary::Search documentation for that list). The closet thing I could find was the Tree module described below.
SEE ALSOThis module is part of a larger group, which are listed below.
BUGSNone that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.CODE COVERAGEI use Devel::Cover to test the code coverage of my tests, below is the Devel::Cover report on this module test suite.-------------------------------------------- ------ ------ ------ ------ ------ ------ ------ File stmt branch cond sub pod time total -------------------------------------------- ------ ------ ------ ------ ------ ------ ------ Tree/Binary.pm 100.0 97.3 93.9 100.0 100.0 71.7 98.7 Tree/Binary/Search.pm 99.0 90.5 81.2 100.0 100.0 13.9 95.1 Tree/Binary/Search/Node.pm 100.0 100.0 66.7 100.0 100.0 11.7 98.2 Tree/Binary/VisitorFactory.pm 100.0 100.0 n/a 100.0 100.0 0.5 100.0 Tree/Binary/Visitor/Base.pm 100.0 100.0 66.7 100.0 100.0 0.5 96.4 Tree/Binary/Visitor/BreadthFirstTraversal.pm 100.0 100.0 100.0 100.0 100.0 0.0 100.0 Tree/Binary/Visitor/InOrderTraversal.pm 100.0 100.0 100.0 100.0 100.0 1.1 100.0 Tree/Binary/Visitor/PostOrderTraversal.pm 100.0 100.0 100.0 100.0 100.0 0.3 100.0 Tree/Binary/Visitor/PreOrderTraversal.pm 100.0 100.0 100.0 100.0 100.0 0.3 100.0 -------------------------------------------- ------ ------ ------ ------ ------ ------ ------ Total 99.6 94.4 88.8 100.0 100.0 100.0 97.4 -------------------------------------------- ------ ------ ------ ------ ------ ------ ------ Repository<https://github.com/ronsavage/Tree-Binary>AUTHORstevan little, <stevan@iinteractive.com>Since V 1.00, Ron Savage <ron@savage.net.au> has been the maintainer. COPYRIGHT AND LICENSECopyright 2004, 2005 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.
Visit the GSP FreeBSD Man Page Interface. |