object Main {
private def clone_rec(root: Node, nodes_completed: HashMap[Node, Node]): Node = {
if (root == null) return null
val newNode = new Node(root.data)
nodes_completed.put(root, newNode)
for (p <- root.friends) {
val x = nodes_completed(p)
if (x == null) newNode.friends.addOne(clone_rec(p, nodes_completed))
else newNode.friends.addOne(x)
}
newNode
}
def clone(root: Node): Node = {
val nodes_completed = new HashMap[Node, Node](){override def default(key:Node):Node = null}
clone_rec(root, nodes_completed)
}
def main(args: Array[String]): Unit = {
val vertices = CloneGraph.createTestGraphDirected(7, 18)
CloneGraph.printGraph(vertices(0))
val cp = clone(vertices(0))
println()
println("After copy.")
CloneGraph.printGraph(cp)
val set = new HashSet[Node]()
println(CloneGraph.are_graphs_equal_rec(vertices(0), cp, set))
}
}