Beispiel #1
0
        public static void RunBenchmark()
        {
            Process myProcess = Process.GetCurrentProcess();
            var     count     = 1024 * 1024;
            var     items     = new int[count];// one million items
            var     random    = new Random(13);

            for (var i = 0; i < items.Length; i++)
            {
                items[i] = random.Next(0, 1000000);
            }

            var arrayBst = new ArrayBst <int>(int.MinValue, count);
            //benchmarking insertions
            var tick = DateTime.Now;

            foreach (var item in items)
            {
                arrayBst.Add(item);
            }
            var tock = DateTime.Now;

            Console.WriteLine($"Time to insert {count} ints: {(tock-tick).TotalMilliseconds} milliseconds");
            var peakWorkingSet = myProcess.PeakWorkingSet64;

            Console.WriteLine($"  Peak physical memory usage : {peakWorkingSet}");
        }
Beispiel #2
0
        public void Rebalance(int[] items)
        {
            var bst = new ArrayBst <int>(int.MinValue, items.Length * 2);

            foreach (var item in items)
            {
                bst.Add(item);
            }

            Array.Sort(items);
            Assert.Equal(items, bst.GetSortedItems());
        }
Beispiel #3
0
        public void Remove_root()
        {
            var bst = new ArrayBst <int>(int.MinValue, 16);

            bst.Add(5);
            bst.Add(3);
            bst.Add(8);
            bst.Add(1);
            bst.Add(4);
            bst.Add(6);
            bst.Add(9);
            bst.Add(7);

            bst.Remove(5);
            Assert.False(bst.Find(5));
            Assert.True(bst.Find(6));

            Assert.True(bst.Find(1));
        }
Beispiel #4
0
        public void Add_more_than_size()
        {
            var bst = new ArrayBst <int>(int.MinValue, 8);

            bst.Add(5);
            bst.Add(3);
            bst.Add(8);
            bst.Add(1);
            bst.Add(4);
            bst.Add(6);
            bst.Add(9);
            bst.Add(7);

            //now we will need a re-allocation
            bst.Add(2);
            bst.Add(10);

            Assert.True(bst.Find(2));
            Assert.True(bst.Find(10));
        }
Beispiel #5
0
        public void Add_and_find()
        {
            var bst = new ArrayBst <int>(int.MinValue);

            bst.Add(5);
            bst.Add(3);
            bst.Add(8);
            bst.Add(1);
            bst.Add(4);
            bst.Add(6);
            bst.Add(9);
            bst.Add(7);

            Assert.True(bst.Find(1));
            Assert.True(bst.Find(3));
            Assert.True(bst.Find(6));
            Assert.True(bst.Find(9));
            Assert.True(bst.Find(7));

            Assert.False(bst.Find(0));
            Assert.False(bst.Find(10));
        }