Mind bender array tree flattening

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    Mind bender array tree flattening

    I have multidimensiona l (tree) array of form like this:

    Code:
    $a = array
    (
        "t" => array("b")
        "d" => array(
                     "p" => array("b")
                     "n" => array(
                                  "p" => array ( "b")
                                  )
                    )
        "p" => array("b")
        "n" => array(
                     "p" => array("b")
                     )
    )
    That needs converting into 2 dimensional (tree respecting) array like this:

    Code:
    $a = array
    (
    	array("t", "b"),
    	array("d", "p", "b"),
    	array("d", "n", "p", "b"),
    	array("p", "b"),
    	array("n", "p", "b")
    )
    Any ideas!? Solve this and beer and pizza is on me :)
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Hm maybe this should be moved to algorithms

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      I'll move it to algorithms. :)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        *goes to drawing board*

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Looks like a depth-first search.
          Code:
          {
            current node = root
            current path = nothing
            PrintPath(node, path)
          }
          
          PrintPath(node, path){
            if no children -> print path and return
            foreach childNode on current node{
              PrintPath(childNode, path + childnode)
            }
          }
          path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.

          Comment

          • zorgi
            Recognized Expert Contributor
            • Mar 2008
            • 431

            #6
            Language is php. I came up with this:

            Comment

            • zorgi
              Recognized Expert Contributor
              • Mar 2008
              • 431

              #7
              Originally posted by jkmyoung
              Looks like a depth-first search.
              Code:
              {
                current node = root
                current path = nothing
                PrintPath(node, path)
              }
              
              PrintPath(node, path){
                if no children -> print path and return
                foreach childNode on current node{
                  PrintPath(childNode, path + childnode)
                }
              }
              path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.
              Thanks to your post I came up with much neater solution.

              Code:
              printPath($a);
              
              function printPath($node, $path = array()){
              	if(!is_array($node)){
              		$path[] = $node;
              		print_r($path);	
              		return;
              	}
              	
              	foreach($node as $key => $child_node){
              		$p = $path;		
              		if(is_array($child_node)){
              			$p[] = $key;
              			printPath($child_node, $p);
              		}else{
              			printPath($child_node, $p);	
              		}			
              	}
              }
              Thanx. Beer and pizza is on me if we ever meet :)

              Comment

              Working...