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


C# TreeNode.Where方法代码示例

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


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

示例1: Main

        internal static void Main()
        {
            Console.WriteLine("N = ");
            var n = int.Parse(Console.ReadLine());

            var nodes = new TreeNode<int>[n];
            for (int i = 0; i < n; i++)
            {
                nodes[i] = new TreeNode<int>(i);
            }

            for (int i = 0; i < n - 1; i++)
            {
                Console.WriteLine("Enter parent and child node [separated by spaces] ");
                var input = Console.ReadLine().Split(' ');
                var parentValue = int.Parse(input[0]);
                var childValue = int.Parse(input[1]);

                nodes[parentValue].AddChild(nodes[childValue]);
            }

            Console.WriteLine("Root node: ");
            var rootNode = nodes.FirstOrDefault(x => x.HasParent == false);
            if (rootNode != null)
            {
                Console.WriteLine(rootNode.Value);
            }

            Console.WriteLine("Leaf nodes: ");
            var leafNodes = nodes.Where(x => x.Children.Count == 0).ToList();
            leafNodes.ForEach(x => Console.WriteLine(x.Value));

            Console.WriteLine("Middle nodes: ");
            var middleNodes = nodes.Where(x => x.Children.Count != 0 && x.HasParent).ToList();
            middleNodes.ForEach(x => Console.WriteLine(x.Value));

            Console.WriteLine("Max depth: " + TreeDepthCalculate(rootNode));
        }
开发者ID:atanas-georgiev,项目名称:Data-Structures-and-Algorithms-Homeworks,代码行数:38,代码来源:Startup.cs

示例2: Main

        static void Main()
        {
/* Paste sample input
   Example input:
7
2 4
3 2
5 0
3 5
5 6
5 1
*/
            int numberOfNodes = int.Parse(Console.ReadLine());

            TreeNode<int>[] values = new TreeNode<int>[numberOfNodes];

            for (int i = 0; i < numberOfNodes; i++)
            {
                values[i] = new TreeNode<int>(i);
            }

            for (int i = 0; i < numberOfNodes - 1; i++)
            {
                string edge = Console.ReadLine();
                string[] parentChildPair = edge.Split(' ');
                int parent = int.Parse(parentChildPair[0]);
                int child = int.Parse(parentChildPair[1]);

                values[parent].AddChild(values[child]);
            }

            // Problem 1a:
            var root = values.Where(x => !x.HasParent).FirstOrDefault();

            Console.WriteLine("Root: {0}", root.Value);

            // Problem 1b:
            var leaves = values.Where(x => x.ChildrenCount == 0).Select(x => x.Value);

            Console.WriteLine("Leafe nodes: {0}", string.Join(", ", leaves));

            // Problem 1c:

            var middleNodes = values.Where(x => x.HasParent && x.ChildrenCount > 0).Select(x => x.Value);

            Console.WriteLine("Middle nodes: {0}", string.Join(", ", middleNodes));

            // Problem 1d:
            Console.WriteLine("Longest path: {0}", FindLongestPath(root));

            // Problem 1e:
            int sum = 9;
            var allPathsWithSumS = GetAllPathsWithGivenSum(sum, values, root);
            Console.WriteLine();
            foreach (var path in allPathsWithSumS)
            {
                Console.WriteLine("Path in tree with sum {0} is: {1}", sum, path);
            }

            var subtreeSum = 6;
            var allSubTreesWithSumS = GetAllSubtreeWithGivenSum(subtreeSum, root);
            Console.WriteLine();
            foreach (var path in allSubTreesWithSumS)
            {
                Console.WriteLine("Subtree with sum {0} is: {1}", subtreeSum, path);
            }
        }
开发者ID:b-slavov,项目名称:Telerik-Software-Academy,代码行数:67,代码来源:TreeProblems.cs


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