当前位置: 首页>>代码示例>>C#>>正文


C# BinaryTree.LeastCommonAncestor方法代码示例

本文整理汇总了C#中BinaryTree.LeastCommonAncestor方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryTree.LeastCommonAncestor方法的具体用法?C# BinaryTree.LeastCommonAncestor怎么用?C# BinaryTree.LeastCommonAncestor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryTree的用法示例。


在下文中一共展示了BinaryTree.LeastCommonAncestor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            //constructing the main fields
            var readFromConsole = new ReadFromConsole();
            var employeeOne = new Employee();
            var employeeTwo = new Employee();
            var root = new Employee();
            var employees = new Dictionary<Employee, List<Employee>>();

            //Filling the fields with data from the console input
            readFromConsole.Read(employeeOne, employeeTwo, employees,ref root);

            if (employees.Count == 0)
            {
                Console.WriteLine("You didn't enter the tree!");
            }
            else
            {
                //Printing the root of Tree to check, if we entered the tree right.
                if (employees.ContainsKey(root))
                {
                    Console.WriteLine("Name of the ROOT: " + root.FirstName);
                }

                //Taking the subordinates of the root of the tree for building the EmployeeTree
                //Creating a tree using the recursiveTreeBuilder(root, root.left/root.right, dictianary with the employees)
                List<Employee> subordinates = employees[root];
                BinaryTree<Employee> employeeTree = new BinaryTree<Employee>(root, TreeBuilder.recursiveTreeBuilder(subordinates[0], employees),
                                                                                    TreeBuilder.recursiveTreeBuilder(subordinates[1], employees));
                //Printing the tree after the creation
                Console.WriteLine("Printing the builded tree");
                employeeTree.PrintInorder();

                //Starting the loop for searching LeastCommonAncestor(LCA) of two employees
                string line = "";
                while (true)
                {

                    Console.WriteLine("\nPlease enter the first name of the first employee whose boss you search:");
                    employeeOne.FirstName = Console.ReadLine();
                    Console.WriteLine("Please enter the first name of the second employee whose boss you search:");
                    employeeTwo.FirstName = Console.ReadLine();

                    BinaryTreeNode<Employee> employeeOneNode = new BinaryTreeNode<Employee>(employeeOne);
                    BinaryTreeNode<Employee> employeeTwoNode = new BinaryTreeNode<Employee>(employeeTwo);

                    //Calling the LCA for the builded tree with the (root, firstSearched, secondSearched) and returning their Boss
                    BinaryTreeNode<Employee> searchedBoss = employeeTree.LeastCommonAncestor(employeeOneNode, employeeTwoNode);

                    if (searchedBoss == null)
                    {
                        Console.WriteLine("Your employees doesn't have common ancestor");
                    }
                    else
                    {
                        Console.WriteLine("The LCA of the two employees is: " + searchedBoss.Value.FirstName);
                    }

                    Console.WriteLine("If you want to exit the search write END, else press Enter to begin new search");
                    line = Console.ReadLine();
                    if (line == "END")
                    {
                        break;
                    }

                }
            }

            Console.WriteLine("\nTo exit press any key!");
            Console.ReadKey();
        }
开发者ID:iramov,项目名称:EmployeeBinaryTree,代码行数:71,代码来源:Program.cs


注:本文中的BinaryTree.LeastCommonAncestor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。