트리를 그 모양이 뒤집어 놓은 나무와 같다고 해서 트리라는 이름이 붙었다.

이진트리는 기존의 트리에서 자식노드를 최대 두개만 가질 수 있게 구성한 트리이다.
이진트리에는 정이진트리(full binary tree), 완전이진트리(complete binary tree), 균형이진트리(balanced binary tree) 등이 있다.

완전 이진 트리는 앞서 설명한대로 이진트리의 한 종류이다.
이진트리와 동일하게 자식노드를 최대 두개만 가질 수 있다는 특징이 있지만 다른 이진트리들과는 다르게 왼쪽부터 차례로 입력된다는 큰 특징이 있다.

public class Tree {
public void DFS(Node root){
if(root==null) return;
else {
System.out.println(root.data+" "); //전위순회
DFS(root.leftNode);
DFS(root.rightNode);
}
}
public static void main(String[] args) {
Tree tr = new Tree();
TreeClass tc = new TreeClass();
Node n50 = tc.makeNode(50, null, null);
Node n60 = tc.makeNode(60, null, null);
Node n20 = tc.makeNode(20, n50, n60);
Node n30 = tc.makeNode(30, null, null);
Node n10 = tc.makeNode(10, n20, n30);
/*
10
/ \
20 30
/ \
50 60
*/
tr.DFS(n10);
}
}
class Node {
public int data; //노드의 값
public Node leftNode; //왼쪽 자식노드의 값
public Node rightNode; //오른쪽 자식노드의 값
public Node(int data, Node leftNode, Node rightNode) {
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
}
class TreeClass {
private Node newNode;
//데이터 지정
public Node makeNode(int data, Node leftNode, Node rightNode) {
newNode = new Node(data, leftNode, rightNode);
return newNode;
}
}
한 노드가 두개의 자식노드를 가질 수 있는 이진트리 구조에서 왼쪽부터 차례로 노드를 입력하는 트리를 의미한다. 완전 이진 트리의 구현 방법은 그래프의 인접리스트를 사용하는 방법, 별도의 class를 이용한 방법, 배열을 이용해 구현하는 방법이 있다.