NAME

Tree::Create::Size - Create a tree object of certain size

VERSION

This document describes version 0.02 of Tree::Create::Size (from Perl distribution Tree-Create-Size), released on 2016-03-31.

SYNOPSIS

 use Tree::Create::Size qw(create_tree);
 use MyNode;

 my $tree = create_tree(

     # either specify height + num_children ... (e.g. this will create a tree
     # with 1 + 2 + 4 + 8 + 16 nodes).
     height => 4,
     num_children => 2,

     # ... or specify num_nodes_per_level, e.g.
     num_nodes_per_level => [100, 3000, 5000, 8000, 3000, 1000, 300],

     class => 'MyNode',
     # optional
     #code_create_node => sub {
     #    my ($class, $level, $parent) = @_;
     #    $class->new(...);
     #},
 );

FUNCTIONS

create_tree(%args) -> any

Create a tree object of certain size.

This routine creates a tree object of certain size/dimension. You need to supply either height and num_children, or num_nodes_per_level.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • class => str

    Perl class name.

    Any class will do as long as it responds to parent and children. See the Role::TinyCommons::Tree::Node for more details on the requirement.

  • code_create_node => code

    By default, node object will be created with:

     $class->new()

    you can customize this by providing a routine to instantiate the node. The code will receive:

     ($class, $level, $parent)

    where $class is the class name (your code can naturally create nodes using any class you want), $level is the current level (0 for root node, 1 for its children, and so on), $parent is the parent node object. The code should return the node object.

    Your code need not set the node's parent(), connecting parent and children nodes will be performed by this routine.

    Example:

     sub {
         ($class, $level, $parent) = @_;
         $class->new( attr => 10*rand );
     }
  • height => int

    Height of tree.

    Height of 0 means the tree only consists of the root node. Height of 1 means the tree consists of root node and its children. Height of 2 increases this with the children's children. And so on.

  • num_children => int

    Number of children for each node.

  • num_nodes_per_level => array[int]

    Number of nodes per level.

    This argument specifies number of nodes per level and should be an array. The first element of the array corresponds to the total number of children nodes below the root node (i.e. the total number of nodes at level 1), the second element of the array corresponds to the total number of all that children's children (i.e. the total number of nodes at level 2, not the number of children for each child), and so on.

    The children will be distributed evenly among the parents.

Return value: (any)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Tree-Create-Size.

SOURCE

Source repository is at https://github.com/perlancar/perl-Tree-Create-Size.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Tree-Create-Size

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Role::TinyCommons::Tree::Node

Other modules to create tree: Tree::FromStruct, Tree::FromText, Tree::FromTextLines, Tree::Create::Callback.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.