2017-04-23 1 views
0

재귀없이 파일 트리/디렉토리를 탐색하고 Java를 사용하여 스택을 탐색하는 방법을 참조하십시오.재귀없이 스택을 사용하여 파일 트리를 탐색하십시오.

public void traverse(Path path) 
throws IOException 
{ 
    Stack<Stream<Path>> st = new Stack<>(); 
    st.add(Files.list(path)); 
    for(Iterator<Path> it = st.peek().iterator(); it.hasNext();) 
    { 
     Path temp = it.next(); 
     final BasicFileAttributes fa = Files.readAttributes(temp, BasicFileAttributes.class); 
     if(fa.isDirectory()) 
     { 
      //list all the directory contents 
      st.push(Files.list(temp)); 
     } 
     else if(fa.isRegularFile()) 
     { 
     } 
     else if(fa.isSymbolicLink()) {} //symbolic link 
     else if(fa.isOther()) {} //other 
     else {} 
    } 
} 

고마워요!

답변

0

기본적으로 경로 트리가 있습니다. 다른 나무처럼 탐색하십시오. 이진 트리의 반복적, 스택 지원, 순차적, 순회를위한 좋은 예를 제공합니다 here.

확대 해보세요.

모든 스택은 현재 위치에서 필요한 "메모리"를 제공하므로 현재 분기에서 필요한 것을 찾지 못했을 때 트리를 백업 할 수 있습니다.

관련 문제