일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Class
- java
- 최단 경로
- 신경망
- 회로이론
- 강화학습
- 미분 방정식
- cURL
- 이진탐색트리
- 피보나치 수열
- 파이썬
- 자바
- 계단 오르기
- 코드업
- 딥러닝
- 딕셔너리
- 자료형
- auto-encoder
- Asteroid RL
- 2P1L
- 선적분
- 벡터해석
- BST
- 백트래킹
- dictionary
- Python
- 벡터 해석
- 소행성
- 함수
- 델
- Today
- Total
Zeta Oph's Study
[Data Structure (Java)] Tree (3) - Binary Search Tree 본문
https://crane206265.tistory.com/82
[Data Structure (Java)] Tree (2) - Tree Traversal Algorithms
https://crane206265.tistory.com/81 [Data Structure (Java)] Tree (1) - Binary Treehttps://crane206265.tistory.com/80 [Data Structure (Java)] Iteratorhttps://crane206265.tistory.com/79 [Data Structure (Java)] Listhttps://crane206265.tistory.com/78 [Dat
crane206265.tistory.com
BST를 해봅시다. Tree 글은 이걸로 끝일거 같긴 한데, 사실 그렇다고 말하기도 애매한게 앞으로 다룰 내용이 전부 트리 바탕이긴 합니다.
Binary Search Tree (BST)
: binary tree s.t. satisfiy below 2 conditions
(1) elements in the left subtree : less than root of subtree
(2) elements in the right subtree : greater than root of subtree
Tree API
API of Tree<E>
methods | explanation |
boolean isEmpty() | return if tree is empty |
int getSize() | return the # of nodes of the tree |
boolean search(E e) | return if the element E e exists |
void insert(E e) | insert the element E e |
void inorder() | excute and print inorder traversal |
void preorder() | excute and print preorder traversal |
void postorder() | excute and print postorder traversal |
search a element : compare with node a few times (algorithm of binary search)
- if less -> go left
- if greater -> go right
- if equal : search hit
* running time : proportional to the height of the tree
insert : same process of search -> if null : insert
inorder traversal in BST : ascending order of keys
* BST class : reference to a rood Node
before implementation
public class BST<E extends Comparable<E>> implements Tree<E> { ... }
--> generic type E must be comparable -> "bounded type parameter"
* java initially provides java.lang.Comparable interface for String -> includes a single method compareTo()
Comparable Interface
: based on the natural ordering
- standard order for natural/real numbers
- alphabetical order for strings
v.compareTo(w)
: return integer
- v is less than w : return negative
- v equals to w : return 0
- v is greater than w : return positive
BST implementation
tree implementation : linked list + recursion
public class BST<E extends Comparable<E>> implements Tree<E> {
//----- Nested Node Class -----
private static class Node<E> {
private E element;
private Node<E> left;
private Node<E> right;
private int n; //# of nodes in its subtree (include itself)
public Node(E e, int n) {
element = e;
this.n = n; //why use 'this'? : to distinguish instance var n and parameter n
// left & right : initialized to null
}
}
//----- Construct BST Class -----
private Node<E> root;
public BST() {} //construct empty BST
public BST(E[] objects) {
for(i = 0; i < objects.length; i++) {
insert(objects[i]);
}
} //construct BST with array
//----- Methods Implementation -----
public int getSize() {
getSize(root);
}
private int getSize(Node<E> x) {
if(x == null) return 0;
else return x.n;
}
public boolean isEmpty() {
return getSize() == 0;
}
public boolean search(E e) {
Node<E> current = root;
while(current != null) {
int cmp = e.compareTo(current.element);
if(cmp == 0)
return true;
else if(cmp > 0)
current = current.right;
else if(cmp < 0)
current = current.left;
}
return false;
}
public void insert(E e) {
root = insert(root, e);
}
private Node insert(Node<E> x, E e) {
if(x == null)
return new Node(e, 1);
int cmp = e.compareTo(x.element);
if(cmp > 0)
x.right = insert(x.right, e);
else if(cmp < 0)
x.left = insert(x.left, e);
x.n = 1 + getSize(x.left) + getSize(x.right);
return x;
}
public void inorder() {
inorder(root);
}
private void inorder(Node<E> x) {
if(x == null) return;
inorder(x.left);
System.out.print(x.element + " ");
inorder(x.right);
}
public void preorder() {
preorder(root);
}
private void preorder(Node<E> x) {
if(x == null) return;
System.out.print(x.element + " ");
preorder(x.left);
preorder(x.right);
}
public void postorder() {
postorder(root);
}
private void postorder(Node<E> x) {
if(x == null) return;
postorder(x.left);
postorder(x.right);
System.out.print(x.element + " ");
}
}
'프로그래밍 > Java' 카테고리의 다른 글
[Data Structure (Java)] Heapsort (2) | 2024.06.03 |
---|---|
[Data Structure (Java)] Heap (2) | 2024.06.02 |
[Data Structure (Java)] Tree (2) - Tree Traversal Algorithms (1) | 2024.05.13 |
[Data Structure (Java)] Tree (1) - Binary Tree (1) | 2024.05.12 |
[Data Structure (Java)] Iterator (1) | 2024.05.12 |