Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like a straightforward algorithm: Find the root of the search tree in the game tree and check whether the children of the search tree are a subset of the children in the game tree.</p> <p>From your explanations, I'm not sure whether the search tree</p> <pre><code> A / \ A A </code></pre> <p>should match this tree:</p> <pre><code> A /|\ A C A </code></pre> <p>(i.e. if non-matching children are supposed to be ignored.)</p> <p>Anyway, here's the code I just toyed around with. It's a fully running example and comes with a main method and a simple <code>Node</code> class. Feel free to play with it:</p> <pre><code>import java.util.Vector; public class PartialTreeMatch { public static void main(String[] args) { Node testTree = createTestTree(); Node searchTree = createSearchTree(); System.out.println(testTree); System.out.println(searchTree); partialMatch(testTree, searchTree); } private static boolean partialMatch(Node tree, Node searchTree) { Node subTree = findSubTreeInTree(tree, searchTree); if (subTree != null) { System.out.println("Found: " + subTree); return true; } return false; } private static Node findSubTreeInTree(Node tree, Node node) { if (tree.value == node.value) { if (matchChildren(tree, node)) { return tree; } } Node result = null; for (Node child : tree.children) { result = findSubTreeInTree(child, node); if (result != null) { if (matchChildren(tree, result)) { return result; } } } return result; } private static boolean matchChildren(Node tree, Node searchTree) { if (tree.value != searchTree.value) { return false; } if (tree.children.size() &lt; searchTree.children.size()) { return false; } boolean result = true; int treeChildrenIndex = 0; for (int searchChildrenIndex = 0; searchChildrenIndex &lt; searchTree.children.size(); searchChildrenIndex++) { // Skip non-matching children in the tree. while (treeChildrenIndex &lt; tree.children.size() &amp;&amp; !(result = matchChildren(tree.children.get(treeChildrenIndex), searchTree.children.get(searchChildrenIndex)))) { treeChildrenIndex++; } if (!result) { return result; } } return result; } private static Node createTestTree() { Node subTree1 = new Node('A'); subTree1.children.add(new Node('A')); subTree1.children.add(new Node('A')); Node subTree2 = new Node('A'); subTree2.children.add(new Node('A')); subTree2.children.add(new Node('C')); subTree2.children.add(subTree1); Node subTree3 = new Node('B'); subTree3.children.add(subTree2); Node root = new Node('A'); root.children.add(subTree3); return root; } private static Node createSearchTree() { Node root = new Node('A'); root.children.add(new Node('A')); root.children.add(new Node('A')); return root; } } class Node { char value; Vector&lt;Node&gt; children; public Node(char val) { value = val; children = new Vector&lt;Node&gt;(); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append('('); sb.append(value); for (Node child : children) { sb.append(' '); sb.append(child.toString()); } sb.append(')'); return sb.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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