TreeSet Iteration, of course, is O (n), as you would expect from any reasonable tree walking algorithm.
I think that by providing links back from the child node to the parent node I could improve performance.
TreeMap (which TreeSet based TreeSet ) already has such parent links. This is a method that comes down to:
private Entry<K,V> successor(Entry<K,V> t) { if (t == null) return null; else if (t.right != null) { Entry<K,V> p = t.right; while (p.left != null) p = p.left; return p; } else { Entry<K,V> p = t.parent; Entry<K,V> ch = t; while (p != null && ch == p.right) { ch = p; p = p.parent; } return p; } }
Michael borgwardt
source share