java中的arraylist顺序遍历

java中的arraylist顺序遍历,java,arraylist,traversal,inorder,Java,Arraylist,Traversal,Inorder,存在某个类的arraylist,其值为 Node,Depth,Value root,0,-2147483647 d3,1,4 e3,2,0 c5,2,0 c3,2,-3 c4,1,4 e3,2,0 c5,2,0 c3,2,-3 e6,1,4 d6,2,0 f4,2,0 f6,2,-3 f5,1,4 d6,2,0 f4,2,0 f6,2,-3 该对象存储了父节点ID(即对于节点d3,父节点(d3))根。该关系是这样的:给定节点X和depth=X.depth+1下的所有节点都是其子节点。 因此,根

存在某个类的arraylist,其值为

Node,Depth,Value
root,0,-2147483647
d3,1,4
e3,2,0
c5,2,0
c3,2,-3
c4,1,4
e3,2,0
c5,2,0
c3,2,-3
e6,1,4
d6,2,0
f4,2,0
f6,2,-3
f5,1,4
d6,2,0
f4,2,0
f6,2,-3
该对象存储了父节点ID(即对于节点d3,父节点(d3))根。该关系是这样的:给定节点
X
depth=X.depth+1
下的所有节点都是其子节点。 因此,根子级是:
d3、c4、f5、e6
d3子项是:
c3
e3
c5

现在,我必须编写一些代码来为它生成顺序遍历。 比如:

root,0,-2147483647
d3,1,4
e3,2,0
d3,1,4
c5,2,0
d3,1,4
c3,2,-3
d3,1,4
root,0,-2147483647
c4,1,4
e3,2,0
c4,1,4
c5,2,0
c4,1,4
c3,2,-3
c4,1,4
root,0,-2147483647
e6,1,4
d6,2,0
e6,1,4
f4,2,0
e6,1,4
f6,2,-3
e6,1,4
root,0,-2147483647
f5,1,4
d6,2,0
f5,1,4
f4,2,0
f5,1,4
f6,2,-3
f5,1,4
root,0,-2147483647
我已经编写了这样一个java方法

private static void inordertraversal() {

ListIterator <nextmove> iter = nmstack.listIterator();
while(iter.hasNext())
{
    nextmove node=iter.next();
    if(node.depth==CutoffDepth)
    {
        maxdepth=true;
    }

    if (!maxdepth)
    {
        System.out.println(node.To.Name+","+node.depth+","+node.weight);

    }
    else
    {
        nextmove parent=findparent(node.parent);
        if (node.parent!=0)
        {   
        System.out.println(node.To.Name+","+node.depth+","+node.weight);
        System.out.println(parent.To.Name+","+parent.depth+","+parent.weight);

        }
        else
        {
            System.out.println(parent.To.Name+","+parent.depth+","+parent.weight);
            System.out.println(node.To.Name+","+node.depth+","+node.weight);

        }
    }

}
}
inordertraversal()中的私有静态void{
ListIterator iter=nmstack.ListIterator();
while(iter.hasNext())
{
nextmove node=iter.next();
if(node.depth==截止深度)
{
maxdepth=true;
}
如果(!maxdepth)
{
System.out.println(node.To.Name+“,“+node.depth+”,“+node.weight”);
}
其他的
{
nextmove parent=findparent(node.parent);
if(node.parent!=0)
{   
System.out.println(node.To.Name+“,“+node.depth+”,“+node.weight”);
System.out.println(父项到.Name+”,“+父项深度+”,“+父项重量);
}
其他的
{
System.out.println(父项到.Name+”,“+父项深度+”,“+父项重量);
System.out.println(node.To.Name+“,“+node.depth+”,“+node.weight”);
}
}
}
}
但这不是通用的(如果深度增加/更改,它将不工作)也不完整。如何从我拥有的arraylist按顺序进行遍历。假设arraylist有节点名、深度和父级序列号。有人能给我一个指针吗?
这不是一个二叉树。

首先,您绝对希望获取
ArrayList
并首先从中构造一个树。您确实不希望在数据结构仍然表示为
ArrayList
时尝试按序遍历数据结构。这是您的第一个任务

完成后,按序遍历非常容易。它有以下方案:

  • 递归地探索左边的孩子
  • 处理该节点
  • 递归地探索正确的孩子

  • 但是,您有一个主要的问题,那就是您说这不是一个二叉树。顺序遍历只为二叉树定义,因为您必须先做左边的事情,然后是当前节点,然后是右边的路径。这对于任何其他类型的树都没有很好的定义。

    我看不到下面的例子gic是你想要的结果的背后……无论如何,如果你使用番石榴,你可能需要看一看(并实施)