이진트리순회(DFS : Depth-First Search)

이진트리순회(DFS : Depth-First Search)

import java.util.*;
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{
			DFS(root.lt);
			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); 
    } 
} 


손으로 그려보면서 해보니 훨씬 이해가 쉬웠다 !

좋은 웹페이지 즐겨찾기