Tree::Binary2(3)
π Tree::Binary2 π
okay bestie, sit down, hydrate, and let me tell you about Tree::Binary2(3) β it's a Perl thing, it lives in the User Contributed Perl Documentation, and it's genuinely so cute once you get it. no cap. π
β¨ NAME β¨
Tree::Binary2 β an implementation of a binary tree π³
πΈ SYNOPSIS πΈ
my $tree = Tree::Binary2->new( 'root' );
my $left = Tree::Binary2->new( 'left' );
$tree->left( $left );
my $right = Tree::Binary2->new( 'left' );
$tree->right( $right );
my $right_child = $tree->right;
$tree->right( undef ); # Unset the right child.
my @nodes = $tree->traverse( $tree->POST_ORDER );
my $traversal = $tree->traverse( $tree->IN_ORDER );
while ( my $node = $traversal->() ) {
# Do something with $node here
}
π¦ DESCRIPTION π¦
So. This is an implementation of a binary tree. and here's the plot twist that makes it iconic: this class inherits from Tree, which is an N-ary tree implementation. Because of that whole situation, this class actually gives you a complete binary tree vs. a sparse binary tree. the empty nodes? they're instances of Tree::Null, which is described over in Tree. π
and lowkey the best part: that should have no effect on your usage of this class. you just vibe, it handles the rest.
π METHODS π
In addition to the methods provided by Tree, the following items are provided or overridden. (yes, overridden, she's a main character.)
left([$child])/right([$child])
These access the left and right children, respectively. They're mutators, which means their behavior changes depending on if you pass in a value. it's giving shapeshifter.
- If you don't pass in any parameters, it acts as a getter for the specific child β returns the child if set, or
undefif not. - If you pass in a child, it acts as a setter for the specific child, setting the child to the passed-in value and returning the
$tree. (so yes, this method chains. slay.) β¨ - If you wish to unset the child, do
$tree>left( undef );
children()
This will return the children of the tree.
NOTE: There will be two children, always.Tree::Binary2implements a complete binary tree, filling in missing children withTree::Nullobjects. (Please seeTree::Fastfor more information onTree::Null.)
so like, no empty seats at this table. ever. π
traverse( [$order] )
When called in list context (my @traversal = $tree->traverse()), this returns a list of the nodes in the given traversal order. When called in scalar context (my $traversal = $tree->traverse()), this returns a closure that will, over successive calls, iterate over the nodes in the given traversal order. When it's finished it will return false. (rip, but respectfully.)
The default traversal order is pre-order.
In addition to the traversal orders provided by Tree, Tree::Binary2 provides in-order traversals.
- In-order
This will return the result of an in-order traversal on the left node (if any), then the node, then the result of an in-order traversal on the right node (if any). left β me β right, basically. π
NOTE: You have access to all the methods provided byTree, but it is not recommended that you use many of them, unless you know what you're doing. This list includesadd_child()andremove_child(). fr fr, don't just start pressing buttons. π
π TODO π
- Make in-order closure traversal work iteratively
- Make post-order closure traversal work iteratively
(it's on the list, bestie. it's on the list. π«Ά)
π CODE COVERAGE π
Please see the relevant sections of Tree.
π SUPPORT π
Please see the relevant sections of Tree.
β AUTHORS β
- Rob Kinyon
<rob.kinyon@iinteractive.com> - Stevan Little
<stevan.little@iinteractive.com>
Thanks to Infinity Interactive for generously donating our time. π
π§ COPYRIGHT AND LICENSE π§
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.
and that's the whole page, bestie. this one's perl v5.42.0, dated 2025-07-25, and it goes out to Tree::Binary2(3) β stay leafy πΈπ
π¦ where this page comes from
- fedora 43: perl-Tree 1.16-6.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms