Beispiel #1
0
        public void ModifyTest()
        {
            Heap <int> simpleHeap = new Heap <int>();
            HeapWithIndices <int, String> heap = new HeapWithIndices <int, string>();

            for (int i = 0; i < 10; ++i)
            {
                simpleHeap.Add(i);
            }
            for (int i = 0; i <= 10; ++i)
            {
                heap.Add(i, i.ToString());
            }
            simpleHeap.ModifyTop(12);
            Assert.AreEqual(1, simpleHeap.Peek);
            simpleHeap.ModifyTop(-11);
            Assert.AreEqual(-11, simpleHeap.Peek);
            for (int i = 0; i < 9; ++i)
            {
                simpleHeap.Pop();
            }
            Assert.AreEqual(12, simpleHeap.Peek);
            heap.Modify("10", -1111);
            Assert.AreEqual(-1111, heap.Peek.Value);
            Assert.AreEqual("10", heap.Peek.Key);
            heap.Pop();
            Assert.AreEqual("0", heap.Peek.Key);
            Assert.AreEqual(0, heap.Peek.Value);
            for (int i = 0; i < 9; ++i)
            {
                heap.Pop();
            }
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(9, heap.Peek.Value);
            heap.ModifyTop(1111);
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(1111, heap.Peek.Value);
        }
Beispiel #2
0
        public void HeapWithDictionaryStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, long> priorityQueue = new SortedDictionary <long, long>();
            Dictionary <long, long>       dictionary    = new Dictionary <long, long>();
            HeapWithIndices <long, long>  myHeap        = new HeapWithIndices <long, long>(10);
            long   lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                if (rand.Next() % 3 == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        long key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                else
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    myHeap.Add(x, (long)i);
                    priorityQueue.Add(x, (long)i);
                    dictionary.Add((long)i, x);
                    lastKey = i;
                }
                if (rand.Next() % 3 == 0)
                {
                    if (rand.Next() % 2 == 0)
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                priorityQueue.Remove(dictionary[lastKey]);
                                myHeap.Remove((long)lastKey);
                                dictionary.Remove(lastKey);
                            }
                        }
                    }
                    else
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (rand.Next() % 2 == 0)
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = myHeap.PeekKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.ModifyTop(newTop);
                            }
                            else if (myHeap.ContainsKey((long)lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public void HeapWithIndicesStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, int> priorityQueue = new SortedDictionary <long, int>();
            Dictionary <int, long>       dictionary    = new Dictionary <int, long>();
            HeapWithIndices <long>       myHeap        = new HeapWithIndices <long>(10);
            int    lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                int type = rand.Next() % 3;
                if (type == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        int  key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                if (rand.NextDouble() > 0.1)
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    int y = myHeap.Add(x);
                    priorityQueue.Add(x, y);
                    dictionary.Add(y, x);
                    lastKey = y;
                }
                if (type == 1)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (dictionary.ContainsKey(lastKey))
                        {
                            priorityQueue.Remove(dictionary[lastKey]);
                            myHeap.Remove(lastKey);
                            dictionary.Remove(lastKey);
                        }
                    }
                }
                if (type == 2)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (rand.NextDouble() < 0.5)
                        {
                            long newTop = rand.Next();
                            while (priorityQueue.ContainsKey(newTop))
                            {
                                newTop = rand.Next();
                            }

                            int key = myHeap.PeekKey;
                            priorityQueue.Remove(dictionary[key]);
                            dictionary.Remove(key);
                            priorityQueue.Add(newTop, key);
                            dictionary.Add(key, newTop);
                            myHeap.ModifyTop(newTop);
                        }
                        else
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }
                                int key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }