### ----------------------------------------------------
### -- ML::TriesWithFrequencies
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: lib/ML/TriesWithFrequencies/Trie.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 2
tree, &label, &children,
                       :$indent = '',
                       :@mid = ('├─', '│ '),
                       :@end = ('└─', '  '),
                       Str :$lb = '',
                       Str :$sep = ' => ',
                       Str :$rb = '',
                       Bool :$key-value-nodes = True,
                       Str :$trieValueLabel = 'VALUE';
                       ) {
        sub visit($node, *@pre) {
            my $suffix = '';
            if $key-value-nodes and $node.value.isa(Hash) and $node.value{$trieValueLabel}:exists {
                $suffix = $sep ~ $node.value{$trieValueLabel}
            }
            gather {
                if $node.&label ~~ $trieValueLabel {
                    if not $key-value-nodes {
                        take @pre[0] ~ $node.value
                    }
                } else {
                    take @pre[0] ~ $lb ~ $node.&label ~ $suffix ~ $rb;
                    my @children = sort $node.&children.grep({ $_.key ne $trieValueLabel });
                    my $end = @children.end;
                    for @children.kv -> $_, $child {
                        when $end { take visit($child, (@pre[1] X~ @end)) }
                        default { take visit($child, (@pre[1] X~ @mid)) }
                    }
                }
            }
        }

        flat visit($tree, $indent xx 2);
    }

}


