Пример #1
0
        public void Test2()
        {
            var s = new SortedDictionary <int, float>();

            const int cnt   = 10000000;
            var       raw   = Enumerable.Range(1, cnt).ToArray();
            var       datas = raw.ToArray();
            var       rand  = new Random();

            for (var i = datas.Length - 1; i > 0; i--)
            {
                var value       = datas[i];
                var randomIndex = rand.Next(0, i);
                datas[i]           = datas[randomIndex];
                datas[randomIndex] = value;
            }

            var tree = new AvlTree <int>();

            foreach (var i in datas)
            {
                tree.Add(i);
            }

            Assert.True(tree.Min == 1);
            Assert.True(tree.Max == cnt);
            Assert.True(tree.Count == raw.Length);

            var ind = 0;

            foreach (var i in tree)
            {
                Assert.True(tree.Contains(i));
                Assert.True(raw[ind] == i);
                ind++;
            }

            Assert.False(tree.Contains(0));
            Assert.False(tree.Contains(int.MinValue));

            foreach (var i in tree)
            {
                var node = tree.FindNode(i);
                Assert.True(node.HasValue);
                var prev = tree.PreviousNode(node);
                var next = tree.NextNode(node);
                if (i == 1)
                {
                    Assert.False(prev.HasValue);
                }
                else
                {
                    Assert.True(prev.HasValue);
                }

                if (i == cnt)
                {
                    Assert.False(next.HasValue);
                }
                else
                {
                    Assert.True(next.HasValue);
                }

                var nearNext = tree.NearestNext(i, int.MaxValue);
                var nearPrev = tree.NearestPrevious(i, int.MinValue);
                if (i == cnt)
                {
                    Assert.True(nearNext == int.MaxValue);
                }
                else
                {
                    Assert.True(nearNext == i + 1);
                }

                if (i == 1)
                {
                    Assert.True(nearPrev == int.MinValue);
                }
                else
                {
                    Assert.True(nearPrev == i - 1);
                }
            }

            Assert.True(tree.MaxHeight == tree.RecordHeight);
            Console.WriteLine(tree.MaxHeight);
            Console.WriteLine(Log2(cnt));

            tree.CheckHeight();
        }