値を持ついくつかのクラスの配列リストがあります
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 、Parent(d3)-root の場合)。関係は、特定のノードの下にあるすべてのノードがその子であるようなもの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);
}
}
}
}
しかし、これはジェネリックではなく(深さが増加/変更された場合は機能しません)、不完全でもあります。私が持っている配列リストからこの順序通りのトラバーサルを行うにはどうすればよいですか。Arraylist にノード名、その深さ、およびその親のシリアル番号があるとします。誰かが私にポインタを与えることができますか?これは二分木ではありません。