private void ShiftUp(int k) { while (k > 1 && _items[k].CompareTo(_items[k / 2]) < 0) { HeapHelper.Swap <T>(_items, k, k / 2); k = k / 2; } }
public T ExtractTop() { if (_count < 1) { return(null); } var top = _items[1]; HeapHelper.Swap <T>(_items, 1, _count); _count--; ShiftDown(1); return(top); }
private void ShiftDown(int k) { while (2 * k < _count) { var toBeSwapIndex = 2 * k; if (2 * k + 1 <= _count && _items[2 * k + 1].CompareTo(_items[2 * k]) < 0) { toBeSwapIndex = 2 * k + 1; } if (_items[k].CompareTo(_items[toBeSwapIndex]) < 0) { break; } HeapHelper.Swap <T>(_items, k, toBeSwapIndex); k = toBeSwapIndex; } }