๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] TreeSet ๊ตฌํ˜„

์ถœ์ฒ˜

ChatGPT

Java 1.8 ์†Œ์Šค ์ฝ”๋“œ


์ž๋ฐ”์˜ TreeSet์€ java.util ํŒจํ‚ค์ง€์— ํฌํ•จ๋œ ์ •๋ ฌ๋œ ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์ด๋‹ค. TreeSet์€ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์˜ ์ผ์ข…์ธ ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ(Red-Black Tree) ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์œผ๋ฉฐ, ์š”์†Œ๋ฅผ ์ €์žฅํ•  ๋•Œ ์ž๋™์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค. ์ด ๋•Œ๋ฌธ์— TreeSet์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ, ํ•ญ์ƒ ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ์š”์†Œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

 

TreeSet์˜ ์ฃผ์š” ํŠน์„ฑ

1. ์ •๋ ฌ๋œ ์ˆœ์„œ

TreeSet์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์š”์†Œ๋“ค์„ ์ •๋ ฌ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค. ์š”์†Œ๋Š” ์‚ฝ์ž…๋  ๋•Œ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ๋ฐฐ์น˜๋˜๋ฉฐ, ๊ฒ€์ƒ‰, ํƒ์ƒ‰, ๋ฒ”์œ„ ๊ฒ€์ƒ‰ ๋“ฑ์—์„œ ํšจ์œจ์ ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

2. ์ค‘๋ณต ํ—ˆ์šฉ ์•ˆํ•จ

TreeSet์€ ์ค‘๋ณต ์š”์†Œ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์•ฝ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ๊ธฐ์กด ์š”์†Œ๊ฐ€ ์œ ์ง€๋˜๊ณ  ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.

3. ์„ฑ๋Šฅ

