java 由 DefaultMutableTreeNode 制成的遍历树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:39:46  来源:igfitidea点击:

Traversing tree made from DefaultMutableTreeNode

javaswingparsingtree

提问by fixxxer

We have a tree structure implemented using the DefaultMutableTreeNodespecified in Java.

我们有一个使用DefaultMutableTreeNodeJava 中指定的实现的树结构。

Is there any way of traversing it, that is inbuilt?

有没有内置的遍历它的方法?

If not, please suggest other techniques.

如果没有,请建议其他技术。

采纳答案by Adamski

If you mean you want to traverse the tree you can call breadthFirstEnumeration()or depthFirstEnumeration()in order to iterate over all nodes in the tree.

如果您的意思是要遍历树,则可以调用breadthFirstEnumeration()depthFirstEnumeration()以遍历树中的所有节点。

Example:

例子:

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

回答by PhiLho

You have in theory four ways to walk the tree from a node (DefaultMutableTreeNode):

理论上,您有四种方法可以从节点 ( DefaultMutableTreeNode)遍历树:

  • breadthFirstEnumeration
  • depthFirstEnumeration
  • preorderEnumeration
  • postorderEnumeration
  • breadthFirstEnumeration
  • depthFirstEnumeration
  • preorderEnumeration
  • postorderEnumeration

but actually depth first is implemented as postorder.
The JavaDoc is a bit terse on the differences on these methods. I came here looking for an answer, but I ended by doing the test myself, with a code looking like:

但实际上深度优先是作为后序实现的。
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]);
  }

I could have refined with indentation proportional to level, but it was just a quick hack.

我可以用与级别成正比的缩进进行细化,但这只是一个快速的技巧。

So, what are the differences?

那么,有什么区别呢?

  • preorderEnumeration= from top of tree to bottom, as if you were using the down arrow to walk it
  • postorderEnumeration= depthFirstEnumeration= first list the deepest leafs of the first path, then their parent, then the deepest leafs of the second path, etc.
  • breadthFirstEnumeration= list the elements at the first level, then the elements at the second level, and so on
  • preorderEnumeration= 从树的顶部到底部,就像您使用向下箭头来行走一样
  • postorderEnumeration==depthFirstEnumeration首先列出第一条路径最深的叶子,然后是它们的父,然后是第二条路径的最深叶子,依此类推。
  • breadthFirstEnumeration= 列出第一层的元素,然后是第二层的元素,依此类推

To be more concrete:

更具体地说:

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

? Preorder: as shown above
? DepthFirst/Postorder:
Leaf F1, Leaf F1, Folder 1
Leaf SF1, Leaf SF1, Sub-folder 1
Leaf SF 2, Leaf SF2, Sub-folder 2, Folder 2, Root
? BreathFirst:
Root
Folder 1, Folder 2
Leaf F1, Leaf F1, Sub-folder 1, Sub-folder 2
Leaf SF 1, Leaf SF 1, Leaf SF 2, Leaf SF 2

? 预购:如上图
?深度优先/后序:
叶 F1、叶 F1、文件夹 1
叶 SF1、叶 SF1、子文件夹 1
叶 SF 2、叶 SF2、子文件夹 2、文件夹 2、根
?BreathFirst:

文件夹 1、文件夹 2
Leaf F1、Leaf F1、子文件夹 1、子文件夹 2
Leaf SF 1、Leaf SF 1、Leaf SF 2、Leaf SF 2

回答by neokyle

Here's another description of the 3 enumeration Methods, that may be easier to understand.

这里对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.

回答by Jens

Correct, breadtFirst is inorder. Preorder (First root, then children) is supported as well (preorderEnumeration)

正确,breadtFirst 是有序的。还支持预排序(第一个根,然后是子项)(preorderEnumeration)