本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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;
}
示例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)));
}
示例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;
}
示例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;
}