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 :

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,
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:
If the question is to find the minimum number of steps from A to B (which is the distance), i would implement a
distanceToMethod 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
parentattribute isnull(because the root, which is the topmost node, has no parent). All other nodes in the tree haveparent!=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
Nodeis the java class above):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:
Or print the value of a parent’s node:
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:
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).