Hola,
No hay problema, y para ser sincero, llevo muchos años programando y este tipo de algoritmos realmente son dificiles de comprender, posiblemente todavía tenga algunos errores porque no lo probé extensamente,
Código PHP:
<?php
function walk($subdata) {
$tree = array();
$i = 0; $j = count($subdata);
while ($i < $j) {
$prev = $subdata[$i++];
$label = implode('_', $prev);
$tree[$label] = $label;
while (
$i < $j &&
count($prev) < count($subdata[$i]) &&
$prev === array_slice($subdata[$i], 0, count($prev))) {
if (!is_array($tree[$label]))
$tree[$label] = array();
$tree[$label][] = $subdata[$i++];
}
if (is_array($tree[$label]))
$tree[$label] = walk($tree[$label]);
}
return $tree;
}
function renum($subdata, $prev) {
global $done; $i = 0;
foreach ($subdata as $k => $s) {
$label = $prev . '.' . $i++;
if (is_array($s)) {
$done[] = $k . ' => ' . $label;
renum($s, $label);
} else {
$done[] = $s . ' => ' . $label;
}
}
}
$data = array(
"0_0_0",
"0_1_0",
"1_0_0",
"2",
"2_0_5",
"2_0_6",
"5",
"5_2",
"6",
"6_1_0",
"6_1_1",
"6_2_0"
);
$done = array();
sort($data, SORT_STRING);
foreach ($data as &$d)
$d = explode('_', $d);
$data = walk($data);
renum($data, 'P');
print_r($done);
?>
Saludos,