Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:29:20+00:00 2026-05-11T09:29:20+00:00

I’m writing some code that uses a Tree (a regular tree that can have

  • 0

I’m writing some code that uses a Tree (a regular tree that can have an unlimited number of nodes, but no crossover, i.e. two parent nodes will not point the the same child node). Anyway, two things:

1) Are there any well-known algorithms for finding a sub-tree within a tree.

2) Are there any Java libraries (or any libraries for that matter) that already implement this algorithm? Even if there are none, can anyone recommend any good general purpose Java tree library?

I want to use these trees for holding data in a tree format, not for their searching capabilities.

To expand a bit: I’m using the tree as part of game to keep a history of what happens when a certain events happen. For example, an A can hit a B which can hit two A’s which can hit another two A’s etc.

That would look something like:

    A     |     B    /   A   / \   A   A    / \   A   A 

Of course there’s more than just A and B. What I want to do is (for an achievement system) is be able to tell when, say an A has hit two A’s:

  A  / \ A   A 

I want to be able to easily know if the first tree contains that subtree. And I don’t want to have to write all the code for doing so if I don’t have to 🙂

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:29:21+00:00Added an answer on May 11, 2026 at 9:29 am

    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.

    From your explanations, I’m not sure whether the search tree

      A  / \ A   A 

    should match this tree:

      A  /|\ A C A 

    (i.e. if non-matching children are supposed to be ignored.)

    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 Node class. Feel free to play with it:

    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() < searchTree.children.size()) {             return false;         }          boolean result = true;         int treeChildrenIndex = 0;          for (int searchChildrenIndex = 0;                  searchChildrenIndex < searchTree.children.size();                  searchChildrenIndex++) {              // Skip non-matching children in the tree.             while (treeChildrenIndex < tree.children.size()                   && !(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<Node> children;      public Node(char val) {         value = val;         children = new Vector<Node>();     }      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();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer dont think there is any (easy) way to validate sql… May 12, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer Q1: The calls are serialized. What signals are doing internally… May 12, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer The bad but easy way to do this is to… May 12, 2026 at 6:24 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.