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


C# Tree.TraverseDFS方法代码示例

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


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

示例1: Main

        static void Main()
        {
            var bst = new Tree<int>();

            for (int i = 0; i < 20; i++)
            {
                bst.Insert(i + 1);
            }

            Console.WriteLine("Initially: {0}", bst);
            bst.BalanceTree();

            Console.WriteLine("Balanced: {0}", bst);
            Console.WriteLine("Traverse and print (DFS)");
            bst.TraverseDFS();

            var bst2 = new Tree<int>();
            for (int i = 0; i < 20; i++)
            {
                bst2.Insert(i + 1);
            }
            bst2.BalanceTree();
            Console.WriteLine("Second tree (balanced): {0}", bst2);

            // returns true only if both trees have the same number of nodes,
            // nodes hold equal values, and trees are balanced in the same way
            Console.WriteLine("First tree equals second tree? {0}", bst.Equals(bst2));

            var bst3 = (Tree<int>)bst2.Clone();
            Console.WriteLine("Third tree, cloned from second: {0}", bst3);
            Console.WriteLine("Traverse and print cloned tree (DFS)");
            bst3.TraverseDFS();
        }
开发者ID:antonpopov,项目名称:TelerikAcademy,代码行数:33,代码来源:BTSTest.cs

示例2: Main

        static void Main()
        {
            Tree<int> tree =
            new Tree<int>(3,
                          new Tree<int>(5,
                                        new Tree<int>(0),
                                        new Tree<int>(1),
                                        new Tree<int>(6)),
                          new Tree<int>(2,
                                        new Tree<int>(4)));

            Console.WriteLine("Depth-First Search (DFS) traversal (recursive):");
            tree.TraverseDFS();
            Console.WriteLine();

            Console.WriteLine("Breadth-First Search (BFS) traversal (with queue):");
            tree.TraverseBFS();
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Depth-First Search (DFS) traversal (with stack):");
            tree.TraverseDFSWithStack();
            Console.WriteLine();

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

            Console.Write("Leafs: ");
            tree.TraverseLeafs(tree.Root);
            Console.WriteLine();
        }
开发者ID:georgimanov,项目名称:Data-Structures-and-Algorithms,代码行数:30,代码来源:Startup.cs

示例3: Main

        public static void Main()
        {
            var tree = new Tree<string>(
                new TreeNode<string>("Root"),
                    new TreeNode<string>("Music"),
                    new TreeNode<string>("Pictures",
                        new TreeNode<string>("Miami",
                            new TreeNode<string>("Landscape.jpg"),
                            new TreeNode<string>("Seaside.jpg"),
                            new TreeNode<string>("Restaurant.jpg"))),
                    new TreeNode<string>("Documents"));

            tree.TraverseDFS();
        }
开发者ID:InKolev,项目名称:TelerikAcademy,代码行数:14,代码来源:Startup.cs

示例4: GetAllSubtreesWithSum

        //f) * all subtrees with given sum S of their nodes
        public static List<Tree<int>> GetAllSubtreesWithSum(Tree<int> tree, int sum)
        {
            List<TreeNode<int>> allNodes = tree.TraverseDFS(tree.Root, new List<TreeNode<int>>());
            List<Tree<int>> allSubtreesWithSum = new List<Tree<int>>();

            foreach (var node in allNodes)
            {
                Tree<int> newTree = new Tree<int>(new TreeNode<int>(node.Value){Children = node.Children});
                List<TreeNode<int>> nodesInSubtree = newTree.TraverseDFS(newTree.Root, new List<TreeNode<int>>());

                if (nodesInSubtree.Sum(nextNode => nextNode.Value) == sum)
                {
                    allSubtreesWithSum.Add(newTree);
                }
            }

            return allSubtreesWithSum;
        }
开发者ID:VDGone,项目名称:TelerikAcademy-2,代码行数:19,代码来源:EntryPoint.cs

示例5: Main

    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int s = 6;
        var nodes = new Node<int>[n];
        for (int i = 0; i < n; i++)
        {
            nodes[i] = new Node<int>(i);
        }

        for (int i = 0; i < n - 1; i++)
        {
            string[] input = Console.ReadLine().Split(' ');
            nodes[int.Parse(input[0])].AddChild(nodes[int.Parse(input[1])]);
        }

        Console.WriteLine("The tree structure is:");
        var tree = new Tree<int>(GetRoot(nodes));
        tree.TraverseDFS();
        //task 1 a):
        Console.WriteLine("The root is: {0}", GetRoot(nodes).Value);
        //task 1 b):
        Console.WriteLine("The leafs are: {0}", string.Join(", ", GetLeaves(nodes).Select(node => node.Value)));
        //task 1 c):
        Console.WriteLine("The middle nodes are: {0}", string.Join(", ", GetMiddleNodes(nodes).Select(node => node.Value)));
        //task 1 d):
        Console.WriteLine("The longest path in the tree from the root is {0} steps", FindLongestPathDFS(GetRoot(nodes)));
        //task 1 e*):
        Console.WriteLine("The paths with sum {0} are:", s);
        foreach (var node in nodes)
        {
            AllPathsFromNode(node);
            var output = string.Join(Environment.NewLine, allPaths.Where(x => x.Sum(y => y.Value) == s).Select(x => string.Join("->", x.Select(y => y.Value))));
            if(output != string.Empty)
            {
                Console.WriteLine(output);
            }
            allPaths.Clear();
            currentPath.Clear();
        }
        //task 1 f*):
        var subTreesRoots = new List<Node<int>>();
        foreach (var node in nodes)
        {
            var subTree = new Tree<int>(node);
            if(subTree.TraverseBFS().Sum(x => x.Value) == s)
            {
                subTreesRoots.Add(node);
            }
        }
        Console.WriteLine("The subtrees with sum {0} have roots:", s);
        Console.WriteLine(string.Join(", ", subTreesRoots.Select(x => x.Value)));
    }
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:53,代码来源:TreeTest.cs

示例6: GetAllLeaves

        //b) all leaf nodes
        public static List<TreeNode<int>> GetAllLeaves(Tree<int> tree)
        {
            List<TreeNode<int>> allNodes = new List<TreeNode<int>>();

            allNodes = tree.TraverseDFS(tree.Root, allNodes);

            List<TreeNode<int>> allLeaves = allNodes.Where(node => node.Children.Count == 0).ToList();

            return allLeaves;
        }
开发者ID:VDGone,项目名称:TelerikAcademy-2,代码行数:11,代码来源:EntryPoint.cs

示例7: GetMiddleNodes

        //c) all middle nodes
        public static List<TreeNode<int>> GetMiddleNodes(Tree<int> tree)
        {
            List<TreeNode<int>> allNodes = new List<TreeNode<int>>();

            allNodes = tree.TraverseDFS(tree.Root, allNodes);

            List<TreeNode<int>> allMiddleNodes = allNodes.Where(node => node.Children.Count > 0 && node.Parent != null).ToList();

            return allMiddleNodes;
        }
开发者ID:VDGone,项目名称:TelerikAcademy-2,代码行数:11,代码来源:EntryPoint.cs


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