|
| 1 | +using static System.Console; |
| 2 | + |
| 3 | +var minHeap = new MinHeap(10); |
| 4 | + |
| 5 | +minHeap.Push(20); |
| 6 | +minHeap.Push(30); |
| 7 | +minHeap.Push(300); |
| 8 | +minHeap.Push(1); |
| 9 | +minHeap.Push(40); |
| 10 | +minHeap.Push(5); |
| 11 | +minHeap.Push(7); |
| 12 | +minHeap.Push(9); |
| 13 | +minHeap.Push(9); |
| 14 | +minHeap.Push(19); |
| 15 | + |
| 16 | +WriteLine(minHeap.Pop()); |
| 17 | +WriteLine(minHeap.Pop()); |
| 18 | +WriteLine(minHeap.Pop()); |
| 19 | +WriteLine(minHeap.Pop()); |
| 20 | +WriteLine(minHeap.Pop()); |
| 21 | +WriteLine(minHeap.Pop()); |
| 22 | +WriteLine(minHeap.Pop()); |
| 23 | +WriteLine(minHeap.Pop()); |
| 24 | +WriteLine(minHeap.Pop()); |
| 25 | +WriteLine(minHeap.Pop()); |
| 26 | + |
| 27 | + |
| 28 | +public class MinHeap |
| 29 | +{ |
| 30 | + private readonly int[] _heap; |
| 31 | + private readonly int _maxSize; |
| 32 | + private int _size; |
| 33 | + |
| 34 | + public MinHeap(int maxSize) |
| 35 | + { |
| 36 | + _maxSize = maxSize; |
| 37 | + _size = 0; |
| 38 | + _heap = new int[_maxSize]; |
| 39 | + } |
| 40 | + |
| 41 | + private static int _parent(int index) |
| 42 | + => (index - 1) / 2; |
| 43 | + |
| 44 | + private static int _leftChild(int index) |
| 45 | + => (index * 2) + 1; |
| 46 | + |
| 47 | + private static int _rightChild(int index) |
| 48 | + => (index * 2) + 2; |
| 49 | + |
| 50 | + private bool _isSmaller(int firstIndex, int secondIndex) |
| 51 | + => _heap[firstIndex] < _heap[secondIndex]; |
| 52 | + |
| 53 | + private void _swap(int firstIndex, int secondIndex) |
| 54 | + => (_heap[firstIndex], _heap[secondIndex]) = (_heap[secondIndex], _heap[firstIndex]); |
| 55 | + |
| 56 | + public void Push(int element) |
| 57 | + { |
| 58 | + if(_size >= _maxSize) |
| 59 | + { |
| 60 | + throw new InvalidOperationException("Heap is full"); |
| 61 | + } |
| 62 | + |
| 63 | + _heap[_size] = element; |
| 64 | + |
| 65 | + var current = _size; |
| 66 | + var parent = _parent(current); |
| 67 | + |
| 68 | + while(_isSmaller(current, parent)) |
| 69 | + { |
| 70 | + _swap(current, parent); |
| 71 | + current = parent; |
| 72 | + parent = _parent(current); |
| 73 | + } |
| 74 | + _size++; |
| 75 | + } |
| 76 | + |
| 77 | + public int Peek() |
| 78 | + => _size > 0 ? _heap[0] : throw new InvalidOperationException("Heap is empty"); |
| 79 | + |
| 80 | + public int Pop() |
| 81 | + { |
| 82 | + if(_size <= 0) |
| 83 | + { |
| 84 | + throw new InvalidOperationException("Heap is empty"); |
| 85 | + } |
| 86 | + |
| 87 | + _size--; |
| 88 | + var result = _heap[0]; |
| 89 | + _heap[0] = _heap[_size]; |
| 90 | + |
| 91 | + |
| 92 | + var current = 0; |
| 93 | + var leftChild = _leftChild(current); |
| 94 | + var rightChild = _rightChild(current); |
| 95 | + |
| 96 | + while(leftChild < _size) |
| 97 | + { |
| 98 | + var smallestChild = rightChild < _size && _isSmaller(rightChild, leftChild) ? rightChild : leftChild; |
| 99 | + if(_isSmaller(current, smallestChild)) |
| 100 | + { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + _swap(current, smallestChild); |
| 105 | + current = smallestChild; |
| 106 | + leftChild = _leftChild(current); |
| 107 | + rightChild = _rightChild(current); |
| 108 | + } |
| 109 | + |
| 110 | + return result; |
| 111 | + } |
| 112 | +} |
0 commit comments