Exemplo n.º 1
0
        private void AddForConsolidation(HeapNode newRoots)
        {
            Debug.Assert(newRoots != null);

            foreach (var root in newRoots.GetSiblings())
            {
                Debug.Assert(root.LeftSibling != null && root.RightSibling != null);
            }

            newRoots.Parent = null;

            if (_consolidateRoots == null)
            {
                _consolidateRoots = newRoots;
            }
            else
            {
                _consolidateRoots.InsertBefore(newRoots);
            }

            foreach (var root in newRoots.GetSiblings())
            {
                Debug.Assert(root.LeftSibling != null && root.RightSibling != null);
            }
        }
Exemplo n.º 2
0
        public override NodeItem <TKey, TValue> Add(TKey key, TValue value)
        {
            var newNode = new HeapNode(key, value);

#if VERBOSE
            Console.WriteLine("Current min ({0}:{1}:{2}) >>",
                              _roots.Count(r => r != null),
                              _consolidateRoots == null ? 0 : _consolidateRoots.GetSiblings().Count(),
                              Count);
            Console.WriteLine(_minNode == null ? string.Empty : _minNode.ToString(_traversalActions));
            Console.WriteLine(">> Add >> " + newNode);
#endif

            if (Count == 0)
            {
                Debug.Assert(_roots.Count == 0);
                _minNode = newNode;
                _roots.Push(_minNode);
                Count = 1;
                return(newNode);
            }

            // Check for an improved minimum
            if (Comparer.Compare(key, _minNode.Key) <= 0)
            {
                _minNode = newNode;
            }

            // Only store the node for a later consolidation
            AddForConsolidation(newNode);
            Count++;

            return(newNode);
        }