static void Main(string[] args)
        {
            RedBlackTreeList <int> tree = new RedBlackTreeList <int>();

            for (int i = 1; i <= 10; i++)
            {
                tree.Add(i);
            }

            tree.Remove(9);

            bool contains = tree.ContainsKey(5);

            Console.WriteLine("Does value exist? " + (contains ? "yes" : "no"));

            uint count = tree.Count;

            tree.Greatest(out int greatest);
            tree.Least(out int least);
            Console.WriteLine($"{count} elements in the range {least}-{greatest}");

            Console.WriteLine("Values: " + string.Join(", ", tree.GetEnumerable()));

            Console.Write("Values: ");
            foreach (EntryList <int> node in tree)
            {
                Console.Write(node + " ");
            }

            Console.ReadKey();
        }
예제 #2
0
 /// <inheritdoc/>
 public PriorityQueue(PriorityQueueType queueType, IComparer<TPriority> comparer) {
     if ((queueType != PriorityQueueType.Minimum) && (queueType != PriorityQueueType.Maximum)) {
         throw new ArgumentOutOfRangeException("queueType");
     }
     this.queueType = queueType;
     tree = new RedBlackTreeList<TPriority, TValue>(comparer);
 }
예제 #3
0
        private void TestRedBlackHeight()
        {
            Random rnd = new Random(this.seed.Value);

            int minOvershoot = Int32.MaxValue;
            int maxOvershoot = 0;

            for (int i = 0; i < 64; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    ulong c = unchecked ((1UL << i) + (ulong)j);

                    int estimatedMaxDepth = RedBlackTreeList <int> .EstimateMaxDepth(c);

                    int theoreticalMaxDepth = RedBlackTreeList <int> .TheoreticalMaxDepth(c);

                    try
                    {
                        TestTrue("AVL max depth", delegate() { return(estimatedMaxDepth >= theoreticalMaxDepth); });
                    }
                    catch (Exception)
                    {
                        WriteLine("failing n value: {0}", c);
                        throw;
                    }

                    minOvershoot = Math.Min(minOvershoot, estimatedMaxDepth - theoreticalMaxDepth);
                    maxOvershoot = Math.Max(maxOvershoot, estimatedMaxDepth - theoreticalMaxDepth);
                }
            }

            Stopwatch timer = Stopwatch.StartNew();
            long      iter  = 0;

            while (timer.ElapsedMilliseconds < DurationMSec)
            {
                double r = rnd.NextDouble() + rnd.NextDouble() * (1.0 / 2147483648); // inside knowledge -- generate full 53 bits of randomness
                double d = Math.Pow(2, rnd.NextDouble() * 64);
                ulong  c = Math.Max(unchecked ((ulong)d), Int64.MaxValue);

                int estimatedMaxDepth = RedBlackTreeList <int> .EstimateMaxDepth(c);

                int theoreticalMaxDepth = RedBlackTreeList <int> .TheoreticalMaxDepth(c);

                try
                {
                    TestTrue("AVL max depth", delegate() { return(estimatedMaxDepth >= theoreticalMaxDepth); });
                }
                catch (Exception)
                {
                    WriteLine("failing n value: {0}", c);
                    throw;
                }

                minOvershoot = Math.Min(minOvershoot, estimatedMaxDepth - theoreticalMaxDepth);
                maxOvershoot = Math.Max(maxOvershoot, estimatedMaxDepth - theoreticalMaxDepth);

                iter++;
            }

            //Console.WriteLine("Red-Black height estimator overshoot: min={0}, max={1}, iter={2}", minOvershoot, maxOvershoot, iter);
        }