看起来 Java 的 TreeMap“借用”了 JabberNet's Tree 的部分实现,JabberNet's Tree 是用 C# 编写的——here the full C# source code。
Java 的TreeMap 的作者之一很可能包含了反映这一事实的评论(因此评论中的“CLR”确实意味着“公共语言运行时”)。
这里是sn-p from Java's TreeMap:
/** From CLR */
private void fixAfterDeletion(Entry<K,V> x) {
while (x != root && colorOf(x) == BLACK) {
if (x == leftOf(parentOf(x))) {
Entry<K,V> sib = rightOf(parentOf(x));
if (colorOf(sib) == RED) {
setColor(sib, BLACK);
setColor(parentOf(x), RED);
rotateLeft(parentOf(x));
sib = rightOf(parentOf(x));
}
if (colorOf(leftOf(sib)) == BLACK &&
colorOf(rightOf(sib)) == BLACK) {
...
这里对应的sn-pfrom the JabberNet C# code:
private void fixAfterDeletion(Node x)
{
while ((x != root) && (colorOf(x) == NodeColor.BLACK))
{
if (x == leftOf(parentOf(x)))
{
Node sib = rightOf(parentOf(x));
if (colorOf(sib) == NodeColor.RED)
{
setColor(sib, NodeColor.BLACK);
setColor(parentOf(x), NodeColor.RED);
rotateLeft(parentOf(x));
sib = rightOf(parentOf(x));
}
if ((colorOf(leftOf(sib)) == NodeColor.BLACK) &&
(colorOf(rightOf(sib)) == NodeColor.BLACK))
...
如您所见,代码几乎相同——除了缩进、节点类名和语法不同。
对于标记为/** From CLR */ 的其他方法也是如此。
但是,Java 代码不是似乎是完全从 C# 代码自动生成的,参见。 this comment in the Java code:
/**
* Balancing operations.
*
* Implementations of rebalancings during insertion and deletion are
* slightly different than the CLR version. Rather than using dummy
* nilnodes, we use a set of accessors that deal properly with null. They
* are used to avoid messiness surrounding nullness checks in the main
* algorithms.
*/