I am a bit confused on the follwoing method of java.util.TreeMap:
static <K,V> TreeMap.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;
}
}
This method is used in TreeMap's containsValue method. And it is told to retrieve successor of first Entry and successor of previously retrieved successor and so on. So this above method retrieves Entries of the whole TreeMap. But I don't understand very well how it works, how it seeks successor?
Thank you!