示例#1
0
        public void NoLCA()
        {
            BinaryTree <string> tree = getExampleTree();
            Node <string>       k    = Q03 <string> .BfsFind(tree, "K")?[0];

            Assert.NotNull(k);

            Node <string> lcaNode = Q04.FindLCA(tree, k as Node, tree.Root as Node);

            Assert.Null(lcaNode);
        }
示例#2
0
        public void Test01()
        {
            BinaryTree <string> tree = getExampleTree();
            Node <string>       d    = Q03 <string> .BfsFind(tree, "D")?[0];

            Node <string> n = Q03 <string> .BfsFind(tree, "N")?[0];

            Assert.NotNull(d);
            Assert.NotNull(n);

            Node <string> lcaNode = Q04.FindLCA(tree, d as Node, n as Node);

            Assert.Equal(tree.Root, lcaNode);
        }
示例#3
0
        public void Test02()
        {
            BinaryTree <string> tree = getExampleTree();
            Node <string>       c    = Q03 <string> .BfsFind(tree, "C")?[0];

            Node <string> l = Q03 <string> .BfsFind(tree, "L")?[0];

            Node <string> p = Q03 <string> .BfsFind(tree, "P")?[0];

            Assert.NotNull(c);
            Assert.NotNull(l);
            Assert.NotNull(p);
            Assert.Equal(l, c.Left.Left);
            Assert.Equal(p, c.Right);

            Node <string> lcaNode = Q04.FindLCA(tree, l as Node, p as Node);

            Assert.Equal(c, lcaNode);
            Assert.Equal("C", lcaNode.Value);
        }
示例#4
0
        public void Example()
        {
            BinaryTree <string> tree = getExampleTree();
            Node <string>       k    = Q03 <string> .BfsFind(tree, "K")?[0];

            Node <string> m = Q03 <string> .BfsFind(tree, "M")?[0];

            Node <string> n = Q03 <string> .BfsFind(tree, "N")?[0];

            Assert.NotNull(k);
            Assert.NotNull(m);
            Assert.NotNull(n);
            Assert.Equal(m, k.Left.Right);
            Assert.Equal(n, k.Right);
            Assert.Equal(k, (m as Node).Parent.Parent);
            Assert.Equal(k, (n as Node).Parent);

            Node <string> lcaNode = Q04.FindLCA(tree, m as Node, n as Node);

            Assert.Equal(k, lcaNode);
            Assert.Equal("K", lcaNode.Value);
        }