如何获取Jtree Java中所有父节点的叶节点数

时间:2017-06-18 10:32:34

标签: java swing count jtree tree-traversal

我是Jtree和Java的新手。 我有这样的树结构:

-Abcd
 --Efghi
  ---Pqrst
  ---Uvwxyz
  ---Xyza
  ---Hdwik
  ---Lmnop
  ---Bcdef
 --Tqrsp
  ---Jumak
   ----Uoaha
   ----Lobte 
    -----Cshnt
   ----Karke 

现在我想得到Abcd = 14的计数(即Abcd的所有孩子的数量+ 1) 类似地,Abcd - Efghi = 7(即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,还是有其他方法可以在不使用树的情况下获取该信息?

1 个答案:

答案 0 :(得分:0)

你可以用两种方式之一来计算你的数量:

  1. 在构建JTree节点时,您可以累积正在寻找的计数
  2. 另一种方法是遍历树,然后使用getChildCount()实例的node方法,您可以获得子节点的计数
  3. 希望这有帮助!