Implemented a generic traversal function