class Node{
int data;
Node lt, rt; // 인스턴스 변수-노드라는 클래스의 객체 주소를 저장
public Node(int val){
data=val;
lt=rt=null;
}
}
public class Main{
Node root;
public void DFS(Node root){
if(root==null) return; // 말단 노드라면
else{
// System.out.print(root.data+" "); // 전위 순회
DFS(root.lt);
// System.out.print(root.data+" "); // 중위 순회
DFS(root.rt);
// System.out.print(root.data+" "); // 후위 순회
}
}
public static void main(String[] args) {
Main tree=new Main();
tree.root=new Node(1);
tree.root.lt=new Node(2);
tree.root.rt=new Node(3);
tree.root.lt.lt=new Node(4);
tree.root.lt.rt=new Node(5);
tree.root.rt.lt=new Node(6);
tree.root.rt.rt=new Node(7);
tree.DFS(tree.root);
}
}
'ALGORITHM' 카테고리의 다른 글
[JAVA] 백준 15651번- N과 M(3) (0) | 2022.08.21 |
---|---|
[JAVA] 백준 15650번- N과 M(2) (0) | 2022.08.20 |
[JAVA] 백준 15649번- N과 M(1) (0) | 2022.08.15 |
[JAVA] 알고리즘 : DFS- 피보나치 재귀(메모이제이션) (0) | 2022.08.10 |
[JAVA] 백준 1193번- 분수찾기 (0) | 2022.08.10 |