-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPrint-Tree-Console.linq
81 lines (67 loc) · 1.43 KB
/
Print-Tree-Console.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<Query Kind="Program" />
//Some parts of this code are taken from this github: https://github.com/andrewlock/blog-examples
void Main()
{
var node = new Node();
node.Name = "Root";
var list = new Nodes();
{
list.Add(new Node() { Name = "Ride" });
list.Add(new Node() { Name = "The" });
list.Add(new Node() { Name = "Lightning" });
};
node.Children.Add(new Node()
{
Name = "Master",
Children = list
});
node.Children.Add(new Node()
{
Name = "Of",
});
node.Children.Add(new Node()
{
Name = "Puppets",
});
node.Dump();
PrintNode(node);
}
// Constants for drawing lines and spaces
private const string _cross = " ├─";
private const string _corner = " └─";
private const string _vertical = " │ ";
private const string _space = " ";
static void PrintNode(Node node, string indent = "")
{
Console.WriteLine(node.Name);
var numberOfChildren = node.Children.Count;
for (var i = 0; i < numberOfChildren; i++)
{
var child = node.Children[i];
var isLast = (i == (numberOfChildren - 1));
PrintChildNode(child, indent, isLast);
}
}
static void PrintChildNode(Node node, string indent, bool isLast)
{
Console.Write(indent);
if (isLast)
{
Console.Write(_corner);
indent += _space;
}
else
{
Console.Write(_cross);
indent += _vertical;
}
PrintNode(node, indent);
}
class Node
{
public string Name { get; set; }
public Nodes Children { get; set; } = new Nodes();
}
class Nodes : List<Node>
{
}