Logo

Programming-Idioms

History of Idiom 17 > diff from v40 to v41

Edit summary for version 41 by foo:
New Perl implementation by user [foo]

Version 40

2019-09-26, 17:07:24

Version 41

2019-09-26, 19:52:40

Idiom #17 Create a Tree data structure

The structure must be recursive. A node may have zero or more children. A node has access to children nodes, but not to its parent.

Idiom #17 Create a Tree data structure

The structure must be recursive. A node may have zero or more children. A node has access to children nodes, but not to its parent.

Code
my $tree = {
           'Root' => {
                 'Child1' => {
                       'GrandChild1' => {},
                       'GrandChild2' => {}
                 },
                 'Child2' => {}
           }
};
Comments bubble
The natural way to create a tree in perl is using hash references.

Just declare it!

To add a new item:

$tree->{Root}->{Child2}->{GrandChild3} = {};