NAME

Data::Random::Tree - Create a random tree

VERSION

This document describes version 0.06 of Data::Random::Tree (from Perl distribution Data-Random-Tree), released on 2016-07-03.

SYNOPSIS

use Data::Random::Tree qw(create_random_tree);
use MyNode;
use MyOtherNode;

my $tree = create_random_tree(
    num_objects_per_level => [100, 3000, 5000, 8000, 3000, 1000, 300],
    classes => ['MyNode', 'MyOtherNode'],
    # optional
    #code_instantiate_node => sub {
    #    my ($class, $level, $parent) = @_;
    #    $class->new(...);
    #},
);

FUNCTIONS

create_random_tree(%args) -> any

Create a random tree.

This routine creates a random tree object. You need to supply at least one node class. A node class is a class that must at least have these attributes: parent, and children. See Role::TinyCommons::Tree::Node if you want to use a role to enforce this for your class.

TODO: allow specifying the minimum/maximum number of objects (per-level).

TODO: allow specifying the minimum/maximum level.

TODO: allow varying the number of children of each node.

TODO: allow to customize the distribution of class instances (e.g. root node up until level 3 should only be C1 nodes, and so on).

TODO: Allow setting node attributes with random values (without having the user use code_instantiate_node).

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • classes* => array[str]

    Class(es) to use to instantiate node objects.

    The nodes will be created from a random pick of this list of classes. If you only supply one class, then all nodes will be of that class.

  • code_instantiate_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 );
    }
  • num_objects_per_level* => array[int]

    Number of objects per level.

    This argument specifies number of objects 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 objects 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 objects at level 2, not the number of children for each child), and so on.

Return value: (any)

HOMEPAGE

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

SOURCE

Source repository is at https://github.com/perlancar/perl-Data-Random-Tree.

BUGS

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

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 Data::Random::* modules.

Tree::FromStruct

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.