Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your recursive method needs to return a list of partial matches, instead of just a boolean. That would go a long way to solving both your problems (the need to return the list of matches, as well as finding multiple matches).</p> <p>Java-like pseudocode for the recursive function might look something like this:</p> <pre><code>findMatches(treeNode, searchNode) { if searchNode has no children { // search successful pairs = [] // empty list return [pairs] // list of lists } else { matches = [] // empty list searchChild = first child node of searchNode searchNode2 = searchNode with searchChild removed // NOTE: searchNode2 is created by doing a shallow copy of just the node // (not it's children) and then removing searchChild from the child list. for each treeChild in treeNode.children { if treeChild.value == searchChild.value { treeNode2 = treeNode with treeChild removed // also a shallow copy childMatches = findMatches(searchChild, treeChild) nodeMatches = findMatches(treeNode2, searchNode2) // cross-product for each nodeMatchPairs in nodeMatches { for each childMatchPairs in childMatches { fullMatchPairs = [(searchChild, treeChild)] + childMatchPairs + nodeMatchPairs // concatenate lists add fullMatchPairs to matches } } } } return matches } } </code></pre> <p>Notice that this function does not test treeNode.value == searchNode.value, or add this to the list. The caller needs to do that. This function needs to be run at every node of the tree.</p> <p>As currently designed, it probably uses too much memory, but that could be optimized.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload