/// <inheritdoc cref="IHeap{TKey,TValue}.Remove"/> public override TValue Remove(PairingHeapNode <TKey, TValue> node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } // Remove the node from its list of siblings. Merge all its children to form // a new tree and merge that tree with the root. var item = node.Value; if (node == this.Root) { // Simplified case when we remove the root this.Root = this.MergePairwisely(node.Child); } else { // The node is somewhere in the middle of the tree, so before we handle // the child of the node, we also need to fix the list of siblings. node.RemoveFromListOfSiblings(); // The only part left is the child of the node. As stated previously, // we will simply merge it with the entire heap. this.Root = this.Merge(this.Root, this.MergePairwisely(node.Child)); } --this.count; return(item); }
/// <inheritdoc cref="IHeap{TKey,TValue}.Update"/> public override void Update(PairingHeapNode <TKey, TValue> node, TKey key) { if (node == null) { throw new ArgumentNullException(nameof(node)); } var relation = this.Comparer.Compare(key, node.Key); node.Key = key; // If the new value is considered equal to the previous value, there is no need // to fix the heap property, because it is already preserved. if (relation == 0) { return; } if (relation < 0) { // In case the root gets a value that should be extracted from the heap even // earlier, there is also no need to fix anything. if (node == this.Root) { return; } node.RemoveFromListOfSiblings(); this.Root = this.Merge(this.Root, node); return; } // In case the node was updated with a greater value, it is in the right spot. // However, the heap property might be violated for all its children. For that // reason we will merge them pairwisely to form a single tree and merge this // tree with our heap. var child = node.Child; if (child == null) { return; } node.Child = null; child.Previous = null; // TODO: this line might be not needed var tree = this.MergePairwisely(child); this.Root = this.Merge(this.Root, tree); }