Binary
NAME
Tree::Binary - Simple Binary Tree Role with pretty printing
SYNOPSIS
use Tree::Binary;
class IntTree does Tree::Binary[Int] {};
my IntTree(Str) $tree = "1(2)(3)";
say $tree;
1
āā“ā
2 3
DESCRIPTION
Tree::Binary is intended to be a simple role that can be used to represent binary trees. It's intended to be a basis for a series of different types for trees.
Tree::Binary does not include code for inserting or deleting nodes as this is dependent on the concreate class using it.
Tree::Binary
role Tree::Binary[
::ValueType=Any,
Tree::Binary::Role::Renderer :$gist-renderer=Tree::Binary::PrettyTree,
Tree::Binary::Role::Renderer :$Str-renderer=BasicStrRenderer,
]
The Tree::Binary Role
accepts one postional and two named parameters :
- ValueType
- The type of object it should allow (defaulting to Any)
- :$gist-renderer
- An output renderer used for creating the Tree::Binary's gist representation. The default for this is Tree::Binary::PrettyTree but any class that does Tree::Binary::Role::Renderer will work.
- :$Str-renderer
- An output renderer used for creating the Tree::Binary's Str representation. The default for this is BasicStrRenderer but any class that does Tree::Binary::Role::Renderer will work.
Construction
The default constructor takes two named arguments :
- ValueType :$value
- The value of the current node
- Array[Tree::Binary] :@nodes[2]
- An array of 0-2 Tree::Binary nodes that are the children of the current node.
The role also allows for basic string coercion where a tree can be represented with the following structure.
value(node1)(node2)
Where value
is a Str
that can be coerced into a ValueType
and node1
and node2
another Tree::Binary
representation. The Str
method should produce
a value that can be coered into a Tree::Binary of the appropriate ValueType
.
Alternate construction options using Str
coercion are :
# Coercion from a Str to a Tree::Binary
my Tree::Binary(Str) $tree1 = "1(a)(Ā£)";
# Using the from-Str constructor
my $tree2 = Tree::Binary.from-Str("1(a)(Ā£)");
Attributes
Methods
AUTHOR
Scimon Proctor <[email protected]>
COPYRIGHT AND LICENSE
Copyright 2021 Scimon Proctor
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.