1Tree::Binary2(3) User Contributed Perl Documentation Tree::Binary2(3)
2
3
4
6 Tree::Binary2 - An implementation of a binary tree
7
9 my $tree = Tree::Binary2->new( 'root' );
10
11 my $left = Tree::Binary2->new( 'left' );
12 $tree->left( $left );
13
14 my $right = Tree::Binary2->new( 'left' );
15 $tree->right( $right );
16
17 my $right_child = $tree->right;
18
19 $tree->right( undef ); # Unset the right child.
20
21 my @nodes = $tree->traverse( $tree->POST_ORDER );
22
23 my $traversal = $tree->traverse( $tree->IN_ORDER );
24 while ( my $node = $traversal->() ) {
25 # Do something with $node here
26 }
27
29 This is an implementation of a binary tree. This class inherits from
30 Tree, which is an N-ary tree implemenation. Because of this, this class
31 actually provides an implementation of a complete binary tree vs. a
32 sparse binary tree. The empty nodes are instances of Tree::Null, which
33 is described in Tree. This should have no effect on your usage of this
34 class.
35
37 In addition to the methods provided by Tree, the following items are
38 provided or overriden.
39
40 · "left([$child])" / "right([$child])"
41
42 These access the left and right children, respectively. They are
43 mutators, which means that their behavior changes depending on if
44 you pass in a value.
45
46 If you do not pass in any parameters, then it will act as a getter
47 for the specific child, return the child (if set) or undef (if
48 not).
49
50 If you pass in a child, it will act as a setter for the specific
51 child, setting the child to the passed-in value and returning the
52 $tree. (Thus, this method chains.)
53
54 If you wish to unset the child, do "$tree>left( undef );"
55
56 · "children()"
57
58 This will return the children of the tree.
59
60 NOTE: There will be two children, always. Tree::Binary2 implements
61 a complete binary tree, filling in missing children with Tree::Null
62 objects. (Please see Tree::Fast for more information on
63 Tree::Null.)
64
65 · traverse( [$order] )
66
67 When called in list context ("my @traversal = $tree->traverse()"),
68 this will return a list of the nodes in the given traversal order.
69 When called in scalar context ("my $traversal =
70 $tree->traverse()"), this will return a closure that will, over
71 successive calls, iterate over the nodes in the given traversal
72 order. When finished it will return false.
73
74 The default traversal order is pre-order.
75
76 In addition to the traversal orders provided by Tree, Tree::Binary2
77 provides in-order traversals.
78
79 · In-order
80
81 This will return the result of an in-order traversal on the
82 left node (if any), then the node, then the result of an in-
83 order traversal on the right node (if any).
84
85 NOTE: You have access to all the methods provided by Tree, but it is
86 not recommended that you use many of them, unless you know what you're
87 doing. This list includes "add_child()" and "remove_child()".
88
90 · Make in-order closure traversal work iteratively
91
92 · Make post-order closure traversal work iteratively
93
95 Please see the relevant sections of Tree.
96
98 Please see the relevant sections of Tree.
99
101 Rob Kinyon <rob.kinyon@iinteractive.com>
102
103 Stevan Little <stevan.little@iinteractive.com>
104
105 Thanks to Infinity Interactive for generously donating our time.
106
108 Copyright 2004, 2005 by Infinity Interactive, Inc.
109
110 <http://www.iinteractive.com>
111
112 This library is free software; you can redistribute it and/or modify it
113 under the same terms as Perl itself.
114
115
116
117perl v5.32.1 2021-02-02 Tree::Binary2(3)