Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遍历由DefaultMutableTreeNode生成的树_Java_Swing_Parsing_Tree - Fatal编程技术网

Java 遍历由DefaultMutableTreeNode生成的树

Java 遍历由DefaultMutableTreeNode生成的树,java,swing,parsing,tree,Java,Swing,Parsing,Tree,我们使用Java中指定的DefaultMutableTreeNode实现了一个树结构 有没有办法穿越它,那是内在的 如果没有,请建议其他技术。如果您想遍历树,可以调用breadthFirstEnumeration()或depthFirstEnumeration(),以遍历树中的所有节点 例如: DefaultMutableTreeNode root = ... Enumeration en = root.depthFirstEnumeration(); while (en.hasMoreEle

我们使用Java中指定的
DefaultMutableTreeNode
实现了一个树结构

有没有办法穿越它,那是内在的


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

如果您想遍历树,可以调用
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();
}

正确,面包优先是有序的。也支持预排序(先是根,然后是子项)(预排序计数)

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

  • broadthfirstumeration
  • 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]);
  }
♦ 预订单:如上所示
♦ 深度优先/邮购:
叶F1,叶F1,文件夹1
叶SF1,叶SF1,子文件夹1
叶SF2,叶SF2,子文件夹2,文件夹2,根
♦ 呼吸优先:

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

叶SF 1、叶SF 1、叶SF 2、叶SF 2这是对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.

你解析它是什么意思?通常,您会解析表达式以构建内部表示(如已有的树结构)。你只是想遍历树吗?很抱歉。是的,我的意思是遍历树。如何访问搜索算法返回的每个节点?你能给我指一个好的资源吗?最后是我需要的!我使用了一个ArrayList,在创建节点时添加了节点,这很好,但是因为我在不同的文件夹中使用了重复的条目,所以我需要为每个文件夹创建一个新的数组,然后它会很快变大。也感谢下面的答案,我可以处理他们的2倍快!起初我没有说出我的意思,我是想把它作为一个可视化辅助工具,但我无意中说出了它的字面意思。我现在已经改了,希望它更清晰,谢谢你的反馈。
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.