0

I need to traverse the following type of structures:

            P
        /   |   \
    E1      E2     E3 .....
   / \     /  \    |
  V1  V2  V1  V2   V3  .....
  |   |   |   |    / \
  T1  T2  T3  T4  T5  T6 .....

In order to form an associative array with the following elements:

        V1(key) = [T1(E1), T3(E2), ...]
        V2(key) = [T2(E1), T4(E2), ...]
        V3(key) = [T5(E3), T6(E3), ...]
        .....

Now here comes the tricky part: the structure is actually simplified. I don't know beforehand how many E-level nodes I'll actually need to deal with (3 in the drawing), or how many V-level nodes each of them has (but at least 1 will be there), and furthermore, each V-level node may also have multiple T-nodes.

I tried using a recursion function to do this (in PHP). I'll simplify the code because it has weird methods regarding some objects that don't really matter to the discussion. My current attempt results in:

        V1(key) = [T1(E1)]
        V2(key) = [T2(E1)]

That I think means that the traversal is only occurring going down the first E-level "branch".

This is my code:

$result = [];

$traverser = function($node) use (&$traverser, &$result) {
  $children = $node->getChildrenArray();

  foreach($children as $key=>$child){
    if ($child->nodeType() == 'v_node') {
      $v_node_key = $child->name;

      $t_nodes = $child->getChildrenArray();

        if ( !array_key_exists($v_node_key, $results) ){
          $results[$v_node_key] = [];
        }

        foreach($t_nodes as $keyt=>$t_node) {
          $info_array = $t_node->toArray();
          array_push($results[$v_node_key], $info_array);
        }

    } else if ($child->nodeType() == 'e_node') {
        // keep digging
        return $traverser($child);
    }

  }
};

$traverser($p_node); 

I think the problem is that once I call the $traverser function within the foreach it won't come back and resume from the previous state.

Can anyone advise on how I should be tackling this to get the result I placed above?

3 Answers 3

1

Well, this is a bit awkward and I'm still not entirely sure if this is the right motive, but I solved this by removing the return in my code.

I thought that the return would allow me to exit the nested function call, but rather I think it jumped out of the first function call (the $traverser($p_node); line).

Even so, by changing the return $traverser($child); line to $traverser($child); it did what it had to do.

Hope this helps anyone!

Sign up to request clarification or add additional context in comments.

Comments

0

I not show about what error you got but i suggest you to change you function to be this

function traverser($results, $node) {
$children = $node->getChildrenArray();

foreach($children as $key=>$child){
    if ($child->nodeType() == 'v_node') {
      $v_node_key = $child->name;

      $t_nodes = $child->getChildrenArray();

        if ( !array_key_exists($v_node_key, $results) ){
          $results[$v_node_key] = [];
        }

        foreach($t_nodes as $keyt=>$t_node) {
          $info_array = $t_node->toArray();
          array_push($results[$v_node_key], $info_array);
        }

    } else if ($child->nodeType() == 'e_node') {
      // keep digging
      return traverser($child);
    }

  }
  return $results;
}

Hope this help

1 Comment

How does this help to solve the question? I hope you know that his code is a valid anonymous function, and that he actually struggles with the algorithm and not the syntax?
0

Well, since you know that you'll have P->E->V->T-nodes, you could simply go for multiple foreach-loops, like this

foreach($p_node->getChildren() as $e_node) {
  $e_node_key = $e_node->name;
 foreach($e_node->getChildren() as $v_node) {
   $v_node_key = $v_node->name;
   foreach($v_node->getChildren() as $t_node) {
    $t_node_key = $t_node->name;
     // do whatever it needs to array_push to results
   }
 }
}

3 Comments

This is a possible approach, even if a bit brute-forcy. :) I'm afraid it's not a solution though, because I haven't really told the whole story in my question. I don't really know the depth of the tree, rather than it's width, so a recursive anonymous function is really the way to go. But thank you for your thoughts.
What I meant by the above comment is that sometimes I'll start traversing the level above the P-level in my description, others I might start at the E-level, so I'm trying to kill all the birds with half a stone. But it works! :D
Oh, well... then you're right. It's not a solution ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.