TreeSet์—์„œ ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฒ€์ƒ‰์˜ ์„ฑ๋Šฅ์€ ๋ชจ๋‘ O(log n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๋Š” HashSet์˜ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋ณต์žก๋„(O(1))๋ณด๋‹ค๋Š” ๋Š๋ฆฌ์ง€๋งŒ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์œ ์ง€๋˜๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

2. TreeSet์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„

1. TreeSet์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

TreeSet์€ ๋‚ด๋ถ€์ ์œผ๋กœ NavigableMap ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ TreeMap์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. TreeMap์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, TreeSet๋„ ๋™์ผํ•œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ณ , ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•œ๋‹ค.

  • ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋Š” ์Šค์Šค๋กœ ๊ท ํ˜•์„ ์œ ์ง€ํ•˜๋Š” ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์ด๋‹ค. ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œ ์ž‘์—…์ด ๋ฐœ์ƒํ•  ๋•Œ, ํŠธ๋ฆฌ์˜ ๊ท ํ˜•์„ ๋งž์ถ”๋Š” ์ถ”๊ฐ€ ์ž‘์—…์ด ๋ฐœ์ƒํ•˜์ง€๋งŒ, ์ด๋กœ ์ธํ•ด O(log n)์˜ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

TreeSet ํด๋ž˜์Šค์˜ ์„ ์–ธ

public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable {
    // ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” TreeMap
    private transient NavigableMap<E,Object> m;

    // TreeMap์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜. TreeSet์€ ์˜ค์ง ํ‚ค๋งŒ ์ค‘์š”ํ•˜๋ฏ€๋กœ ๋ชจ๋“  ๊ฐ’์€ ๋™์ผํ•œ ์ƒ์ˆ˜๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
    private static final Object PRESENT = new Object();

    // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž (์ž์—ฐ ์ •๋ ฌ)
    public TreeSet() {
        this(new TreeMap<E,Object>());
    }

    // Comparator๋ฅผ ๋ฐ›๋Š” ์ƒ์„ฑ์ž
    public TreeSet(Comparator<? super E> comparator) {
        this(new TreeMap<>(comparator));
    }

    // ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์„ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์„ฑ์ž
    TreeSet(NavigableMap<E,Object> m) {
        this.m = m;
    }

    // ๊ธฐํƒ€ ์ฝ”๋“œ ์ƒ๋žต...
}

 

  • ์œ„ ์ฝ”๋“œ์—์„œ TreeSet์€ NavigableMap<E, Object> ํƒ€์ž…์˜ TreeMap์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. PRESENT๋ผ๋Š” ์ƒ์ˆ˜๋Š” TreeMap์—์„œ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด TreeSet์€ ํ‚ค๋งŒ ์ค‘์š”ํ•˜๊ณ  ๊ฐ’์€ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

2. TreeSet์˜ add ๋ฉ”์†Œ๋“œ

TreeSet์˜ add ๋ฉ”์†Œ๋“œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์˜ put ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. TreeMap์—์„œ ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ์ค‘๋ณต๋œ ์š”์†Œ๋Š” ์ž๋™์œผ๋กœ ๊ฑฐ๋ถ€๋œ๋‹ค.

 

add ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„

public boolean add(E e) {
    // TreeMap์˜ put ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€
    return m.put(e, PRESENT) == null;
}

 

  • m.put(e, PRESENT)๋Š” TreeMap์— ์ƒˆ๋กœ์šด ํ‚ค e๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ฐ’์€ PRESENT ์ƒ์ˆ˜๋กœ ์ €์žฅ๋œ๋‹ค.
  • ๋งŒ์•ฝ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ํ‚ค e๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด put ๋ฉ”์†Œ๋“œ๋Š” ๊ธฐ์กด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.
  • == null ์กฐ๊ฑด์€ put ๋ฉ”์†Œ๋“œ๊ฐ€ null์„ ๋ฐ˜ํ™˜ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด, ์ƒˆ๋กœ ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

3. TreeSet์˜ remove ๋ฉ”์†Œ๋“œ

TreeSet์˜ remove ๋ฉ”์†Œ๋“œ๋„ ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์˜ remove ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. TreeMap์—์„œ ํ•ด๋‹น ํ‚ค๋ฅผ ์ฐพ์•„ ์ œ๊ฑฐํ•œ๋‹ค.

 

remove ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„

public boolean remove(Object o) {
    return m.remove(o) == PRESENT;
}

 

  • m.remove(o)๋Š” TreeMap์—์„œ ํ•ด๋‹น ํ‚ค๋ฅผ ์ฐพ์•„์„œ ์ œ๊ฑฐํ•˜๊ณ , ์‚ญ์ œ๋œ ๊ฐ’(์ฆ‰, PRESNET ์ƒ์ˆ˜)์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฐ˜ํ™”๋œ ๊ฐ’์ด PRESENT์™€ ๊ฐ™์œผ๋ฉด, ํ•ด๋‹น ์š”์†Œ๊ฐ€ ์กด์žฌํ–ˆ๊ณ  ์‚ญ์ œ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

4. ์ •๋ ฌ ๋ฐ ํƒ์ƒ‰ ๊ด€๋ จ ๋ฉ”์†Œ๋“œ

TreeSet์€ ์š”์†Œ๋“ค์ด ํ•ญ์ƒ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์ €์žฅ๋œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์˜ ์ •๋ ฌ๋œ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ๋‹ค. ์ด ๊ตฌ์กฐ ๋•๋ถ„์— ๋‹ค์–‘ํ•œ ํƒ์ƒ‰ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

first, last, higher, lower ๋ฉ”์†Œ๋“œ

public E first() {
    return m.firstKey();
}

public E last() {
    return m.lastKey();
}

public E higher(E e) {
    return m.higherKey(e);
}

public E lower(E e) {
    return m.lowerKey(e);
}

 

  • first()์™€ last() : ํŠธ๋ฆฌ์—์„œ ๊ฐ€์žฅ ์ž‘์€(์ฒซ ๋ฒˆ์งธ) ํ‚ค์™€ ๊ฐ€์žฅ ํฐ(๋งˆ์ง€๋ง‰) ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋“ค์€ ํŠธ๋ฆฌ์˜ ์ขŒ์ธก ๋๊ณผ ์šฐ์ธก ๋์— ์œ„์น˜ํ•œ ์š”์†Œ์ด๋‹ค.
  • higher(E e)์™€ lower(E e): ์ฃผ์–ด์ง„ ํ‚ค e๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์™€ ์ž‘์€ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋Š” ํŠธ๋ฆฌ์—์„œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ˆœ์„œ์— ๋งž๋Š” ๋‹ค์Œ ๋˜๋Š” ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.

5. ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” TreeMap์˜ ๋™์ž‘ ์›๋ฆฌ

TreeSet์ด ์‚ฌ์šฉํ•˜๋Š” TreeMap์€ ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค. ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋Š” ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์˜ ์ผ์ •์œผ๋กœ ์‚ฝ์ž…, ์‚ญ์ œ, ํƒ์ƒ‰ ์ž‘์—…์„ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ํ•ญ์ƒ O(log n)์˜ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•œ๋‹ค.

 

TreeMap์˜ ์ผ๋ถ€ ๊ตฌํ˜„

public V put(K key, V value) {
    Entry<K,V> t = root;
    if (t == null) {
        compare(key, key); // type (and possibly null) check

        root = new Entry<>(key, value, null);
        size = 1;
        modCount++;
        return null;
    }

    int cmp;
    Entry<K,V> parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    // comparable key
    else {
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
        Comparable<? super K> k = (Comparable<? super K>) key;
        do {
            parent = t;
            cmp = k.compareTo(t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }

    Entry<K,V> e = new Entry<>(key, value, parent);
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
    fixAfterInsertion(e);
    size++;
    modCount++;
    return null;
}

 

  • ์‚ฝ์ž… ๊ณผ์ • : put ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ์—์„œ ์‹œ์ž‘ํ•ด ๋น„๊ต๋ฅผ ํ†ตํ•ด ์™ผ์ชฝ ๋˜๋Š” ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋กœ ๋‚ด๋ ค๊ฐ„๋‹ค. ์ ์ ˆํ•œ ์œ„์น˜๋ฅผ ์ฐพ์œผ๋ฉด ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.
  • ๊ท ํ˜• ์œ ์ง€ : fixAfterInsertion ๋ฉ”์†Œ๋“œ๋Š” ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ์˜ ๊ทœ์น™์— ๋”ฐ๋ผ ํŠธ๋ฆฌ์˜ ๊ท ํ˜•์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ์ด ๊ทœ์น™์„ ์ ์šฉํ•จ์œผ๋กœ์จ ํŠธ๋ฆฌ๋Š” ํ•ญ์ƒ ๊ท ํ˜•์„ ์œ ์ง€ํ•˜๋ฉฐ, ์‚ฝ์ž…, ์‚ญ์ œ ๋“ฑ์˜ ์ž‘์—…์ด O(log n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

3. TreeSet์˜ ๋™์ž‘ ๋ฐฉ์‹

1. ์š”์†Œ ์ถ”๊ฐ€ (add ๋ฉ”์†Œ๋“œ)

TreeSet์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด, ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์˜ put ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค. ์ด๋•Œ, ์š”์†Œ๊ฐ€ ์ •๋ ฌ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์œ„์น˜์— ์‚ฝ์ž…๋œ๋‹ค.

TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(10);
treeSet.add(20);
treeSet.add(15);  // ์ž๋™์œผ๋กœ ์ •๋ ฌ๋˜์–ด [10, 15, 20] ์ˆœ์„œ๋กœ ์ €์žฅ๋จ

 

2. ์š”์†Œ ์ œ๊ฑฐ (remove ๋ฉ”์†Œ๋“œ)

TreeSet์—์„œ ํŠน์ • ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด, ๋‚ด๋ถ€์ ์œผ๋กœ TreeMap์—์„œ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ด, ํŠธ๋ฆฌ์˜ ๊ท ํ˜•์„ ์œ ์ง€ํ•œ๋‹ค.

treeSet.remove(15);  // ์š”์†Œ 15๊ฐ€ ์‚ญ์ œ๋˜๊ณ , ํŠธ๋ฆฌ๋Š” ๋‹ค์‹œ ์ •๋ ฌ๋œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•จ

 

3. ์ •๋ ฌ ๊ธฐ์ค€

TreeSet์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์š”์†Œ๋“ค์˜ ์ž์—ฐ ์ˆœ์„œ(Natural Ordering)์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ๋‹ค. ์ฆ‰, ์š”์†Œ๋“ค์ด Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์„ ๊ฒฝ์šฐ, ์ด๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Integer๋‚˜ String๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๋Š” ์ด๋ฏธ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.

TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("Banana");
treeSet.add("Apple");
treeSet.add("Cherry");

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ: [Apple, Banana, Cherry]
System.out.println(treeSet);

 

4. ์‚ฌ์šฉ์ž ์ •์˜ ์ •๋ ฌ ๊ธฐ์ค€

TreeSet์€ ์‚ฌ์šฉ์ž ์ •์˜ Comparator๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌ ๊ธฐ์ค€์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. TreeSet์˜ ์ƒ์„ฑ์ž์—์„œ Comparator๋ฅผ ์ „๋‹ฌํ•˜๋ฉฐ, ํ•ด๋‹น ๋น„๊ต ๊ธฐ์ค€์— ๋”ฐ๋ผ ์š”์†Œ๊ฐ€ ์ •๋ ฌ๋œ๋‹ค.

Comparator<String> reverseOrder = Comparator.reverseOrder();
TreeSet<String> treeSet = new TreeSet<>(reverseOrder);
treeSet.add("Banana");
treeSet.add("Apple");
treeSet.add("Cherry");

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ: [Cherry, Banana, Apple] (๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ)
System.out.println(treeSet);

 

4. TreeSet์˜ ๋ฉ”์†Œ๋“œ

  • add(E e) : ์ฃผ์–ด์ง„ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์œ ์ง€ํ•œ๋‹ค.
  • remove(Object o) : ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • first() : ์ง‘ํ•ฉ์—์„œ ๊ฐ€์žฅ ์ž‘์€(์ฒซ ๋ฒˆ์งธ) ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • last() : ์ง‘ํ•ฉ์—์„œ ๊ฐ€์žฅ ํฐ(๋งˆ์ง€๋ง‰)์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • higher(E e) : ์ฃผ์–ด์ง„ ์š”์†Œ๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • lower(E e) : ์ฃผ์–ด์ง„ ์š”์†Œ๋ณด๋‹ค ์ž‘์€ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • subSet(E fromElement, E toElement) : ์ฃผ์–ด์ง„ ๋ฒ”์œ„ ๋‚ด์˜ ์š”์†Œ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(1);
treeSet.add(2);
treeSet.add(3);
treeSet.add(4);
treeSet.add(5);

// ๋ฒ”์œ„ ๊ฒ€์ƒ‰
SortedSet<Integer> subset = treeSet.subSet(2, 5);  // [2, 3, 4]
System.out.println(subset);

// ๊ฐ€์žฅ ์ž‘์€ ์š”์†Œ
System.out.println(treeSet.first());  // 1

// ๊ฐ€์žฅ ํฐ ์š”์†Œ
System.out.println(treeSet.last());   // 5

 

5. TreeSet์˜ ์žฅ๋‹จ์ 

์žฅ์ 

1. ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์œ ์ง€

TreeSet์€ ํ•ญ์ƒ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค.

2. ๋ฒ”์œ„ ๊ฒ€์ƒ‰ ๋ฐ ํƒ์ƒ‰

subSet, headSet, tailSet ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํŠน์ • ๋ฒ”์œ„ ๋‚ด์˜ ์š”์†Œ๋ฅผ ์‰ฝ๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

3. ์ค‘๋ณต ํ—ˆ์šฉ ์•ˆ ํ•จ

์ค‘๋ณต๋œ ์š”์†Œ๋Š” ์ €์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ์ง‘ํ•ฉ์˜ ํŠน์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ 

1. ๋Š๋ฆฐ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ

TreeSet์€ HashSet๊ณผ ๋‹ฌ๋ฆฌ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ ์ž‘์—…์—์„œ O(log n)์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ์ด๋Š” HashSet์˜ O(1)์— ๋น„ํ•ด ๋Š๋ฆฌ๋ฏ€๋กœ, ์ •๋ ฌ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด HashSet์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.

2. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰

TreeSet์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, HashSet๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋” ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค.

 

6. ๊ฒฐ๋ก 

TreeSet์€ ์š”์†Œ๋ฅผ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ž๋ฐ”์˜ ์ค‘์š”ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ์ด๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ํšจ์œจ์ ์œผ๋กœ ์‚ฝ์ž…, ์‚ญ์ œ, ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. TreeSet์€ ๋ฐ์ดํ„ฐ์˜ ์ •๋ ฌ์ด ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๋ฒ”์œ„ ๊ฒ€์ƒ‰๊ณผ ๊ฐ™์€ ์ž‘์—…์ด ํ•„์šฏ๋‚˜ ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์ˆœํžˆ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ๋น ๋ฅธ ์ง‘ํ•ฉ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด HashSet์ด ๋” ๋‚˜์€ ์„ ํƒ์ผ ์ˆ˜ ์žˆ๋‹ค.