Name
Tree::Ops - Tree operations.
Synopsis
Create a tree:
my $t = Tree::Ops::new 'a';
for(1..2)
{$t->open ('b');
$t->single('c');
$t->close;
}
$t->single ('d');
Print the tree:
is_deeply $t->print(sub{@_}), <<END;
a
b
c
b
c
d
END
Locate a specific child in the tree and print it:
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
Description
Tree operations.
Version 20200701.
The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.
Build
Create a tree.
new($user)
Create a new child recording the specified user data.
Parameter Description
1 $user User data to be recorded in the child
Example:
if (1)
{my $t = Tree::Ops::𝗻𝗲𝘄 'a';
for(1..2)
{$t->open ('b');
$t->single('c');
$t->close;
}
$t->single ('d');
is_deeply $t->print, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
}
This is a static method and so should either be imported or invoked as:
Tree::Ops::new
open($tree, $user)
Add a child and make it the currently active scope into which new children will be added.
Parameter Description
1 $tree Tree
2 $user User data to be recorded in the interior child being opened
Example:
if (1)
{my $t = Tree::Ops::new 'a';
for(1..2)
{$t->𝗼𝗽𝗲𝗻 ('b');
$t->single('c');
$t->close;
}
$t->single ('d');
is_deeply $t->print, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
}
close($tree)
Close the current scope returning to the previous scope.
Parameter Description
1 $tree Tree
Example:
if (1)
{my $t = Tree::Ops::new 'a';
for(1..2)
{$t->open ('b');
$t->single('c');
$t->𝗰𝗹𝗼𝘀𝗲;
}
$t->single ('d');
is_deeply $t->print, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
}
single($tree, $user)
Add one child in the current scope.
Parameter Description
1 $tree Tree
2 $user User data to be recorded in the child being created
Example:
if (1)
{my $t = Tree::Ops::new 'a';
for(1..2)
{$t->open ('b');
$t->𝘀𝗶𝗻𝗴𝗹𝗲('c');
$t->close;
}
$t->𝘀𝗶𝗻𝗴𝗹𝗲 ('d');
is_deeply $t->print, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
}
Navigation
Navigate through the tree.
first($parent)
Get the first child under the specified parent.
Parameter Description
1 $parent Parent
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $c->parent, $b;
is_deeply $a->𝗳𝗶𝗿𝘀𝘁, $b;
is_deeply $a->last, $d;
is_deeply $e->next, $f;
is_deeply $f->prev, $e;
last($parent)
Get the last child under the specified parent.
Parameter Description
1 $parent Parent
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $c->parent, $b;
is_deeply $a->first, $b;
is_deeply $a->𝗹𝗮𝘀𝘁, $d;
is_deeply $e->next, $f;
is_deeply $f->prev, $e;
next($child)
Get the next sibling following the specified child.
Parameter Description
1 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $c->parent, $b;
is_deeply $a->first, $b;
is_deeply $a->last, $d;
is_deeply $e->𝗻𝗲𝘅𝘁, $f;
is_deeply $f->prev, $e;
prev($child)
Get the previous sibling of the specified child.
Parameter Description
1 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $c->parent, $b;
is_deeply $a->first, $b;
is_deeply $a->last, $d;
is_deeply $e->next, $f;
is_deeply $f->𝗽𝗿𝗲𝘃, $e;
firstMost($parent)
Return the first most descendant child in the tree starting at this parent or else return undef if this parent has no children.
Parameter Description
1 $parent Child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
is_deeply $a->print(sub{@_}), <<END;
a
b
c
y
x
y
x
d
e
f
g
h
i
j
END
is_deeply $a->𝗳𝗶𝗿𝘀𝘁𝗠𝗼𝘀𝘁->brackets, 'c';
is_deeply $a-> lastMost->brackets, 'j';
lastMost($parent)
Return the last most descendant child in the tree starting at this parent or else return undef if this parent has no children.
Parameter Description
1 $parent Child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
is_deeply $a->print(sub{@_}), <<END;
a
b
c
y
x
y
x
d
e
f
g
h
i
j
END
is_deeply $a->firstMost->brackets, 'c';
is_deeply $a-> 𝗹𝗮𝘀𝘁𝗠𝗼𝘀𝘁->brackets, 'j';
Location
Verify the current location.
context($child)
Get the context of the current child.
Parameter Description
1 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->𝗰𝗼𝗻𝘁𝗲𝘅𝘁], [qw(x y a)];
is_deeply join(' ', $a->by(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->cut;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->unwrap;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->wrap(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->dup);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
isFirst($child)
Return the specified child if that child is first under its parent, else return undef.
Parameter Description
1 $child Child
Example:
if (1)
{is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $b->singleChildOfParent, $c;
is_deeply $e->𝗶𝘀𝗙𝗶𝗿𝘀𝘁, $e;
ok !$f->𝗶𝘀𝗙𝗶𝗿𝘀𝘁;
ok !$g->isLast;
is_deeply $h->isLast, $h;
}
isLast($child)
Return the specified child if that child is last under its parent, else return undef.
Parameter Description
1 $child Child
Example:
if (1)
{is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $b->singleChildOfParent, $c;
is_deeply $e->isFirst, $e;
ok !$f->isFirst;
ok !$g->𝗶𝘀𝗟𝗮𝘀𝘁;
is_deeply $h->𝗶𝘀𝗟𝗮𝘀𝘁, $h;
}
singleChildOfParent($parent)
Return the only child of this parent if the parent has an only child, else undef
Parameter Description
1 $parent Parent
Example:
if (1)
{is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
is_deeply $b->𝘀𝗶𝗻𝗴𝗹𝗲𝗖𝗵𝗶𝗹𝗱𝗢𝗳𝗣𝗮𝗿𝗲𝗻𝘁, $c;
is_deeply $e->isFirst, $e;
ok !$f->isFirst;
ok !$g->isLast;
is_deeply $h->isLast, $h;
}
Put
Insert children into a tree.
putFirst($parent, $child)
Place a new child first under the specified parent and return the child.
Parameter Description
1 $parent Parent
2 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
my $z = $b->putNext(new 'z');
is_deeply $z->brackets, 'z';
is_deeply $a->brackets, 'a(b(c)zd(efgh(i(j))))';
my $y = $d->putPrev(new 'y');
is_deeply $y->brackets, 'y';
is_deeply $a->brackets, 'a(b(c)zyd(efgh(i(j))))';
$z->putLast(new 't');
is_deeply $z->brackets, 'z(t)';
is_deeply $a->brackets, 'a(b(c)z(t)yd(efgh(i(j))))';
$z->𝗽𝘂𝘁𝗙𝗶𝗿𝘀𝘁(new 's');
is_deeply $a->brackets, 'a(b(c)z(st)yd(efgh(i(j))))';
putLast($parent, $child)
Place a new child last under the specified parent and return the child.
Parameter Description
1 $parent Parent
2 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
my $z = $b->putNext(new 'z');
is_deeply $z->brackets, 'z';
is_deeply $a->brackets, 'a(b(c)zd(efgh(i(j))))';
my $y = $d->putPrev(new 'y');
is_deeply $y->brackets, 'y';
is_deeply $a->brackets, 'a(b(c)zyd(efgh(i(j))))';
$z->𝗽𝘂𝘁𝗟𝗮𝘀𝘁(new 't');
is_deeply $z->brackets, 'z(t)';
is_deeply $a->brackets, 'a(b(c)z(t)yd(efgh(i(j))))';
$z->putFirst(new 's');
is_deeply $a->brackets, 'a(b(c)z(st)yd(efgh(i(j))))';
putNext($child, $new)
Place a new child after the specified child.
Parameter Description
1 $child Existing child
2 $new New child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
my $z = $b->𝗽𝘂𝘁𝗡𝗲𝘅𝘁(new 'z');
is_deeply $z->brackets, 'z';
is_deeply $a->brackets, 'a(b(c)zd(efgh(i(j))))';
my $y = $d->putPrev(new 'y');
is_deeply $y->brackets, 'y';
is_deeply $a->brackets, 'a(b(c)zyd(efgh(i(j))))';
$z->putLast(new 't');
is_deeply $z->brackets, 'z(t)';
is_deeply $a->brackets, 'a(b(c)z(t)yd(efgh(i(j))))';
$z->putFirst(new 's');
is_deeply $a->brackets, 'a(b(c)z(st)yd(efgh(i(j))))';
putPrev($child, $new)
Place a new child before the specified child.
Parameter Description
1 $child Child
2 $new New child
Example:
is_deeply $a->brackets, 'a(b(c)d(efgh(i(j))))';
my $z = $b->putNext(new 'z');
is_deeply $z->brackets, 'z';
is_deeply $a->brackets, 'a(b(c)zd(efgh(i(j))))';
my $y = $d->𝗽𝘂𝘁𝗣𝗿𝗲𝘃(new 'y');
is_deeply $y->brackets, 'y';
is_deeply $a->brackets, 'a(b(c)zyd(efgh(i(j))))';
$z->putLast(new 't');
is_deeply $z->brackets, 'z(t)';
is_deeply $a->brackets, 'a(b(c)z(t)yd(efgh(i(j))))';
$z->putFirst(new 's');
is_deeply $a->brackets, 'a(b(c)z(st)yd(efgh(i(j))))';
Steps
Move the start or end of a scope forwards or backwards as suggested by Alex Monroe.
step($parent)
Make the first child of the specified parent the parents previous sibling and return the parent. In effect this moves the start of the parent one step forwards.
Parameter Description
1 $parent Parent
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$d->𝘀𝘁𝗲𝗽;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)ed(fgh(i(j))))';
$d->stepBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$b->stepEnd;
is_deeply $a->brackets, 'a(b(cy(x))y(x)d(efgh(i(j))))';
$b->stepEndBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
stepEnd($parent)
Make the next sibling of the specified parent the parents last child and return the parent. In effect this moves the end of the parent one step forwards.
Parameter Description
1 $parent Parent
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$d->step;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)ed(fgh(i(j))))';
$d->stepBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$b->𝘀𝘁𝗲𝗽𝗘𝗻𝗱;
is_deeply $a->brackets, 'a(b(cy(x))y(x)d(efgh(i(j))))';
$b->stepEndBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
stepBack()
Make the previous sibling of the specified parent the parents first child and return the parent. In effect this moves the start of the parent one step backwards.
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$d->step;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)ed(fgh(i(j))))';
$d->𝘀𝘁𝗲𝗽𝗕𝗮𝗰𝗸;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$b->stepEnd;
is_deeply $a->brackets, 'a(b(cy(x))y(x)d(efgh(i(j))))';
$b->stepEndBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
stepEndBack()
Make the last child of the specified parent the parents next sibling and return the parent. In effect this moves the end of the parent one step backwards.
Example:
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$d->step;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)ed(fgh(i(j))))';
$d->stepBack;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
$b->stepEnd;
is_deeply $a->brackets, 'a(b(cy(x))y(x)d(efgh(i(j))))';
$b->𝘀𝘁𝗲𝗽𝗘𝗻𝗱𝗕𝗮𝗰𝗸;
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
Edit
Edit a tree in situ.
cut($child)
Cut out a child and all its content and children, return it ready for reinsertion else where.
Parameter Description
1 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->context], [qw(x y a)];
is_deeply join(' ', $a->by(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->𝗰𝘂𝘁;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->unwrap;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->wrap(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->dup);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
dup($parent)
Duplicate a parent and all its descendants.
Parameter Description
1 $parent Parent
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->context], [qw(x y a)];
is_deeply join(' ', $a->by(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->cut;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->unwrap;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->wrap(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->𝗱𝘂𝗽);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
unwrap($child)
Unwrap the specified child and return that child.
Parameter Description
1 $child Child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->context], [qw(x y a)];
is_deeply join(' ', $a->by(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->cut;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->𝘂𝗻𝘄𝗿𝗮𝗽;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->wrap(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->dup);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
wrap($child, $new)
Wrap the specified child with a new parent and return the new parent.
Parameter Description
1 $child Child to wrap
2 $new New wrapping parent
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->context], [qw(x y a)];
is_deeply join(' ', $a->by(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->cut;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->unwrap;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->𝘄𝗿𝗮𝗽(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->dup);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
Traverse
Traverse the tree.
by($tree, $sub)
Traverse a tree in order to process each child and return an array of the results of processing each child.
Parameter Description
1 $tree Tree
2 $sub Method to process a child
Example:
is_deeply $a->brackets, 'a(b(c)y(x)z(st)d(efgh(i(j))))';
is_deeply [map {$_->user} $x->context], [qw(x y a)];
is_deeply join(' ', $a->𝗯𝘆(sub{$_[0]->user})), "c b x y s t z e f g j i h d a";
$z->cut;
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->unwrap;
is_deeply $a->brackets, 'a(b(c)xd(efgh(i(j))))';
$y = $x->wrap(new 'y');
is_deeply $y->brackets, 'y(x)';
is_deeply $a->brackets, 'a(b(c)y(x)d(efgh(i(j))))';
$y->putNext($y->dup);
is_deeply $a->brackets, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
select($tree, $select)
Select matching children in a tree. A child can be selected via named value, array of values, a hash of values, a regular expression or a sub reference.
Parameter Description
1 $tree Tree
2 $select Method to select a child
Example:
if (1)
{my $t = Tree::Ops::new 'a';
for(1..2)
{$t->open ('b');
$t->single('c');
$t->close;
}
$t->single ('d');
is_deeply $t->print, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->𝘀𝗲𝗹𝗲𝗰𝘁('b')], ['b', 'b'];
}
Print the tree.
print($tree, $print)
String representation as a horizontal tree.
Parameter Description
1 $tree Tree
2 $print Optional print method
Example:
if (1)
{my $t = Tree::Ops::new 'a';
for(1..2)
{$t->open ('b');
$t->single('c');
$t->close;
}
$t->single ('d');
is_deeply $t->𝗽𝗿𝗶𝗻𝘁, <<END;
a
b
c
b
c
d
END
is_deeply [map {$_->user} $t->select('b')], ['b', 'b'];
}
brackets($tree, $print, $separator)
Bracketed string representation of a tree.
Parameter Description
1 $tree Tree
2 $print Print method
3 $separator Child separator
Example:
is_deeply $a->𝗯𝗿𝗮𝗰𝗸𝗲𝘁𝘀, 'a(b(c)y(x)y(x)d(efgh(i(j))))';
is_deeply $a->print(sub{@_}), <<END;
a
b
c
y
x
y
x
d
e
f
g
h
i
j
END
is_deeply $a->firstMost->𝗯𝗿𝗮𝗰𝗸𝗲𝘁𝘀, 'c';
is_deeply $a-> lastMost->𝗯𝗿𝗮𝗰𝗸𝗲𝘁𝘀, 'j';
Data Structures
Data structures use by this package.
Tree::Ops Definition
Child in the tree.
Output fields
children - Children of this child.
lastChild - Last active child chain - enables us to find the currently open scope from the start if the tree.
parent - Parent for this child.
user - User data for this child.
Private Methods
activeScope($tree)
Locate the active scope in a tree.
Parameter Description
1 $tree Tree
setParentOfChild($child, $parent)
Set the parent of a child and return the child.
Parameter Description
1 $child Child
2 $parent Parent
indexOfChildInParent($child)
Get the index of a child within the specified parent.
Parameter Description
1 $child Child
Index
1 activeScope - Locate the active scope in a tree.
2 brackets - Bracketed string representation of a tree.
3 by - Traverse a tree in order to process each child and return an array of the results of processing each child.
4 close - Close the current scope returning to the previous scope.
5 context - Get the context of the current child.
6 cut - Cut out a child and all its content and children, return it ready for reinsertion else where.
7 dup - Duplicate a parent and all its descendants.
8 first - Get the first child under the specified parent.
9 firstMost - Return the first most descendant child in the tree starting at this parent or else return undef if this parent has no children.
10 indexOfChildInParent - Get the index of a child within the specified parent.
11 isFirst - Return the specified child if that child is first under its parent, else return undef.
12 isLast - Return the specified child if that child is last under its parent, else return undef.
13 last - Get the last child under the specified parent.
14 lastMost - Return the last most descendant child in the tree starting at this parent or else return undef if this parent has no children.
15 new - Create a new child recording the specified user data.
16 next - Get the next sibling following the specified child.
17 open - Add a child and make it the currently active scope into which new children will be added.
18 prev - Get the previous sibling of the specified child.
19 print - String representation as a horizontal tree.
20 putFirst - Place a new child first under the specified parent and return the child.
21 putLast - Place a new child last under the specified parent and return the child.
22 putNext - Place a new child after the specified child.
23 putPrev - Place a new child before the specified child.
24 select - Select matching children in a tree.
25 setParentOfChild - Set the parent of a child and return the child.
26 single - Add one child in the current scope.
27 singleChildOfParent - Return the only child of this parent if the parent has an only child, else undef
28 step - Make the first child of the specified parent the parents previous sibling and return the parent.
29 stepBack - Make the previous sibling of the specified parent the parents first child and return the parent.
30 stepEnd - Make the next sibling of the specified parent the parents last child and return the parent.
31 stepEndBack - Make the last child of the specified parent the parents next sibling and return the parent.
32 unwrap - Unwrap the specified child and return that child.
33 wrap - Wrap the specified child with a new parent and return the new parent.
Installation
This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:
sudo cpan install Tree::Ops
Author
Copyright
Copyright (c) 2016-2019 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.