How to resolve the algorithm Tree from nesting levels step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Tree from nesting levels step by step in the Raku programming language

Table of Contents

Problem Statement

Given a flat list of integers greater than zero, representing object nesting levels, e.g. [1, 2, 4], generate a tree formed from nested lists of those nesting level integers where:

The generated tree data structure should ideally be in a languages nested list format that can be used for further calculations rather than something just calculated for printing.

An input of [1, 2, 4] should produce the equivalent of: [1, [2, [[4]]]] where 1 is at depth1, 2 is two deep and 4 is nested 4 deep. [1, 2, 4, 2, 2, 1] should produce [1, [2, [[4]], 2, 2], 1]. All the nesting integers are in the same order but at the correct nesting levels. Similarly [3, 1, 3, 1] should generate [[[3]], 1, [[3]], 1] Generate and show here the results for the following inputs:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Tree from nesting levels step by step in the Raku programming language

Source code in the raku programming language

sub new_level ( @stack --> Nil ) {
    my $e = [];
    push @stack.tail, $e;
    push @stack,      $e;
}
sub to_tree_iterative ( @xs --> List ) {
    my $nested = [];
    my @stack  = $nested;

    for @xs -> Int $x {
        new_level(@stack) while $x > @stack;
        pop       @stack  while $x < @stack;
        push @stack.tail, $x;
    }

    return $nested;
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), .&to_tree_iterative for @tests;


sub to_tree_recursive ( @list, $index is copy, $depth ) {
    my @so_far = gather while $index <= @list.end {
        my $t = @list[$index];

        given $t <=> $depth {
            when Order::Same {
                take $t;
            }
            when Order::More {
                ( $index, my $n1 ) = to_tree_recursive( @list, $index, $depth+1 );
                take $n1;
            }
            when Order::Less {
                $index--;
                last;
            }
        }
        $index++;
    }

    my $i = ($depth > 1) ?? $index !! -1;
    return $i, @so_far;
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), to_tree_recursive( $_, 0, 1 ).[1] for @tests;


use MONKEY-SEE-NO-EVAL;
sub to_tree_string_eval ( @xs --> Array ) {
    my @gap = [ |@xs, 0 ]  Z-  [ 0, |@xs ];

    my @open  = @gap.map( '[' x  * );
    my @close = @gap.map( ']' x -* );

    my @wrapped = [Z~] @open, @xs, @close.skip;

    return EVAL @wrapped.join(',').subst(:g, ']]', '],]') || '[]';
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), .&to_tree_string_eval for @tests;


  

You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the SSEM programming language
You may also check:How to resolve the algorithm Klarner-Rado sequence step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the OCaml programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Raku programming language