#!/usr/bin/perl -s use v5.24; use Test2::V0; use Data::Dump; use experimental 'signatures'; our ($tests, $examples); run_tests() if $tests || $examples; # does not return die <@*, map {defined ? $nodes{$_} = [] : undef} @children; } $nodes{$root}; } # Recursively invert the tree. sub invert_tree ($tree) { invert_tree($_) for grep defined, $tree->@[1, 2]; $tree->@[1, 2] = $tree->@[2, 1]; $tree; } ### Examples and tests sub run_tests { SKIP: { skip "examples" unless $examples; is invert_tree( build_tree('1:2,3', '2:4,5', '3:6,7', '4', '5', '6', '7')), [1, [3, [7, undef, undef], [6, undef, undef]], [2, [5, undef, undef], [4, undef, undef]]], 'example'; } SKIP: { skip "tests" unless $tests; is invert_tree(build_tree('1:2,3', '2:4', '3:,7', '4', '7')), [1, [3, [7, undef, undef], undef], [2, undef, [4, undef, undef]]], 'handle missing childs'; } done_testing; exit; }