Exemplo n.º 1
0
        static void ex48()
        {
            BinaryTree <int> tree = new BinaryTree <int>()
            {
                Root = new BinaryTreeNode <int>()
                {
                    Value = 1,
                    Left  = new BinaryTreeNode <int>()
                    {
                        Value = 2
                    },
                    Right = new BinaryTreeNode <int>()
                    {
                        Value = 3,
                        Left  = new BinaryTreeNode <int>()
                        {
                            Value = 4,
                            Left  = new BinaryTreeNode <int>()
                            {
                                Value = 6
                            },
                            Right = new BinaryTreeNode <int>()
                            {
                                Value = 7
                            }
                        },
                        Right = new BinaryTreeNode <int>()
                        {
                            Value = 5
                        }
                    }
                }
            };
            var n1 = tree.Find(7);
            var n2 = tree.Find(5);
            var ex = new ex_4_8();
            var r  = ex.FindFirstCommonAncestor(tree, n1, n2);

            if (r == null)
            {
                Console.Out.WriteLine($"NULL");
            }
            else
            {
                Console.Out.WriteLine($"{r.Value}");
            }
        }
Exemplo n.º 2
0
        public void TestBinaryTree()
        {
            int MAX = 1000;

            for (int cases = 0; cases < 100; cases++)
            {
                BinaryTree <int> tree = CreateTree(MAX);

                // Test tree traversal, node by node
                BinaryTree <int> current = tree.Find(1);
                for (int i = 1; i <= MAX; i++)
                {
                    Assert.AreEqual(i, current.Value);
                    Assert.AreEqual(i - 1, current.Index());
                    current = current.Next();
                }
                Assert.IsTrue(current.IsRoot());
                Assert.AreEqual(MAX, current.Index());

                // Test tree traversal, random jumps
                for (int i = 0; i < MAX; i++)
                {
                    int start  = _random.Next(1, MAX + 2);
                    int jumpTo = _random.Next(1, MAX + 2);

                    BinaryTree <int> x = start == MAX + 1 ? tree : tree.Find(start);
                    BinaryTree <int> y = x.Step(jumpTo - start);
                    if (jumpTo == MAX + 1)
                    {
                        Assert.IsTrue(y.IsRoot());
                    }
                    else
                    {
                        Assert.AreEqual(jumpTo, y.Value);
                    }
                }

                // Find indexes
                for (int i = 0; i < MAX; i++)
                {
                    BinaryTree <int> bt = tree.FindIndex(i);
                    Assert.AreEqual(i + 1, bt.Value);
                }
                BinaryTree <int> findRoot = tree.FindIndex(MAX);
                Assert.IsTrue(findRoot.IsRoot());

                if (cases % 2 == 0)
                {
                    // remove in order
                    current = tree.Find(1);
                    for (int i = 1; i <= MAX; i++)
                    {
                        Assert.AreEqual(i, current.Value);
                        current = current.Remove();
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
                else
                {
                    // remove in random order
                    for (int i = 0; i < MAX; i++)
                    {
                        int pos = _random.Next(0, MAX - i);
                        current = tree.FindIndex(pos);
                        Assert.AreEqual(pos, current.Index());
                        int oldValue = current.Value;
                        current = current.Remove();
                        Assert.AreEqual(pos, current.Index());
                        if (!current.IsRoot())
                        {
                            int newValue = current.Value;
                            Assert.IsTrue(oldValue < newValue);
                        }
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
            }
        }