簡體   English   中英

如何獲取Jtree Java中所有父節點的葉節點數

[英]How can i get the count of leaf nodes of all parent nodes in Jtree Java

我是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);
    }
}

有沒有什么方法可以得到樹中每個父項的葉子數量,或者有沒有其他方法來獲取該信息而不使用樹?

您可以通過以下兩種方式之一獲得計數:

  1. 在構建JTree節點時,可以累積所需的計數
  2. 另一種方法是遍歷樹,然后使用node實例的getChildCount()方法,您可以獲得子節點的計數

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM