1
JtreeとJavaを初めて使用しています。のEfghi = 7(すなわちカウント - ABCD、同様Jtree Javaのすべての親ノードの葉ノードの数を取得するには
-Abcd
--Efghi
---Pqrst
---Uvwxyz
---Xyza
---Hdwik
---Lmnop
---Bcdef
--Tqrsp
---Jumak
----Uoaha
----Lobte
-----Cshnt
----Karke
は、今私は= 14(ABCDのすべての子供たちの、つまりcount + 1)ABCDの数を取得したい : は、私はこのようなツリー構造を持っていますEfghiのすべてのリーフノード+1)
しかし、私はカウントを得ることができません。 は、ここでは、コードです:
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.io.*;
import java.util.*;
public class treeTest {
public treeTest(List<String> somelist) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(somelist.get(0));
DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
for(int i = 1;i<somelist.size();i++)
{
buildTreeFromString(model, somelist.get(i));
}
// UI
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(tree);
f.setSize(300, 300);
f.setLocation(200, 200);
f.setVisible(true);
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
DefaultMutableTreeNode rootNode = ((DefaultMutableTreeNode)tree.getModel().getRoot());
int n = tree.getModel().getChildCount(rootNode);
System.out.println(n);
}
private void buildTreeFromString(final DefaultTreeModel model, final String str) {
// Fetch the root node
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
// Split the string around the delimiter
String [] strings = str.split(" - ");
// Create a node object to use for traversing down the tree as it
// is being created
DefaultMutableTreeNode node = root;
// Iterate of the string array
for (String s: strings) {
// Look for the index of a node at the current level that
// has a value equal to the current string
int index = childIndex(node, s);
// Index less than 0, this is a new node not currently present on the tree
if (index < 0) {
// Add the new node
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
node.insert(newChild, node.getChildCount());
node = newChild;
}
// Else, existing node, skip to the next string
else {
node = (DefaultMutableTreeNode) node.getChildAt(index);
}
}
}
private int childIndex(final DefaultMutableTreeNode node, final String childValue) {
Enumeration<DefaultMutableTreeNode> children = node.children();
DefaultMutableTreeNode child = null;
int index = -1;
while (children.hasMoreElements() && index < 0) {
child = children.nextElement();
if (child.getUserObject() != null && childValue.equals(child.getUserObject())) {
index = node.getIndex(child);
}
}
return index;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("Filepath\Sample.txt"));
String line;
while ((line = reader.readLine()) != null) {
list.add(line);
}
reader.close();
new treeTest(list);
}
}
は、私は、ツリー内のすべての親のleafcountを得ることができるか、ツリーを使用せずに、その情報を取得する他の方法がある方法はありますか?
ありがとう、私は木を横断し、カウントを得ました –