如何在TreeSet中找到元素的索引?

jtb*_*des 26 java algorithm sortedset treeset data-structures

我正在使用a TreeSet<Integer>,我只是想在集合中找到数字的索引.有没有一种很好的方法来实际使用二叉树的O(log(n))复杂度?

(如果没有,我该怎么做,有谁知道为什么不呢?我很好奇为什么这样的类会被包含在Java中而没有类似搜索功能的东西.)

jtb*_*des 47

我在TreeSet及其接口周围探索了一段时间,我发现获取元素索引的最佳方法是:

set.headSet(element).size()
Run Code Online (Sandbox Code Playgroud)

headSet(element)返回TreeSet小于其参数的子元素,因此该集合的大小将是所讨论元素的索引.确实是一个奇怪的解

  • 注意:如果元素不在集合中,则不起作用.在这种情况下,它返回0,这不是真的(-1会更合适). (5认同)

Jas*_*key 14

由于@Yrlec指出set.headSet(element).size将返回0,尽管集合中没有此元素.所以我们最好检查一下:

 return set.contains(element)? set.headSet(element).size(): -1;
Run Code Online (Sandbox Code Playgroud)

这是一个显示问题的测试用例:

public static void main(String args[]){
    TreeSet<Integer> set = new TreeSet<>();
    set.add(4);
    set.add(2);
    set.add(3);
    set.add(1);

    System.out.println(set.headSet(1).size());//0
    System.out.println(set.headSet(2).size());//1
    System.out.println(set.headSet(3).size());//2
    System.out.println(set.headSet(4).size());//3
    System.out.println(set.headSet(-1).size());//0!!Caution,returns 0 though it does not exist!

}
Run Code Online (Sandbox Code Playgroud)


Vit*_*ich 12

我有同样的问题.所以我拿了java.util.TreeMap的源代码并编写了IndexedTreeMap.它实现了我自己的IndexedNavigableMap:

public interface IndexedNavigableMap<K, V> extends NavigableMap<K, V> {
   K exactKey(int index);
   Entry<K, V> exactEntry(int index);
   int keyIndex(K k);
}
Run Code Online (Sandbox Code Playgroud)

该实现基于更改红黑树中的节点权重.权重是给定节点下的子节点数加一个自身.例如,当树向左旋转时:

    private void rotateLeft(Entry<K, V> p) {
    if (p != null) {
        Entry<K, V> r = p.right;

        int delta = getWeight(r.left) - getWeight(p.right);
        p.right = r.left;
        p.updateWeight(delta);

        if (r.left != null) {
            r.left.parent = p;
        }

        r.parent = p.parent;


        if (p.parent == null) {
            root = r;
        } else if (p.parent.left == p) {
            delta = getWeight(r) - getWeight(p.parent.left);
            p.parent.left = r;
            p.parent.updateWeight(delta);
        } else {
            delta = getWeight(r) - getWeight(p.parent.right);
            p.parent.right = r;
            p.parent.updateWeight(delta);
        }

        delta = getWeight(p) - getWeight(r.left);
        r.left = p;
        r.updateWeight(delta);

        p.parent = r;
    }
  }
Run Code Online (Sandbox Code Playgroud)

updateWeight只是更新权重到根:

   void updateWeight(int delta) {
        weight += delta;
        Entry<K, V> p = parent;
        while (p != null) {
            p.weight += delta;
            p = p.parent;
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我们需要通过索引找到元素时,这是使用权重的实现:

public K exactKey(int index) {
    if (index < 0 || index > size() - 1) {
        throw new ArrayIndexOutOfBoundsException();
    }
    return getExactKey(root, index);
}

private K getExactKey(Entry<K, V> e, int index) {
    if (e.left == null && index == 0) {
        return e.key;
    }
    if (e.left == null && e.right == null) {
        return e.key;
    }
    if (e.left != null && e.left.weight > index) {
        return getExactKey(e.left, index);
    }
    if (e.left != null && e.left.weight == index) {
        return e.key;
    }
    return getExactKey(e.right, index - (e.left == null ? 0 : e.left.weight) - 1);
}
Run Code Online (Sandbox Code Playgroud)

还可以非常方便地找到键的索引:

    public int keyIndex(K key) {
    if (key == null) {
        throw new NullPointerException();
    }
    Entry<K, V> e = getEntry(key);
    if (e == null) {
        throw new NullPointerException();
    }
    if (e == root) {
        return getWeight(e) - getWeight(e.right) - 1;//index to return
    }
    int index = 0;
    int cmp;
    if (e.left != null) {
        index += getWeight(e.left);
    }
    Entry<K, V> p = e.parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        while (p != null) {
            cmp = cpr.compare(key, p.key);
            if (cmp > 0) {
                index += getWeight(p.left) + 1;
            }
            p = p.parent;
        }
    } else {
        Comparable<? super K> k = (Comparable<? super K>) key;
        while (p != null) {
            if (k.compareTo(p.key) > 0) {
                index += getWeight(p.left) + 1;
            }
            p = p.parent;
        }
    }
    return index;
}
Run Code Online (Sandbox Code Playgroud)

我将很快实现IndexedTreeSet,同时您可以使用IndexedTreeMap中的键集.

更新:现在实现了IndexedTreeSet.

您可以在http://code.google.com/p/indexed-tree-map/找到此工作的结果