示例#1
0
        private void SimpleBinaryHeapRandomOperations(IReadOnlyList <int> startingValues)
        {
            var simpleBinaryHeap  = new SimpleBinaryHeap <int>(startingValues);
            int randomValuesIndex = 0;

            foreach (int operation in _randomOperations)
            {
                if (operation == 1 || simpleBinaryHeap.Size <= 1)
                {
                    simpleBinaryHeap.Add(_randomValues[randomValuesIndex++]);
                }
                else if (operation == 2)
                {
                    simpleBinaryHeap.Extract();
                }
                else if (operation == 3)
                {
                    simpleBinaryHeap.Replace(_randomValues[randomValuesIndex++]);
                }
                else
                {
                    int top = simpleBinaryHeap.Top;
                }
            }
        }
示例#2
0
        public void VerifyRandomOperationsAgainstEachOther()
        {
            var rand             = new Random();
            var simpleNaiveHeap  = new SimpleNaiveHeap <int>(_sourceArray);
            var simpleBinaryHeap = new SimpleBinaryHeap <int>(_sourceArray);

            for (int i = 0; i < 10000; ++i)
            {
                int operation = rand.Next(1, 3 + 1);

                if (operation == 1 || simpleNaiveHeap.Size <= 1)
                {
                    int value = rand.Next();
                    simpleNaiveHeap.Add(value);
                    simpleBinaryHeap.Add(value);
                }
                else if (operation == 2)
                {
                    int simpleNaiveHeapTop  = simpleNaiveHeap.Extract();
                    int simpleBinaryHeapTop = simpleBinaryHeap.Extract();
                    Assert.AreEqual(simpleNaiveHeapTop, simpleBinaryHeapTop);
                }
                else
                {
                    int value = rand.Next();
                    int simpleNaiveHeapTop  = simpleNaiveHeap.Replace(value);
                    int simpleBinaryHeapTop = simpleBinaryHeap.Replace(value);
                    Assert.AreEqual(simpleNaiveHeapTop, simpleBinaryHeapTop);
                }

                Assert.AreEqual(simpleNaiveHeap.Size, simpleBinaryHeap.Size);
                Assert.AreEqual(simpleNaiveHeap.IsEmpty, simpleBinaryHeap.IsEmpty);
                Assert.AreEqual(simpleNaiveHeap.Top, simpleBinaryHeap.Top);
            }
        }