如何在jtree java中获取所有父节点的叶节点数 [英] How can I get the count of leaf nodes of all parent nodes in jtree java

查看:68
本文介绍了如何在jtree java中获取所有父节点的叶节点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Jtree和Java的新手。



所以,我有这样的树形结构:



-Abcd

--Efghi

--- Pqrst

--- Uvwxyz

- -Xyza

--- Hdwik

--- Lmnop

--- Bcdef

--Tqrsp < br $>
--- Jumak

---- Uoaha

---- Lobte

----- Cshnt

---- Karke



现在我想得到Abcd = 14的计数(即所有Abcd +孩子的数量) 1)类似地,Abcd - Efghi = 7(即Efghi的所有leafNodes的数量+ 1)



但是我无法计算。这是代码:



I am new to Jtree and Java.

So, I have a tree structure like this :

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

Now i want to get the count of Abcd = 14 (i.e Count of all children of Abcd+1) similarly, Abcd - Efghi = 7 (i.e Count of all leafNodes of Efghi+1)

But I am not able to get the count. Here's the code :

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





有什么方法可以获得每个父母的子计数树或是否有其他方法可以在不使用树的情况下获取信息?



我尝试过的方法:



我试过创建一个Jtree。



Is there any way that i could get leafcount of every parent in the tree or is there any other way to get that information without using tree?

What I have tried:

I have tried creating a Jtree.

推荐答案

ppolymorphe写道:
ppolymorphe wrote:

我不会用树来解决这个问题,它只会让事情复杂化。

I wouldn't use a tree for this problem, it just complicate things.




引用:

那我该怎么办,能帮帮我吗

Then what should i do, can you help me



这是问题分析的问题:你可以建立一个树,但是你需要吗?

正如你已经找到的那样,你需要读1行1


That is a matter of "problem analyze" : you can build a tree, but do you need to ?
As you have already found, you need to read lines 1 by 1

while ((line = reader.readLine()) != null) {
  // your code for a line goes here.
}



当你有这条线:Abcd - Tqrsp - Jumak - Lobte

你需要计算你看到每个子串的次数:

Abcd - Tqrsp - Jumak - Lobte

Abcd - Tqrsp - Jumak

Abcd - Tqrsp

Abcd

当你得到字符串时,你需要记住每一对字符串(键)和计数器。

最简单的解决方案是用于键的数组和用于计数器的数组,它不能很好地扩展,但它可以满足您的需求。

对于更有效的解决方案,某些语言有关联数组,Java有 hashmaps



如何获得每个子字符串?

你已经使用拆分:


When you have this line: "Abcd - Tqrsp - Jumak - Lobte"
You are required to count how many times you have seen each substring as:
"Abcd - Tqrsp - Jumak - Lobte"
"Abcd - Tqrsp - Jumak"
"Abcd - Tqrsp"
"Abcd"
As you get strings, you need to remember each couples of string (key) and counter.
The most simple solution is an array for keys and an array to counters, it will not scale well, but it will do what you need.
For a more efficient solution,some languages have associative arrays, Java have hashmaps.

How to get each substring ?
you already use split:

// Split the string around the delimiter
String [] strings = str.split(" - ");



当你需要更复杂的东西时,去RegEx(正则表达式)

此RegEx将删除字符串的最后一部分^(。+)\s-\ [[ - - ] +


When you need something more sophisticated, go RegEx (Regular Expressions)
This RegEx will drop the last part of a string "^(.+)\s-\s[^-]+






只是一些有趣的链接来帮助构建和调试RegEx。

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示给你R egEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]



Nota:我不是Java程序员,我只是一名程序员。
"

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]

Nota: I am not a Java programmer, I am just a programmer.


这篇关于如何在jtree java中获取所有父节点的叶节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