Any Java Tree API which can be used to find distance between 2 terms
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
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 开发者_如何学JAVAto 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:
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).
精彩评论