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

  • SEARCH
  • Home
  • 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 984285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:59:28+00:00 2026-05-16T04:59:28+00:00

My requirement is 1) I should construct a tree like the one which is

  • 0

My requirement is

1) I should construct a tree like the one which is given below in the figure.

2) I need to then compute the distance between two terms say C and D. or D and F. ( as per the figure A and B are categories C,D,E and F are terms under the respective categories.

Expected results :

Search Terms ———- Distance

C and D ———————- 2

C and F ———————- 4

D and E———————- 4


Link for image :

https://t3.gstatic.com/images?q=tbn:N-Lb_jjMYri3aM:http://skrud.net/files/binary_tree_boat_race.png&t=1

alt text

Is there any suitable Java API which could do this functionality for me or I should write a algorithm on my own.

If at all there are no suitable API’s available where can I look for an algorithm to implement this….

Would appreciate your timely help

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-16T04:59:28+00:00Added an answer on May 16, 2026 at 4:59 am

    Since this is tagged as homework, you are probably expected to write it yourself.

    Usually when it comes to traversing a tree, you don’t have too many chances of optimizing, because starting at a node, you only have the options to visit the parent node or the child nodes. (As opposed to a graph, where you might apply Dijkstra’s algorithm)

    Just try it, i would recommend recursion.

    A Node usually looks like this:

    public class Node {
      public String value; //contains "C" or "D" etc
      public List<Node> children = new ArrayList<Node>();
      public Node parent;
      public Node(Node parent){
        this.parent = parent;
      }
      public Node(Node parent, String value){
        this.parent = value;
        this.value = value;
      }
      public boolean equals(Object n){//Nodes are equal if they have the same value
        return value.equals(((Node)n).value);
      }
    }
    

    If the question is to find the minimum number of steps from A to B (which is the distance), i would implement a distanceTo Method that traverses the tree and looks for the target Node until it is found. If you never worked with trees before, that might be a bit tricky.


    Ok, since you never worked with Trees before:
    A tree is a data structure that contains a certain amount of Nodes. The connections between the Nodes are represented by references between the Nodes.
    Each Node has a reference to it’s parent Node (the one above) and a list of child nodes (the nodes below). A Node can only have one parent, but any number of children. A node without any children is called a leaf. The root node is usually identifyable because it’s parent attribute is null (because the root, which is the topmost node, has no parent). All other nodes in the tree have parent!=null.

    The starting point for a Tree is the root Node. The following code creates the topmost 3 Nodes from your picture (where the type Node is the java class above):

    Node nodeRoot = new Node(null, "root"); //create a node with parent=null
    Node nodeA = new Node(nodeRoot, "A");
    Node nodeB = new Node(nodeRoot, "B");
    nodeRoot.children.add(nodeA); //you could also place this functionality
    nodeRoot.children.add(nodeB); // in the constructor of Node
    

    You now have 3 a tree (starting at nodeRoot) with 3 nodes: “root”, “A” and “B”.

    You could now show all nodes that are direct children of the root node:

    for(Node child:nodeRoot.children){
       System.out.println(child.value);
    }
    /* prints:
    A
    B
    */
    

    Or print the value of a parent’s node:

    System.out.println(nodeA.parent.value);
    /*prints:
    root
    */
    

    Try to complete the code to create a tree that represents the one in your picture!

    To continue (-> tree traversal), I would strongly advise you to read something about it first. Focus on Depth-First-Traversal and Breadth-First-Traversal and make sure you understand the difference.


    Regarding “Non-Binary Trees”:
    This does work very well for non-binary trees. The definition of Binary Trees states:

    a binary tree is a tree data structure in which each node has at most two children.

    Since I used a List of Nodes in the java class above, you can use any number of children per node. In fact, you would have to invest extra work to make the class compliant to a binary tree – which means making sure that each node can only have 0, 1 or 2 children.

    For non-binary trees you can a) use the class above and b) apply the same algorithms (BFS/DFS).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a requirement where in the text field the first character should be
My requirement is to develop a page with 4 sections, which cannot have a
My requirement A table needs to maintain a status column. This column represents one
A requirement for an ASP.Net 2.0 project I'm working on limits a certain field
My requirement is I have server J2EE web application and client J2EE web application.
My requirement is just to display a set of values retrieved from database on
We have a requirement in project to store all the revisions(Change History) for the
I have a requirement to make a large amount of code MISRA compliant. First
We have a requirement to increase the functionality of a grid we are using
What I have? I have below requirements: Search the database and return TreeNode objects

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.