Java中的树结构?

时间:2014-06-27 02:53:33

标签: java tree

我想实现树结构。树中的每个节点都包含我选择的对象,以及指向每个子节点以及可能是父节点的链接。我想知道Java中是否存在这样的东西,例如:

T randomObject = new randomObject();
Node<T> root;
root.setObject(randomObject);
root.addChild( ... 
....
root.getFirstChild().getObject().getObjectProperty();

我已经研究过Node结构(org.w3c.dom.Node),但它似乎无法存储对象,而且似乎更适合解析文档。

我还研究了DefaultMutableTreeNode,MutableTreeNode和TreeNode之类的东西,但是我还没有找到明确的用法示例。我遇到了很多问题。例如,当将DefaultMutableTreeNode作为参数传递时(似乎不起作用),或者在获取DefaultMutableTreeNode的子节点时,它似乎只返回一个TreeNode,当我明确地将DefaultMutableTreeNode添加为子节点时。

我是Java的新手,所以任何信息都会非常有用。 Oracle文档没有用处。我使用了Vector结构,它很容易使用,可以存储对象并且很容易访问,并且想知道是否存在类似的东西。感谢。

2 个答案:

答案 0 :(得分:1)

查看https://github.com/poshjosh/bcuitreebuilder

用于从文档,文件等构建TreeNode的轻量级java库

public class ReadMe {

    public static void main(String [] args) {

        TreeBuilderFactory treeBuilderFactory = new TreeBuilderFactoryImpl();

        // For displaying each JTree
        //
        JScrollPane scrollPane  = new JScrollPane();
        scrollPane.setBounds(0, 0, 300, 300);

        // DocumentTreeBuilder 
        //
        Document doc = loadDocument(
             new File(System.getProperty("user.home")+"/Documents/Desktop/welcome.xml"));

        DOMTreeBuilder domTreeBuilder = treeBuilderFactory.getDOMInstance();

        Filter<Node> nodeFilter = null; // May be null

        TreeNode docRootNode = domTreeBuilder.build(doc, nodeFilter);

        JTree documentTree = new JTree(docRootNode);

        // Display the JTree
        //
        scrollPane.setViewportView(documentTree);
        JOptionPane.showMessageDialog(null, scrollPane);


        // FileTreeBuilder
        //
        File dir = new File(System.getProperty("user.home")+"/Documents");

        TreeBuilder<File> fileTreeBuilder = 
                 treeBuilderFactory.getInstance(TreeBuilderFactory.FILE);
        // This also works
        //FileTreeBuilder fileTreeBuilder = treeBuilderFactory..getFileInstance(); 

        Filter<File> fileFilter = new Filter<File>() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith(".docx");
            }
        };

        TreeNode fileRootNode = fileTreeBuilder.build(dir, fileFilter);

        JTree fileTree = new JTree(fileRootNode);

        // Display the JTree
        //
        scrollPane.setViewportView(fileTree);
        JOptionPane.showMessageDialog(null, scrollPane);


        // MapTreeBuilder
        //
        MapTreeBuilder mapTreeBuilder = treeBuilderFactory.getMapInstance();

        final HashMap map = new HashMap();
        map.put("boolean", Boolean.TRUE);
        map.put("number", 100);
        map.put("List", new String[]{"1", "2", "3"});
        HashMap grandChildren = new HashMap();
        grandChildren.put("grandChild", "I am a grand child");
        map.put("hasChildren", grandChildren);

        Map.Entry rootEntry = mapTreeBuilder.createRootEntry(map);

        TreeNode mapRootNode = mapTreeBuilder.build(rootEntry, null);

        JTree mapTree = new JTree(mapRootNode);

        // Display the JTree
        //
        scrollPane.setViewportView(mapTree);
        JOptionPane.showMessageDialog(null, scrollPane);
    }

    private static Document loadDocument(File file) {
        Document doc;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            doc = docBuilder.parse(file);
        }catch (SAXException | IOException | ParserConfigurationException e) {
            e.printStackTrace();
            doc = null;
        }
        return doc;
    }
}

答案 1 :(得分:0)

几天前我想要类似的东西。这就是我所做的:

public class MyTreeNode extends DefaultMutableTree{
  //add all your objects here
}

DefaultMutableTree有预定义的方法,如add() - &gt;要添加子节点,它使用枚举并提供标准方法来使用预订和后序处理来访问子节点。