public static void printChildrenA(NodeA current, NodeA[] nodes, int depth) { for (int childIndex = 0; childIndex < current.getChildren().Length; childIndex++) { for (int nodeIndex = 0; nodeIndex < nodes.Length; nodeIndex++) { if (current.getChildren()[childIndex] == nodes[nodeIndex].getId()) { for (int i = 0; i < depth; i++) { Console.Write(" "); } Console.WriteLine(nodes[nodeIndex].getId()); printChildrenA(nodes[nodeIndex], nodes, depth + 1); } } } }
public static NodeB[] addRecursiveChildren(NodeA[] treeA, NodeA parent) { NodeB[] treeB = new NodeB[parent.getChildren().Length]; int index = 0; for (int i = 0; i < treeA.Length; i++) { for (int j = 0; j < parent.getChildren().Length; j++) { if (treeA[i].getId() == parent.getChildren()[j]) { NodeB child = new NodeB(treeA[i].getId()); child.setChildren(addRecursiveChildren(treeA, treeA[i])); treeB[index] = child; index++; } } } return(treeB); }
static void Main(string[] args) { NodeA[] tree1 = new NodeA[10]; for (int i = 0; i < tree1.Length; i++) { tree1[i] = new NodeA(); if (i > 0) { tree1[i].setChildren(new [] { tree1[i - 1].getId() }); } } printTreeA(tree1); NodeB[] treeB = toRecursiveTree(tree1); printTreeB(treeB); //stop program. Console.WriteLine("Click enter to close program"); Console.In.ReadLine(); }