2009-09-24 98 views
14

我们使用Java中指定的DefaultMutableTreeNode实现了一个树结构。使用DefaultMutableTreeNode创建的遍历树

是否有任何方法遍历它,这是内置的?

如果不是,请提出其他技巧。

+0

解析它是什么意思?通常你会解析一个表达式来构建一个内部表示(就像你已经拥有的树结构一样)。你只是想要遍历树? – Adamski 2009-09-24 10:38:44

+0

对不起,我的意思是遍历它。 – fixxxer 2009-09-24 10:42:31

回答

14

如果你的意思是你想遍历树,你可以调用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(); 
} 
+0

如何访问搜索算法返回的每个节点?请你指点我一个好的资源。 – fixxxer 2009-09-24 11:11:02

+0

我刚刚添加了一些示例代码。 – Adamski 2009-09-24 12:10:23

+0

终于我需要!我使用了一个ArrayList,我添加了节点,因为我创建了它们,这很好,但是因为我在不同的文件夹中使用了重复的条目,所以我需要为每个文件夹创建一个新的数组,然后它会变得很快。还要感谢下面的答案,我可以以两倍的速度处理它们! – Daedric 2017-06-24 00:48:06

0

正确,breadtFirst是序。预购(第一根,然后孩子们)支持,以及(preorderEnumeration)

16

你必须在理论上四种方法从一个节点在树(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]); 
    } 

我可以改进与缩进成正比的水平,但它只是一个快速的黑客攻击。

那么,有什么区别?

  • preorderEnumeration =从树的顶部底部,因为如果你使用向下箭头走就
  • postorderEnumeration = depthFirstEnumeration =首先列出的第一条路径的最深的叶子,那么他们的父母,然后最深第二条路径的叶子等
  • breadthFirstEnumeration =列出在第一级上的元素,然后在第二级中的元素,依此类推

更具体:

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

♦预订:如上所示
♦深度优先/后序:
叶F1,叶F1,文件夹1
叶SF1,SF1叶,子文件夹1
叶SF 2,叶SF2,子文件夹2 ,文件夹2,根
♦BreathFirst:

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

1

下面是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. 
+0

本来我没有说我的意思,我的意思是它作为一种可视化协助,但我不小心说了。我现在改变了它,希望更清楚,谢谢你的反馈。 – neokyle 2013-07-15 03:37:21