이진 탐색 트리(BST: binary search tree)는 다음과 같은 속성이 있는 이진 트리 자료 구조이다.

효율적인 탐색
BST는 평균적으로 O(log n)의 시간 복잡도로 요소를 탐색할 수 있다. 이는 배열이나 연결 리스트와 비교했을 때 매우 효율적인 성능을 제공한다.
정렬된 데이터 유지
BST는 삽입 연산 시 자동으로 정렬된 순서를 유지한다. 이를 통해 추가적인 정렬 작업 없이도 데이터를 정렬된 상태로 유지할 수 있다. 이는 중복을 허용하지 않는 경우에 특히 유용하다.
동적 데이터 관리
BST는 동적으로 데이터를 삽입하고 삭제할 수 있다. 배열과는 달리 크기를 미리 지정할 필요가 없으며, 필요에 따라 노드를 추가하거나 제거할 수 있다. 이를 통해 메모리 효율성을 높일 수 있다.
범위 검색 (Range Query)
BST는 특정 범위 내의 값을 효율적으로 검색할 수 있다. 예를 들어, 특정 값 이상이면서 특정 값 이하인 요소들을 찾는 작업이 용이하다. 이는 데이터베이스 인덱싱과 같은 응용 분야에서 매우 유용하다.
순회(traversal) 용이
BST는 다양한 순회 방법(전위 순회, 중위 순회, 후위 순회)을 제공하여 데이터를 다양한 방식으로 처리할 수 있다. 특히, 중위 순회를 사용하면 트리의 요소들을 오름차순으로 방문할 수 있다.
class BinarySearchTree {
//첫 시작은 root로 해야하기 때문이다.
Node root;
//맨처음 root는 null로 넣고 시작
BinarySearchTree() {
root = null;
}
// 삽입과정
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
// 맨 처음 root가 비어있다면? 어쩔 수 없다..
if (root == null) {
root = new Node(key);
return root;
}
// 왼쪽 오른쪽 둘중 하나에 넣자.
if (key < root.key) {
root.left = insertRec(root.left, key);
} else if (key > root.key) {
root.right = insertRec(root.right, key);
}
return root;
}
// 탐색
boolean search(int key) {
return searchRec(root, key) != null;
}
Node searchRec(Node root, int key) {
if (root == null || root.key == key) {
return root;
}
if (root.key > key) {
return searchRec(root.left, key);
}
return searchRec(root.right, key);
}
// 삭제
void delete(int key) {
root = deleteRec(root, key);
}
Node deleteRec(Node root, int key) {
if (root == null) {
return root;
}
if (key < root.key) {
root.left = deleteRec(root.left, key);
} else if (key > root.key) {
root.right = deleteRec(root.right, key);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.key = minValue(root.right);
root.right = deleteRec(root.right, root.key);
}
return root;
}
int minValue(Node root) {
int minValue = root.key;
while (root.left != null) {
minValue = root.left.key;
root = root.left;
}
return minValue;
}
}
// Node는 key와 왼쪽 오른쪽만 있으면 된다.
class Node {
int key;
Node left, right;
public Node(int item) {
this.key = item;
this.left = null;
this.right = null;
}
}
대표적으로 위와 같이 2가지가 존재한다. 하지만 일반적으로 레드 블랙 트리를 사용한다 .
특히 TreeSet은 블랙이진트리로 구현된 자료구조이다.
필자도 어려워서.. 잘은 모르겠다.