Since you mentioned in the comments that given a spanning tree you want some DFS that outputs the same tree, This shouldn't be a problem.
Suppose you have the required spanning tree and the graph in the form of an adjacency list and have a method edge_exists(u,v) which returns true or false depending on whether the edge is present or not in the given spanning tree.
explore(node):
visited[node] = 1;
for v in node.neighbors:
if edge_exists(node, v) && !visited[v]:
v.p = node
explore(v)
BTW i don't think you need to do a visited count since you have a spanning tree, so edge_exisits will do roughly the same for you
By programmatically outputting the spanning tree, I meant, given a graph output all the spanning trees. I am not sure how to do this.