public void Push(HeapNode node)
        {
            if (++_tail >= _arr.Length)
            {
                throw new IndexOutOfRangeException($"Cannot insert value. Array capacity: {_arr.Length} Index: {_tail}");
            }

            _arr[_tail] = node;

            int currentIndex = _tail;
            int parentIndex  = GetParetnIndex(_tail);

            while (parentIndex >= 0 && _arr[currentIndex].Key < _arr[parentIndex].Key)
            {
                _arr[currentIndex] = _arr[parentIndex];
                _arr[parentIndex]  = node;

                currentIndex = parentIndex;
                parentIndex  = GetParetnIndex(currentIndex);
            }
        }
        public HeapNode Pop()
        {
            if (_tail == -1)
            {
                throw new IndexOutOfRangeException("Heap is empty");
            }

            HeapNode top = _arr[0];

            _arr[0]     = _arr[_tail];
            _arr[_tail] = null;
            _tail--;

            int currentIndex = 0;

            while (LeftLeafExists(currentIndex))
            {
                int biggestIndex = GetLeftLeafIndex(currentIndex);

                if (RightLeafExists(currentIndex) && _arr[GetRightLeafIndex(currentIndex)].Key > _arr[biggestIndex].Key)
                {
                    biggestIndex = GetRightLeafIndex(currentIndex);
                }

                if (_arr[currentIndex].Key > _arr[biggestIndex].Key)
                {
                    break;
                }

                // swap
                var tmp = _arr[currentIndex];
                _arr[currentIndex] = _arr[biggestIndex];
                _arr[biggestIndex] = tmp;

                // go further
                currentIndex = biggestIndex;
            }

            return(top);
        }
        public void Push(HeapNode node)
        {
            if (++_tail >= _arr.Length)
            {
                throw new IndexOutOfRangeException($"Cannot insert value. Array capacity: {_arr.Length} Index: {_tail}");
            }

            _arr[_tail] = node;

            // walk up the tree and swap current value with parent if current value is greater than parent
            int currentIndex = _tail;
            int parentIndex  = GetParent(_tail);

            while (parentIndex >= 0 && _arr[currentIndex].Key > _arr[parentIndex].Key)
            {
                _arr[currentIndex] = _arr[parentIndex];
                _arr[parentIndex]  = node; // do not need to use tmp

                currentIndex = parentIndex;
                parentIndex  = GetParent(currentIndex);
            }
        }
 public MinHeap(int size)
 {
     _arr = new HeapNode[size];
 }