php - How to list all partial trees of a tree -


let's begin listing have looked @ , not looking for

i not want list permutations array - get permutations of php array?

i not want find combinations in order array - https://stackoverflow.com/a/38871855/1260548

the 2 above examples got me still generate many combinations. 50 nodes end billions if not trillions of combinations , think can reduce further tree structure.

what looking possible ordered combinations tree structured multidimensional array so

[1] --[2] --[4] [8] --[3] --[9] ----[5] [6] [7] 

what want find possible open nodes (even leaves/end nodes can open). 1 possible combination here numbers like

  • 1.2.3.4.5.8.9

node 1 here parent of 2 , 4. 8 parent of 3 , 9. 9 child of 8 parent of 5. other possible combinations are.

  • 1
  • 1.2.4
  • 1.6.7.8
  • 3.5.8.9
  • 3.5.6.7.8.9

it not possible open node if parent not open. example, if 1 not included 2 , 4 can not included. if 9 not included 5 can not included , if 8 not included 3, 9 , 5 can not included.

the following code use generate sample node structure test with. please note structure ordered , has fixed depth come function work order , depth.

$arr = []; $result = []; $xtotal = 0; $ytotal = 0; $ztotal = 0; ($x=0; $x<2; $x++) {   $arr[$xtotal] = array();   $ytotal = $xtotal+1;   ($y=0; $y<2; $y++) {     $arr[$xtotal][$ytotal] = array();     $ztotal = $ytotal+1;     ($z=0; $z<2; $z++) {       $arr[$xtotal][$ytotal][$ztotal] = array();       $ztotal++;     }     $ytotal = $ztotal+1;   }   $xtotal = $ytotal+1; } ($c=0; $c<5; $c++) {   $arr[$xtotal] = array();   $xtotal++; } 

so wondering how go writing function list these possible combinations?

edit: smaller set can list possible combinations.

[1] --[2] --[4] [8]  1 8 1.8 1.2 1.4 1.2.8 1.4.8 1.2.4 1.2.4.8 

i've come function seems looking for. however, in storing different combinations, can start running memory issues. if want 50 nodes, solution might not work depending on memory limits.

i used little bit different node generator (though did test yours too) allowed me more flexibility in creating random combinations:

$arr = []; $counter = 0; // can change (2,6) produce different numbers of root nodes for($i=1;$i<=mt_rand(2,6);$i++){     $curr = $counter++;     $arr[$curr] = [];     // guarantee first node (0) have children nodes (easier testing) - random other nodes     $child = ($curr == 0) ? true : rand(0,1);     if($child){         // can change (1,2)         for($j=1;$j<=mt_rand(1,2);$j++){             $curr2 = $counter++;             $arr[$curr][$curr2] = [];             $child2 = rand(0,1);             if($child2){                 // can change (1,2) here                 for($k=1;$k<=mt_rand(1,2);$k++){                     $curr3 = $counter++;                     $arr[$curr][$curr2][$curr3] = [];                 }             }         }     } } 

now calculating:

function treenodes($arr,&$results,$parent=null){     foreach($arr $k=>$a){         // here copy our current results - gives 1 current node closed (original), , 1 open (clone)         $clone = [];         foreach($results $key=>$result){             // if node allowed in result (parent on) - root nodes allowed             if($parent === null || in_array($parent,$result)){                 $clone[] = array_merge($result,array($k));             }         }         $results = array_merge($results,$clone);         // if node has children, run function on them         if(count($a)){             treenodes($a,$results,$k);         }     } } // start 1 option of no nodes open $results = [[]]; treenodes($arr,$results);  // show results - can order these way if you'd before printing print count($results)."\n"; foreach($results $result){     print implode(",",$result)."\n"; } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -