public void Remove(TreeInfo treeInfo) { var name = treeInfo.name; Tree tree = Trees.Find(t => t.Name == name); Trees.Remove(tree); }
/// <summary> /// Decreases the priority of the given node. O(log n) operation /// </summary> /// <param name="node">The node to decrease the value of</param> /// <param name="priority">The new priority of the node</param> public void DecreasePriority(HeapNode <TPriority, TItem> node, TPriority priority) { // If node priority is already less than given priority if (node.Priority.CompareTo(priority) < 0) { throw new ArgumentException("Priority is not less than current priority"); } // Decrease key of node node.Priority = priority; // If node is a root node, AND its priority is less than the current minimum if (node.Parent == null && priority.CompareTo(Minimum.Priority) < 0) { minimumTreesNode = Trees.Find(node); // Set as minimum } // Is heap order violated? if (node.Parent != null && node.Priority.CompareTo(node.Parent.Priority) < 0) { // Cut from parent and move to root (remember ref to old parent). Node becomes unmarked in the process HeapNode <TPriority, TItem> parent = MoveToRoot(node); // Is new priority less than minimum? if (priority.CompareTo(Minimum.Priority) < 0) { minimumTreesNode = Trees.First; // Node is first element in trees list } // If parent is unmarked, mark it if (!parent.Marked) { parent.Marked = true; } else // Parent IS already marked { // Cut parent, move to root, DO FOR ALL ANCESTORS while (parent != null && parent.Marked) { parent = MoveToRoot(parent); // Move to root, check its parent } } } }
// изменить приоритет public void DecreasePriority(HeapNode <TPriority, TItem> node, TPriority priority) { // если св-во кочи сохраняются if (node.Priority.CompareTo(priority) < 0) { throw new ArgumentException(); } // изменить приоритет node.Priority = priority; // если node корневвой узел и его приоритет меньше приритета min - он минимум if (node.Parent == null && priority.CompareTo(Minimum.Priority) < 0) { minimumTreesNode = Trees.Find(node); } // если порядок кучи нарушен if (node.Parent != null && node.Priority.CompareTo(node.Parent.Priority) < 0) { // поместить node в корневой список HeapNode <TPriority, TItem> parent = MoveToRoot(node); if (priority.CompareTo(Minimum.Priority) < 0) { minimumTreesNode = Trees.First; } // отметить родителя если еще не отмечен if (!parent.Marked) { parent.Marked = true; } // если уже отмечен то сделать вырезание и для родителей которые уже отмечены else { while (parent != null && parent.Marked) { parent = MoveToRoot(parent); } } } }