I am working on a problem where I need to find the pair in which the difference of value is at most k. Now tree set in java does that for me but I am curious to know how this is working. I am assuming it creates some sort of bucket and map that bucket with some values in a hashmap.
I checked the definition in intelliJ but couldn't find anything which explains how it works.
I found this code in Treemap and now i want to understand how it is actually working
final Entry<K,V> getCeilingEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp < 0) {
if (p.left != null)
p = p.left;
else
return p;
} else if (cmp > 0) {
if (p.right != null) {
p = p.right;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
}
return parent;
}
} else
return p;
}
return null;
}