// вырезание узла
        private HeapNode <TPriority, TItem> MoveToRoot(HeapNode <TPriority, TItem> node)
        {
            // запомнить родителя
            HeapNode <TPriority, TItem> parent = node.Parent;

            // удалить родителя, добавить в корневой список, снять пометку
            node.RemoveParent();
            Trees.AddFirst(node);
            node.Marked = false;
            return(parent);
        }
 // удалить минимум
 private void RemoveMinimum()
 {
     // все дочерние узлы поместить в корневой список и удалить их родителя
     foreach (HeapNode <TPriority, TItem> child in minimumTreesNode.Value.Children)
     {
         child.Parent = null;
         Trees.AddFirst(child);
     }
     // удалить дерево с мин. элементом
     Trees.Remove(minimumTreesNode);
     Count--;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Removes the minimum priorirty node from the list of root trees. Adds children to the list of root trees
        /// </summary>
        private void RemoveMinimum()
        {
            // Add each child as a root tree, remove ref to parent
            foreach (HeapNode <TPriority, TItem> child in minimumTreesNode.Value.Children)
            {
                child.Parent = null;
                Trees.AddFirst(child);
            }

            // Remove minimum from list of trees
            Trees.Remove(minimumTreesNode);
            Count--;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Cuts the given node from its parent and moves it to the list of root trees. Returns the parent of the node, prior to it
        /// being cut
        /// </summary>
        private HeapNode <TPriority, TItem> MoveToRoot(HeapNode <TPriority, TItem> node)
        {
            // Remeber ref to parent because child is about to be cut
            HeapNode <TPriority, TItem> parent = node.Parent;

            // Remove ref to parent and parent ref to child
            node.RemoveParent();
            Trees.AddFirst(node);
            node.Marked = false;

            // Return old parent of node
            return(parent);
        }
        // доваить элемент в кучу
        public HeapNode <TPriority, TItem> Insert(TPriority priority, TItem value)
        {
            // добавить дерево с данным элементов
            HeapNode <TPriority, TItem> node = new HeapNode <TPriority, TItem>(priority, value);

            Trees.AddFirst(node);
            //обновить минимум.
            // Элемент минимум если он единственный в куче или его приритет меньше приоритета min
            if (Count == 0 ||
                node.Priority.CompareTo(Minimum.Priority) < 0)
            {
                minimumTreesNode = Trees.First;
            }
            Count++;
            return(node);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Inserts the given item into the heap maintaining the heap order property. O(1) operation
        /// </summary>
        /// <param name="priority">The priority of the item</param>
        /// <param name="value">The object to store</param>
        /// <returns>The node that the object is stored in</returns>
        public HeapNode <TPriority, TItem> Insert(TPriority priority, TItem value)
        {
            // Add node as first item in root nodes
            HeapNode <TPriority, TItem> node = new HeapNode <TPriority, TItem>(priority, value);

            Trees.AddFirst(node);

            // Update minimum
            if (Count == 0 ||                                       // Node is first node inserted? Its minimum
                node.Priority.CompareTo(Minimum.Priority) < 0)      // Node is less than current minimum? Its minimum
            {
                minimumTreesNode = Trees.First;
            }

            Count++;
            return(node);
        }