public T Delete() { T result = _array[1].Value; // It keeps root data to return. ArrayHeapElement <T> lastElement = _array[_count]; // It keeps terminal node. int parentIndex = 1; // It keeps index of root node. while (true) { int childIndex = GetHighPriorityChildIndex(parentIndex); // it finds index of more high priority child node. if (childIndex == 0) { break; } if (lastElement.Priority <= _array[childIndex].Priority) { break; } // It compares priority between more high priority child and last child. // If The priority of last child is higher than more high priority, return. _array[parentIndex] = _array[childIndex]; // More high priority child node switches to its parent node. parentIndex = childIndex; // Same meaning as upper line. } _array[parentIndex] = lastElement; // It saves last node to parent node. _count -= 1; return(result); }
public void InsertValue(T value, int priority) { int index = _count + 1; while (index != 1) // If It isn't root node, loop these. { int parentIndex = GetParentNodeIndex(index); if (priority >= _array[parentIndex].Priority) { break; // if An inserted priority is lower than their parent node, return. } _array[index] = _array[parentIndex]; // A parent node gets lower to current Index. index = GetParentNodeIndex(index); // The Index of new node gets upper to their parent node index. } _array[index] = new ArrayHeapElement <T>(value, priority); // Save. _count += 1; }