|
Tree::Binary::Visitor::InOrderTraversal - Visitor object for Tree::Binary objects |
Tree::Binary::Visitor::InOrderTraversal - Visitor object for Tree::Binary objects
use Tree::Binary; use Tree::Binary::Visitor::InOrderTraversal;
# create a visitor instance my $visitor = Tree::Binary::Visitor::InOrderTraversal->new();
# create a tree to visit
# this is an expression tree
# representing ((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")));
# by default this will collect all the # node values in depth-first order into # our results $tree->accept($visitor);
# get our results and print them print join ", ", $visitor->getResults(); # prints "2, +, 2, *, 4, +, 5"
# for more complex node objects, you can specify
# a node filter which will be used to extract the
# information desired from each node
$visitor->setNodeFilter(sub {
my ($t) = @_;
return $t->getNodeValue()->description();
});
Post-order traversal is a variation of the depth-first traversal in which the left sub-tree is processed, followed by tree itself, then the right sub-tree. It is another alternative to Tree::Binary's traverse method and it's depth-first, pre-order traversal.
setNodeFilter method to customize its behavior.
setNodeFilter argument.
$filter_function argument. This code reference is used to filter the tree nodes as they are collected. This can be used to customize output, or to gather specific information from a more complex tree node. The filter function should accept a single argument, which is the current Tree::Binary object.
setResults with no argument.
visit method accepts a Tree::Binary and applies the function set in new or setNodeFilter appropriately. The results of this application can be retrieved with getResults
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
See the CODE COVERAGE section of Tree::Binary for details.
stevan little, <stevan@iinteractive.com>
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.
|
Tree::Binary::Visitor::InOrderTraversal - Visitor object for Tree::Binary objects |