0
一个函数,它接受一个节点并复制该节点和所有相邻节点以创建一个与此节点完全相似的新结构。复制图形节点以创建节点网络的全新复制
甲
/\
乙CE
\/
d
应该创建新节点
一个节点对象通过
节点{限定的类似网络
Arraylist neighbors; //返回数组列表中的所有相邻节点
}
此代码是否工作?
public Node copyGraph(Node A){
HashTable<Node, Node> hash = new HashTable<Node, Node>();
return copyGraphWithHash(A, hash);
}
public Node copyGraphWithHash(Node A, HashTable<Node, Node> hash){
if (A.neighbours.size() == 0)
return null;
Node newfirst = null;
if(!hash.hasKey(A))
newfirst = new Node();
hash.add(A,newfirst);
}
for (Node n : A.neighbours()){
if (copyGraphWithHash(n, hash))
newfirst.neightbours.add(copyGraphWithHash(n, hash));
}
return newfirst;
}
请建议我在这里错过什么?