288
правок
Изменения
Нет описания правки
|definition = Left-leaning Red-Black Trees .
==Вращения==
<code>
'''Node '''rotateRight(h : '''Node '''):
x = h.left
h.left= x.right
x.color = h.color
h.color = RED
'''return ''' x
</code>
<code>
'''void''' insert( '''key''' : Key, '''value''' : Value ):
root = insert(root, key, value); root.color = BLACK;
</code>
<code>
'''Node''' insert( h : '''Node''', key : '''Key''', value : '''Value'''): '''if (''' h == null) '''return'''' ''new ''' Node(key, value) '''; if (''' isRed(h.left) '''&& ''' isRed(h.right)) colorFlip(h); '''int ''' cmp = key.compareTo(h.key); '''if (''' cmp == 0) h.val = value; '''else ''' '''if (''' cmp < 0) h.left = insert(h.left, key, value);
'''else'''
h.right = insert(h.right, key, value); '''if (''' isRed(h.right) '''&& ''' !isRed(h.left)) h = rotateLeft(h); '''if (''' isRed(h.left) '''&& ''' isRed(h.left.left)) h = rotateRight(h);
'''return''' ''h''
</code>
<code>
while (x != null)
'''int ''' cmp = key.compareTo(x.key); if (cmp == 0) '''return ''' x.val;
else
if (cmp < 0) x = x.left;
else
if (cmp > 0) x = x.right; '''return ''' null;
</code>
==Удаление==
<code>
void deleteMin(): root = deleteMin(root); root.color = BLACK;</code><code> '''Node''' deleteMin( h : '''Node'''): '''if''' h.left == null '''return''' null '''if''' !isRed(h.left) '''&&''' !isRed(h.left.left) h = moveRedLeft(h) h.left = deleteMin(h.left) '''return''' fixUp(h)
</code>
<code>
</code>