代码之家  ›  专栏  ›  技术社区  ›  fixxxer

从defaultmutabletreenode生成的遍历树

  •  14
  • fixxxer  · 技术社区  · 16 年前

    我们使用 DefaultMutableTreeNode 在Java中指定。

    有没有什么方法可以穿过它,这是内置的?

    如果没有,请推荐其他技术。

    4 回复  |  直到 12 年前
        1
  •  14
  •   Adamski    16 年前

    如果你想穿过这棵树,你可以调用 breadthFirstEnumeration() depthFirstEnumeration() 以便遍历树中的所有节点。

    例子:

    DefaultMutableTreeNode root = ...
    
    Enumeration en = root.depthFirstEnumeration();
    while (en.hasMoreElements()) {
    
      // Unfortunately the enumeration isn't genericised so we need to downcast
      // when calling nextElement():
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement();
    }
    
        2
  •  16
  •   PhiLho    14 年前

    理论上,你有四种方法可以从一个节点走到树上( DefaultMutableTreeNode ):

    • breadthFirstEnumeration
    • depthFirstEnumeration
    • preorderEnumeration
    • postorderEnumeration

    但实际上,深度优先是作为后序实现的。
    JavaDoc对这些方法的差异有点简明扼要。我来这里寻找答案,但最后我自己做了测试,代码如下:

      TreeModel model = tree.getModel();
    
      DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
      // Just changing enumeration kind here
      Enumeration<DefaultMutableTreeNode> en = rootNode.preorderEnumeration();
      while (en.hasMoreElements())
      {
         DefaultMutableTreeNode node = en.nextElement();
         TreeNode[] path = node.getPath();
         System.out.println((node.isLeaf() ? "  - " : "+ ") + path[path.length - 1]);
      }
    

    我本可以根据等级来调整缩进,但这只是一个快速的修改。

    那么,有什么区别呢?

    • 预购 =从树的顶部到底部,就像你用向下箭头来移动它一样。
    • PostOrderEnumeration = 深度优先枚举 =首先列出第一条路径的最深叶,然后列出它们的父级,然后列出第二条路径的最深叶,等等。
    • breadthfirstEnumeration =列出第一级的元素,然后列出第二级的元素,依此类推。

    要更具体:

    + Root
      + Folder 1
        - Leaf F1
        - Leaf F1
     + Folder 2
        + Sub-folder 1
          - Leaf SF1
          - Leaf SF1
        + Sub-folder 2
          - Leaf SF2
          - Leaf SF2
    

    _墠预购:如上所示
    _墠depthfirst/postorder:
    叶F1,叶F1,文件夹1
    叶SF1,叶SF1,子文件夹1
    叶SF 2,叶SF2,子文件夹2,文件夹2,根目录
    呼吸优先:

    文件夹1,文件夹2
    叶F1,叶F1,子文件夹1,子文件夹2
    叶SF 1,叶SF 1,叶SF 2,叶SF 2

        3
  •  1
  •   neoakris    12 年前

    下面是对3种枚举方法的另一种描述,这可能更容易理解。

    en = root.breadthFirstEnumeration();
    //Enumeration lists all nodes at depth 0 (aka root)
    //Then all nodes at depth 1 (aka root's children, top to bottom ordering)
    //Then all nodes at depth 2, and so on till max depth reached
    
    en = root.preorderEnumeration(); 
    //Imagine your JTree is fully expanded (where each node = a row)
    //Enumeration will list nodes from top to bottom (regardless of leaf or not)
    
    en = root.postorderEnumeration(); //Equivalent to root.depthFirstEnumeration();
    //Imagine a fully expanded copy of your JTree (where each node = a row)
    //This will allow you to visualize what Enumeration List will look like
    while(treecopy.hasNodes() ) {
    list 1st leaf sighted going from top to bottom, then remove that leaf }
    //as the leafs are removed, branches then become leafs, and root is last enumerated.
    
        4
  •  0
  •   Jens    14 年前

    对,breadtfirst不太好。也支持预排序(先是根目录,然后是子目录)(预排序)

    推荐文章