/// <summary>
                /// Changes the value of a specified item.
                /// </summary>
                /// <param name="item">The value of the item to be changed.</param>
                /// <param name="newValue">The new value of the item.</param>
                public bool DecreaseKey(T item, T newValue)
                {
                    PairingHeap <T> heap;
                    LinkedListNode <PairingHeap <T> > node;

                    if (this.Find(item, out heap, out node))
                    {
                        PairingMaxHeap <T> minHeap = (PairingMaxHeap <T>)heap;
                        minHeap.root = newValue;
                        if (node == null)
                        {
                            return(true);
                        }
                        node.List.Remove(node);

                        do
                        {
                            minHeap        = (PairingMaxHeap <T>)minHeap.parent;
                            minHeap.Count -= heap.Count;
                        }while (minHeap.parent != null);

                        this.Merge(heap);
                        return(true);
                    }

                    return(false);
                }
예제 #2
0
        public void PairingMaxHeap_Test()
        {
            int nodeCount = 1000 * 10;
            //insert test
            var tree = new PairingMaxHeap <int>();

            for (int i = 0; i <= nodeCount; i++)
            {
                tree.Insert(i);
            }

            for (int i = 0; i <= nodeCount; i++)
            {
                tree.IncrementKey(i, i + 1);
            }

            //IEnumerable tests.
            Assert.AreEqual(tree.Count, tree.Count());

            int max = 0;

            for (int i = nodeCount; i >= 0; i--)
            {
                max = tree.ExtractMax();
                Assert.AreEqual(max, i + 1);
            }

            var rnd        = new Random();
            var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList();

            foreach (var item in testSeries)
            {
                tree.Insert(item);
            }

            for (int i = 0; i < testSeries.Count; i++)
            {
                var incremented = testSeries[i] + rnd.Next(0, 1000);
                tree.IncrementKey(testSeries[i], incremented);
                testSeries[i] = incremented;
            }

            testSeries = testSeries.OrderByDescending(x => x).ToList();

            for (int i = 0; i < nodeCount - 2; i++)
            {
                max = tree.ExtractMax();
                Assert.AreEqual(testSeries[i], max);
            }

            //IEnumerable tests.
            Assert.AreEqual(tree.Count, tree.Count());
        }
                /// <summary>
                /// Merges a heap into this one.
                /// </summary>
                /// <param name="other">The heap to merge into this one.</param>
                public override void Merge(PairingHeap <T> other)
                {
                    PairingMaxHeap <T> heap = other as PairingMaxHeap <T>;

                    if (heap == null)
                    {
                        this.AddRange(other);
                        return;
                    }

                    if (heap.Count == 0)
                    {
                        return;
                    }

                    if (this.Count == 0)
                    {
                        this.root     = heap.root;
                        this.children = heap.children;
                        this.Count    = heap.Count;
                        return;
                    }

                    this.Count += heap.Count;
                    if (this.root.CompareTo(heap.root) >= 0)
                    {
                        this.children.AddFirst(heap);
                        heap.parent = this;
                    }
                    else
                    {
                        PairingMaxHeap <T> newHeap = new PairingMaxHeap <T>(this.root, this.children, this.Count);
                        this.root     = heap.root;
                        this.children = heap.children;
                        this.children.AddFirst(newHeap);
                        newHeap.parent = this;
                    }
                }
예제 #4
0
        public void PairingMaxHeap_Test()
        {
            int nodeCount = 1000 * 10;
            //insert test
            var tree = new PairingMaxHeap <int>();

            var nodePointers = new List <PairingHeapNode <int> >();

            for (int i = 0; i <= nodeCount; i++)
            {
                var node = tree.Insert(i);
                nodePointers.Add(node);
            }

            for (int i = 0; i <= nodeCount; i++)
            {
                nodePointers[i].Value++;
                tree.IncrementKey(nodePointers[i]);
            }
            int max = 0;

            for (int i = nodeCount; i >= 0; i--)
            {
                max = tree.ExtractMax();
                Assert.AreEqual(i + 1, max);
            }

            nodePointers.Clear();

            var rnd        = new Random();
            var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList();


            foreach (var item in testSeries)
            {
                nodePointers.Add(tree.Insert(item));
            }

            max          = tree.ExtractMax();
            nodePointers = nodePointers.Where(x => x.Value != max).ToList();
            var resultSeries = new List <int>();

            for (int i = 0; i < nodePointers.Count; i++)
            {
                nodePointers[i].Value = nodePointers[i].Value + rnd.Next(0, 1000);
                tree.IncrementKey(nodePointers[i]);
            }

            foreach (var item in nodePointers)
            {
                resultSeries.Add(item.Value);
            }

            resultSeries = resultSeries.OrderByDescending(x => x).ToList();

            for (int i = 0; i < nodeCount - 2; i++)
            {
                max = tree.ExtractMax();
                Assert.AreEqual(resultSeries[i], max);
            }
        }