Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.
Do more with
// print node and all children of that node
void PrintTree(Node n)
{
foreach(child c of n) PrintTree(c);
Console.WriteLine(n.ToString());
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<MyTreeStructure> Root = new List<MyTreeStructure>();
private void button1_Click(object sender, EventArgs e)
{
// Create Tree structure
//Parent 1
MyTreeStructure parent1 = new MyTreeStructure("Parent1", "Parent", null);
Root.Add(parent1);
MyTreeStructure child1 = new MyTreeStructure("Child1", "Child", null);
parent1.Descendent.Add(child1);
MyTreeStructure child2 = new MyTreeStructure("Child2", "Child", null);
parent1.Descendent.Add(child2);
MyTreeStructure gChild1 = new MyTreeStructure("Grandchild1", "Grandchild", null);
child2.Descendent.Add(gChild1);
//Parent 2
MyTreeStructure parent2 = new MyTreeStructure("Parent2", "Parent", null);
Root.Add(parent2);
MyTreeStructure child21 = new MyTreeStructure("Child1", "Child", null);
parent2.Descendent.Add(child21);
MyTreeStructure gchild22 = new MyTreeStructure("Grandchild1", "Grandchild", null);
child21.Descendent.Add(gchild22);
MyTreeStructure ggChild1 = new MyTreeStructure("Grandgrandchild1", "Grandgrandchild1", null);
gchild22.Descendent.Add(ggChild1);
//Parent 3
MyTreeStructure parent3 = new MyTreeStructure("Parent3", "Parent", null);
Root.Add(parent3);
}
private void button2_Click(object sender, EventArgs e)
{
// Iterate through the parents
foreach (MyTreeStructure node in Root)
{
// Print the parents sub tree
PrintTree(node);
}
}
int indent = 1;
private void PrintTree(MyTreeStructure node)
{
Console.WriteLine("{0," + indent.ToString() + "}{1}", " ", node.Name);
if (node.Descendent != null)
{
indent += 5;
foreach (MyTreeStructure child in node.Descendent)
{
// This is a recursive call to itself
PrintTree(child);
}
indent -= 5;
}
}
}
// The objects that make up the tree
public class MyTreeStructure
{
public string Name;
public string Type;
public List<MyTreeStructure> Descendent = new List<MyTreeStructure>();
public MyTreeStructure(string name, string type, MyTreeStructure descendent)
{
Name = name;
Type = type;
if(descendent != null)
Descendent.Add(descendent);
}
}
public class Node
{
private readonly List<Node> children = new List<Node>();
private readonly string foo;
public Node(string foo) { this.foo = foo; }
public string Foo { get; set; }
public ICollection<Node> Children { get { return children; } }
}
public static class Program
{
public static int Main(string[] args)
{
// create tree
Node tree = new Node("tree");
Node child1 = new Node("child1");
Node child2 = new Node("child2");
Node gchild1a = new Node("gchild1a");
Node gchild1b = new Node("gchild1b");
Node gchild2a = new Node("gchild2a");
Node gchild2b = new Node("gchild2b");
Node ggchild1ai = new Node("ggchild1ai");
tree.Children.Add(child1);
tree.Children.Add(child2);
child1.Children.Add(gchild1a);
child1.Children.Add(gchild1b);
child2.Children.Add(gchild2a);
child2.Children.Add(gchild2b);
gchild1a.Children.Add(ggchild1ai);
// start recursion
PrintTree(tree);
}
private int printIndent = 0;
private void PrintTree(Node n)
{
Console.Write('-', printIndent);
Console.WriteLine(n.Foo);
printIndent++;
foreach (Node c in n.Children) PrintTree(c);
printIndent--;
}
}
void FillMainCats()
{
foreach (Category rootCat in Category.GetAllRootLeveCategories())
FillTree(theTreeViewControl.Nodes, rootCat);
}
void FillTree(TreeNodeCollection nodes, Category category)
{
string displayName = (from c in category.LangIDCats where c.LangID == "en_US" select c.DisplayName).SingleOrDefault();
TreeNode newNode = new TreeNode(displayName, category.Code);
newNode.SelectAction = TreeNodeSelectAction.Expand;
nodes.Add(newNode);
foreach (Category childCat in category.GetAllChildCats(category.id))
FillTree(newNode.ChildNodes, childCat);
}
Premium Content
You need an Expert Office subscription to comment.Start Free Trial