Implementation of depth-first order traversal