트리는 일반적으로 대상 정보의 각 항목들을 계층적으로 연관되도록 구조화시키고자 할 때 사용하는 비선형 자료구조이다.
데이터 요소들의 단순한 나열이 아닌 부모-자식 관계의 계층적 구조로 표현이 된다.
트리는 그래프의 한 종류이며 사이클이 없다.
모든 노드의 차수가 n이하인 트리를 n진 트리라 함
너비 우선 탐색(breadth-first Search)은 낮은 레벨에서 시작해 왼쪽에서 오른쪽 방향으로 검색하고 한 레벨에서의 검색이 끝나면 다음 레벨로 내려감
노드 방문 -> 왼쪽 자식 -> 오른쪽 자식
왼쪽 자식 -> 노드 방문 -> 오른쪽 자식
왼쪽 자식 -> 오른쪽 자식 -> (돌아와) 노드방문
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinarySearchTree {
private TreeNode root;
private class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
public TreeNode(int data) {
this.data = data;
}
}
public void insert(int value) {
root = insert(root, value);
}
public TreeNode insert(TreeNode root, int value) {
if(root == null) {
root = new TreeNode(value);
return root;
}
if(value < root.data) {
root.left =insert(root.left, value);
} else {
root.right = insert(root.right, value);
}
return root;
}
public void inOrder() {
inOrder(root);
}
public void inOrder(TreeNode root) {
if(root == null) {
return;
}
inOrder(root.left);
System.out.println(root.data + " ");
inOrder(root.right);
}
public TreeNode search(int key) {
return search(root,key);
}
public TreeNode search(TreeNode root, int key) {
if(root == null || root.data == key) { // base case
return root;
}
if(key <root.data) {
return search(root.left, key);
} else {
return search(root.right, key);
}
}
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(1);
bst.inOrder(bst.root);
System.out.println();
if(null != bst.search(3)) {
System.out.println("Key found !!!");
} else {
System.out.println("Key not Found !!!");
}
}
}
노드가 왼쪽 자식과 오른쪽 자식을 같는 트리를 이진트리라 함. 이진트리에서 루트로부터 마지막 레벨을 제외한 레벨의 노드가 꽉 채워져 있으면 완전이진트리(complete binary tree)라고 한다.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
private TreeNode root;
private class TreeNode {
private TreeNode left;
private TreeNode right;
private int data; // Generic type
public TreeNode(int data) {
this.data = data;
}
}
// 이진 트리 생성
public void createBinaryTree() {
TreeNode first = new TreeNode(1);
TreeNode second = new TreeNode(2);
TreeNode third = new TreeNode(3);
TreeNode forth = new TreeNode(4);
TreeNode fifth = new TreeNode(5);
root = first; // root ---> first
first.left = second;
first.right = third; // second <--- first ----> third
second.left = forth;
second.right = fifth;
}
// 전위 순회 (재귀 방식)
public void RecursivePreOrder(TreeNode root) {
if(root == null) {
return;
}
System.out.println(root.data + " ");
RecursivePreOrder(root.left);
RecursivePreOrder(root.right);
}
// 전위 순회 (반복문 방식) - stack활용
public void IterativePreOrder() {
if(root == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode temp = stack.pop();
System.out.println(temp.data + "");
if(temp.right != null) {
stack.push(temp.right);
}
if(temp.left != null) {
stack.push(temp.left);
}
}
}
// 중위 순회 (재귀 방식)
public void RecursiveInOrder(TreeNode root) {
if(root == null) {
return;
}
RecursiveInOrder(root.left);
System.out.println(root.data + " ");
RecursiveInOrder(root.right);
}
// 중위 순회 (반복문 방식) - 스택 사용
public void IterativeInOrder(TreeNode root) {
if(root == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode temp = root;
while(!stack.isEmpty() || temp != null) {
if(temp != null) {
stack.push(temp);
temp = temp.left;
} else {
temp = stack.pop();
System.out.println(temp.data + " ");
temp = temp.right;
}
}
}
// 후위 순회 (재귀방식)
public void ReculsivePostOrder(TreeNode root) {
if(root == null) {
return;
}
ReculsivePostOrder(root.left);
ReculsivePostOrder(root.right);
System.out.println(root.data + " ");
}
public void levelOrder(TreeNode root) {
if(root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode temp = queue.poll();
System.out.println(temp.data + " ");
if(temp.left != null) {
queue.offer(temp.left);
}
if(temp.right != null) {
queue.offer(temp.right);
}
}
}
public int findMax() {
return findMax(root);
}
public int findMax(TreeNode root) {
if(root == null) {
return Integer.MIN_VALUE;
}
int result = root.data;
int left = findMax(root.left);
int right = findMax(root.right);
if(left > result) {
result = left;
}
if(right > result) {
result = right;
}
return result;
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.createBinaryTree();
System.out.println(bt.findMax(bt.root));
}
}