working iterative bfs and dfs