in csharp/src/Containers/AvlTree.cs [391:479]
private Int32 Add(Int32 node, TKey key, TValue value)
{
while (true)
{
if (key.CompareTo(_data[node].Key) < 0)
{
if (_data[node]._left == _NO_ELEMENT)
{
if (_count == _capacity)
{
_capacity <<= 1;
Array.Resize<Entry>(ref _data, _capacity);
for (int i = _count; i < _capacity; ++i)
{
_data[i]._count = 1;
_data[i]._height = 1;
_data[i]._left = _NO_ELEMENT;
_data[i]._parent = _NO_ELEMENT;
_data[i]._right = i + 1;
}
_data[_capacity - 1]._right = _NO_ELEMENT;
_free = _count;
}
int created = _free;
_data[node]._left = created;
_data[created].Key = key;
_data[created].Value = value;
_data[created]._parent = node;
_data[created]._count = 1;
_free = _data[created]._right;
_data[created]._right = _data[created]._left = _NO_ELEMENT;
return created;
}
else
{
node = _data[node]._left;
continue;
}
}
else
{
if (_data[node]._right == _NO_ELEMENT)
{
if (_count == _capacity)
{
_capacity <<= 1;
Array.Resize<Entry>(ref _data, _capacity);
for (int i = _count; i < _capacity; ++i)
{
_data[i]._count = 1;
_data[i]._height = 1;
_data[i]._left = _NO_ELEMENT;
_data[i]._parent = _NO_ELEMENT;
_data[i]._right = i + 1;
}
_data[_capacity - 1]._right = _NO_ELEMENT;
_free = _count;
}
int created = _free;
_data[node]._right = created;
_data[created].Key = key;
_data[created].Value = value;
_data[created]._parent = node;
_data[created]._count = 1;
_free = _data[created]._right;
_data[created]._right = _data[created]._left = _NO_ELEMENT;
return created;
}
else
{
node = _data[node]._right;
continue;
}
}
}
}