Пример #1
0
        public static void LcaTest()
        {
            var tr = PreOrderToBST.Construct(new[] { 10, 5, 1, 7, 40, 50 });

            Console.WriteLine("Using Node Paths -");
            Console.WriteLine("7 and 50: " + LowestCommonAncestorBST.FindLca(tr._root, new BSTNode(7), new BSTNode(50)));
            Console.WriteLine("7 and 1: " + LowestCommonAncestorBST.FindLca(tr._root, new BSTNode(7), new BSTNode(1)));
            Console.WriteLine("1 and 10: " + LowestCommonAncestorBST.FindLca(tr._root, new BSTNode(1), new BSTNode(10)));
            Console.WriteLine("7 and 40: " + LowestCommonAncestorBST.FindLca(tr._root, new BSTNode(7), new BSTNode(40)));
            Console.WriteLine("50 and 40: " + LowestCommonAncestorBST.FindLca(tr._root, new BSTNode(50), new BSTNode(40)));
            Console.WriteLine();
            Console.WriteLine("Using Recursion - ");
            Console.WriteLine("7 and 50: " + LowestCommonAncestorBST.FindLcaUsingRecursion(tr._root, new BSTNode(7), new BSTNode(50)));
            Console.WriteLine("7 and 1: " + LowestCommonAncestorBST.FindLcaUsingRecursion(tr._root, new BSTNode(7), new BSTNode(1)));
            Console.WriteLine("1 and 10: " + LowestCommonAncestorBST.FindLcaUsingRecursion(tr._root, new BSTNode(1), new BSTNode(10)));
            Console.WriteLine("7 and 40: " + LowestCommonAncestorBST.FindLcaUsingRecursion(tr._root, new BSTNode(7), new BSTNode(40)));
            Console.WriteLine("50 and 40: " + LowestCommonAncestorBST.FindLcaUsingRecursion(tr._root, new BSTNode(50), new BSTNode(40)));
            Console.WriteLine();
            Console.WriteLine("Using Iteration - ");
            Console.WriteLine("7 and 50: " + LowestCommonAncestorBST.FindLcaUsingIteration(tr._root, new BSTNode(7), new BSTNode(50)));
            Console.WriteLine("7 and 1: " + LowestCommonAncestorBST.FindLcaUsingIteration(tr._root, new BSTNode(7), new BSTNode(1)));
            Console.WriteLine("1 and 10: " + LowestCommonAncestorBST.FindLcaUsingIteration(tr._root, new BSTNode(1), new BSTNode(10)));
            Console.WriteLine("7 and 40: " + LowestCommonAncestorBST.FindLcaUsingIteration(tr._root, new BSTNode(7), new BSTNode(40)));
            Console.WriteLine("50 and 40: " + LowestCommonAncestorBST.FindLcaUsingIteration(tr._root, new BSTNode(50), new BSTNode(40)));
        }
Пример #2
0
        public static void FindPathTest()
        {
            var tr   = PreOrderToBST.Construct(new[] { 10, 5, 1, 7, 40, 50 });
            var path = BSTUtilities.FindPath(tr._root, 50);

            foreach (var i in path)
            {
                Console.Write(i + ",");
            }

            Console.WriteLine();

            path = BSTUtilities.FindPath(tr._root, 7);
            foreach (var i in path)
            {
                Console.Write(i + ",");
            }

            Console.WriteLine();
            path = BSTUtilities.FindPath(tr._root, 5);
            foreach (var i in path)
            {
                Console.Write(i + ",");
            }
        }
Пример #3
0
        public static void FindCeilBSTTest()
        {
            var tr = PreOrderToBST.Construct(new[] { 8, 4, 2, 6, 12, 10, 14 });

            for (var i = 0; i < 16; i++)
            {
                Console.WriteLine($"{i} : " + FloorAndCeilBST.FindCeilNode(tr._root, i)?.data);
            }
        }
Пример #4
0
        public static void PreorderToBSTTest()
        {
            var tr = PreOrderToBST.Construct(new[] { 10, 5, 1, 7, 40, 50 });

            tr.InOrderTraversal();
        }