TreeSet<E> ํด๋์ค
- ๋ฐ์ดํฐ๊ฐ ์ ๋ ฌ๋ ์ํ๋ก ์ ์ฅ๋๋ ์ด์ง ๊ฒ์ ํธ๋ฆฌ(binary search tree)์ ํํ๋ก ์์๋ฅผ ์ ์ฅ
์ด์ง ๊ฒ์ ํธ๋ฆฌ
- ํธ๋ฆฌ๋ ์๋ฃ์ฌ์ด์ ๊ณ์ฆ ๊ตฌ์กฐ๋ฅผ ๋ํ๋ด๋ ์๋ฃ ๊ตฌ์กฐ ์ ๋๋ค.
- ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ๋ ๋ฑ์ ๊ธฐ๋ณธ ๋์ ์๊ฐ์ด ๋งค์ฐ ๋น ๋ฆ ๋๋ค.
- ์์๋ ธ๋๋ 0,1,2๊ฐ๋ง ๊ฐ์ง ์ ์์ต๋๋ค.
๊ฐ๋จํ ๊ฒ์ ํธ๋ฆฌ ์์
23, 10, 48, 15, 7, 22, 56
ํธ๋ฆฌ ์์ฑ
๋ฃจํธ(root)-23 ๋ฅผ ๊ธฐ์ค์ผ๋ก root ๋ณด๋ค ์์ผ๋ฉด ์ผ์ชฝ, ํฌ๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก ๋ฐฐ์น๋ฉ๋๋ค.
10์ 23๋ณด๋ค ์์ผ๋ฏ๋ก ์ผ์ชฝ, 48์ 10๋ณด๋ค ํฌ๋ฏ๋ก ์ค๋ฅธ์ชฝ
15๋ 23๋ณด๋ค ์์ผ๋ฏ๋ก ์ผ์ชฝ, 10๋ณด๋ค ํฌ๋ฏ๋ก ์ค๋ฅธ์ชฝ
7์ 23๋ณด๋ค ์์ผ๋ฏ๋ก ์ผ์ชฝ, 10๋ณด๋ค ์์ผ๋ฏ๋ก ์ผ์ชฝ
48์ 23๋ณด๋ค ํฌ๋ฏ๋ก ์ค๋ฅธ์ชฝ
- ์์ ๊ฐ์ด ํธ๋ฆฌ๊ฐ ์๋์ผ๋ก ์ ๋ ฌ๋๋ฉด์ ์์ฑ์ด ๋๊ณ , ์ด๋ฅผ ์ค์(Inorder) ์ํ๋ฅผ ํ๋ฉด ์ค๋ฆ์ฐจ์๋ ๊ฒฐ๊ณผ๊ฐ์ ์ป์ ์ ์์ต๋๋ค. (Left - Root - Right)
7 -> 10 -> 15 -> 22 -> 48 -> 56
๐ถ๊ธฐ๋ณธ ์์ 1
import java.util.Iterator;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();
ts.add(23);
ts.add(10);
ts.add(48);
ts.add(15);
ts.add(7);
ts.add(22);
ts.add(56);
for(Integer e : ts) System.out.print(e + " ");
ts.remove(48); // ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์์๋ฅผ ์ ๊ฑฐ
System.out.println();
// iterator() ๋ก ์ํ : ๋ฐ๋ณต์ (ํฅ์๋ for๋ฌธ์ด ๋์ค๊ธฐ ์ด์ ์ ํ์ค์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ์
Iterator<Integer> iterator = ts.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
// ์์์ ๊ฐฏ์
System.out.println("ํฌ๊ธฐ : " + ts.size());
}
}
๐ถ๊ธฐ๋ณธ ์์ 2
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Integer[] score = {78, 45, 60, 54, 92};
TreeSet<Integer> ts = new TreeSet<>(Arrays.asList(score));
System.out.println("60์ ๋ฏธ๋ง : " + ts.headSet(60));
System.out.println("60์ ์ด์ : " + ts.tailSet(60));
// ์ฃผ์ด์ง ์ ์ ๋ฐ๋ก ์๋์ ์ ์ ์ถ๋ ฅ
System.out.println("60์ ๋ฐ๋ก ์๋ ์ ์ : " + ts.lower(60));
// ์ฃผ์ด์ง ์ ์ ๋ฐ๋ก ์์ ์ ์ ์ถ๋ ฅ
System.out.println("60์ ๋ฐ๋ก ์ ์ ์ : " + ts.higher(60));
}
}
TreeMap
- Map ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค ์ค ํค ๊ฐ์ผ๋ก ์๋ฃ๋ฅผ ์ ๋ ฌํ๋ ค๋ฉด TreeMap์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
- TreeMap๋ TreeSet๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด์ง ๊ฒ์ ํธ๋ฆฌ(๊ฐ์ด ์ถ๊ฐ ๋ ๋ ์๋์ ๋ ฌ)๋ก ๊ตฌํ ๋์ด ์์ต๋๋ค.
- TreeMap์ ํค ๊ฐ์ผ๋ก ์ ๋ ฌํ๋ฏ๋ก ํค ๊ฐ์ ํด๋นํ๋ Comparable๊ณผ Comparator๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
- TreeSet๊ณผ์ ์ฐจ์ด์ ์ ํค์ ๊ฐ์ด ์ ์ฅ๋ Map.Entry๋ฅผ ์ ์ฅํฉ๋๋ค.
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> score = new TreeMap<>();
score.put(87, "๋ํฌ๋");
score.put(88, "๊ณ ์ ๋ฆผ");
score.put(75, "๋ฐฑ์ด์ง");
score.put(65, "๊ตฌ์๊ฒฝ");
score.put(98, "์ฐ์์ฐ");
Map.Entry<Integer, String> entry = null;
entry = score.firstEntry();
System.out.println("๊ฐ์ฅ ๋ฎ์ ์ ์ : " + entry.getKey() + ", " + entry.getValue());
entry = score.lastEntry();
System.out.println("๊ฐ์ฅ ๋์ ์ ์ : " + entry.getKey() + ", " + entry.getValue());
entry = score.lowerEntry(95);
System.out.println("95์ ์๋ ์ ์ : " + entry.getKey() + ", " + entry.getValue());
entry = score.higherEntry(95);
System.out.println("95์ ์ ์ ์ : " + entry.getKey() + ", " + entry.getValue());
}
}