일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 딥러닝
- BST
- 벡터 해석
- 이진탐색트리
- 계단 오르기
- 자바
- 소행성
- 딕셔너리
- Class
- 선적분
- 2P1L
- 벡터해석
- 회로이론
- 최단 경로
- dictionary
- 파이썬
- 미분 방정식
- 신경망
- 백트래킹
- 피보나치 수열
- java
- Python
- 함수
- cURL
- 코드업
- Asteroid RL
- auto-encoder
- 자료형
- 강화학습
- 델
- Today
- Total
Zeta Oph's Study
[Data Structrue (Java)] Tree (4) - Binary Search Tree Map 본문
https://crane206265.tistory.com/89
[Data Structure (Java)] Set
https://crane206265.tistory.com/88 [Data Structure (Java)] Hash (Separate Chaining & Linear Probing)https://crane206265.tistory.com/87 [Data Structure (Java)] Map ADThttps://crane206265.tistory.com/86 [Data Structure (Java)] Heapsorthttps://crane206265.
crane206265.tistory.com
https://crane206265.tistory.com/84
[Data Structure (Java)] Tree (3) - Binary Search Tree
https://crane206265.tistory.com/82 [Data Structure (Java)] Tree (2) - Tree Traversal Algorithmshttps://crane206265.tistory.com/81 [Data Structure (Java)] Tree (1) - Binary Treehttps://crane206265.tistory.com/80 [Data Structure (Java)] Iteratorhttps://
crane206265.tistory.com
끝일 줄 알았지만, 다시 가져온 BST. 마저 이어서, 다만 이번에는 Map ADT로 구현해봅시다.
Recall) Binary Search Tree (BST)
: a binary tree in symmetric order
(no need to be complete)
* symmetric order : every node's key is
: larger than all keys in its left subtree
: smaller than all keys in its right subtree
BST Map API
API of BSTMap<Key, Value>
methods | explanation |
boolean isEmpty() | return if tree is empty |
int getSize() | return the # of nodes of the tree |
Value get(Key key) | return the value associated with the give key (if it does not exist : return null) |
void put(Key key, Value val) | if key exist : replace the associated value to the given value else : add the given entry (key, val) |
void delete(Key key) | delete the entry with the given key |
void deleteMin() | delete the entry with the smallest key |
Key min() | return the smallest key |
int height() | return the height of BST |
void inorder() | excute and print inorder traversal |
void preorder() | excute and print preorder traversal |
void postorder() | excute and print postorder traversal |
* BST : reference to the root Node
Node class
- Key key
- Value val
- Node left : reference to the left subtree - initiallized as null
- Node right : reference to the right subtree - initiallized as null
- int size : # of nodes in the subtree rooted at itself
Hibbard Deletion : Algorithm to deletion in BST
Goal : delete a node T with key K
- case 0 : node T has 0 children
delete T -> setting parent link to null
- case 1 : node T has 1 children
delete T -> replace parent link to its child
- case 2 : node T has 2 children
find the successor of T : X -> delete X (in T's right subtree) -> replace T to X -> set the left link of X to T.left
* successor of a node X : the node with the smallest value that is larger than value of X
* predecessor of a node X : the node with the largest value that is smaller than value of X
BST Map Implementation
BSTMap<Key, Value> implementation : for String keys
import java.lang.Comparable;
public class BSTMap<Key extends Comparable<Key>, Value> {
//----- constructor & instance variable -----
private Node root;
private class Node {
private Key key;
private Value val;
private Node left, right;
private int size;
public Node(Key key, Value val, int size) {
this.key = key;
this.val = val;
this.size = size;
}
}
public BSTMap() {
}
//----- methods -----
public boolean isEmpty() {
return size() == 0;
}
public int size() {
return size(root);
}
private int size(Node x) {
if(x == null) return 0;
return x.size;
}
public Value get(Key key) {
Node x = root;
while(x != null) {
int cmp = key.compareTo(x.key);
if(cmp == 0) return x.val;
else if(cmp > 0) x = x.left;
else if(cmp < 0) x = x.right;
}
return null;
}
public void put(Key key, Value val) {
root = put(root, key, val);
}
private Node put(Node x, Key key, Value val) {
if(x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if(cmp == 0) x.val = val;
else if(cmp > 0) x.left = put(x.left, key, val);
else if(cmp < 0) x.right = put(x.right, key, val);
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node x, Key key) {
if(x == null) return null;
int cmp = key.compareTo(x.key);
if(cmp > 0) x.left = delete(x.left, key);
else if(cmp < 0) x.right = delete(x.right, key);
else {
if(x.right == null) return x.left;
if(x.left == null) return x.right;
Node t = x;
x = min(x.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node x) {
if(x.left == null) return x.right;
x.left = deleteMin(x.left);
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public Key min() {
return min(root).key;
}
private Node min(Node x) {
if(x.left == null) return x;
else return min(x.left);
}
public int height() {
return height(root);
}
private int height(Node x) {
if(x == null) return -1;
else return Math.max(height(x.left), height(x.right)) + 1;
}
public void inorder() {
inorder(root);
}
private void inorder(Node x) {
if(x == null) return;
inorder(x.left);
System.out.print(x.key + " ");
inorder(x.right);
}
public void preorder() {
preorder(root);
}
private void preorder(Node x) {
if(x == null) return;
System.out.print(x.key + " ");
preorder(x.left);
preorder(x.right);
}
public void postorder() {
postorder(root);
}
private void postorder(Node x) {
if(x == null) return;
postorder(x.left);
postorder(x.right);
System.out.print(x.key + " ");
}
}
Running Time Analysis
: depends on the tree shape
--> tree shape : depends on order of insertion
- best case : balanced
- worst case : ascending / descending order -> like linked list
methods get(), put(), delete() : O(h)
* h : height of the BST
- worst case : h = O(n)
- best case : h = O(log n)
'프로그래밍 > Java' 카테고리의 다른 글
[Data Structure (Java)] Tree (6) - 2-3 Tree & LLRB Tree (1) | 2024.06.11 |
---|---|
[Data Structure (Java)] Tree (5) - AVL Tree (2) | 2024.06.09 |
[Data Structure (Java)] Set (1) | 2024.06.05 |
[Data Structure (Java)] Hash (Separate Chaining & Linear Probing) (1) | 2024.06.05 |
[Data Structure (Java)] Map (1) | 2024.06.03 |