示例#1
0
        public void BalanceFactorTest()
        {
            AvlTree<int> actual = new AvlTree<int> { 10, 20 };
            AvlTreeNode<int> root = actual.FindNode(10);
            AvlTreeNode<int> leaf = actual.FindNode(20);
            AvlTreeNode<int> emptyNode = default(AvlTreeNode<int>);

            Assert.AreEqual(actual.GetBalanceFactor(root), -1);
            Assert.AreEqual(actual.GetBalanceFactor(leaf), 0);
            Assert.AreEqual(actual.GetBalanceFactor(emptyNode), 0);
        }
示例#2
0
        public void BalanceFactorTest()
        {
            AvlTree <int> actual = new AvlTree <int> {
                10, 20
            };
            AvlTreeNode <int> root      = actual.FindNode(10);
            AvlTreeNode <int> leaf      = actual.FindNode(20);
            AvlTreeNode <int> emptyNode = default(AvlTreeNode <int>);

            Assert.AreEqual(actual.GetBalanceFactor(root), -1);
            Assert.AreEqual(actual.GetBalanceFactor(leaf), 0);
            Assert.AreEqual(actual.GetBalanceFactor(emptyNode), 0);
        }
示例#3
0
        public void BalanceFactorPUT(int val1, int val2)
        {
            PexAssume.AreNotEqual(val1, val2);
            int minVal = Math.Min(val1, val2);
            int other = Math.Max(val1, val2);
            AvlTree<int> actual = new AvlTree<int> {minVal,other};

            AvlTreeNode<int> root = actual.FindNode(minVal);
            AvlTreeNode<int> leaf = actual.FindNode(other);
            AvlTreeNode<int> emptyNode = default(AvlTreeNode<int>);

            PexAssert.AreEqual(-1, actual.GetBalanceFactor(root));
            PexAssert.AreEqual(0, actual.GetBalanceFactor(leaf));
            PexAssert.AreEqual(0, actual.GetBalanceFactor(emptyNode));
        }
示例#4
0
        public void BalanceFactorPUT(int val1, int val2)
        {
            PexAssume.AreNotEqual(val1, val2);
            int           minVal = Math.Min(val1, val2);
            int           other  = Math.Max(val1, val2);
            AvlTree <int> actual = new AvlTree <int> {
                minVal, other
            };

            AvlTreeNode <int> root      = actual.FindNode(minVal);
            AvlTreeNode <int> leaf      = actual.FindNode(other);
            AvlTreeNode <int> emptyNode = default(AvlTreeNode <int>);

            PexAssert.AreEqual(-1, actual.GetBalanceFactor(root));
            PexAssert.AreEqual(0, actual.GetBalanceFactor(leaf));
            PexAssert.AreEqual(0, actual.GetBalanceFactor(emptyNode));
        }
        static void Main(string[] args)
        {
            AvlTree <int> tree = new AvlTree <int>();

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

            Console.WriteLine("In-order: " + string.Join(", ", tree.GetInorderEnumerator()));
            Console.WriteLine("Post-order: " + string.Join(", ", tree.GetPostorderEnumerator()));
            Console.WriteLine("Breadth-first: " + string.Join(", ", tree.GetBreadthFirstEnumerator()));

            AvlTreeNode <int> node = tree.FindNode(8);

            Console.WriteLine($"Children of node {node.Value} (height = {node.Height}): {node.Left.Value} and {node.Right.Value}.");
        }
示例#6
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();
        }