Implement BFS and DFS.