Custom Tree Problem | C # program to create a custom tree from a given set of links . The main class that represents tree and has main method ; Returns an array of trees from links input . Links are assumed to be Strings of the form " < s > ▁ < e > " where < s > and < e > are starting and ending points for the link . The returned array is of size 26 and has non - null values at indexes corresponding to roots of trees in output ; Create two arrays for nodes and forest ; Process each link ; Find the two ends of current link ; Start node ; End node ; If start of link not seen before , add it two both arrays ; Note that it may be removed later when this character is last character of a link . For example , let we first see a -- -> b , then c -- -> a . We first add ' a ' to array of trees and when we see link c -- -> a , we remove it from trees array . ; If end of link is not seen before , add it to the nodes array ; If end of link is seen before , remove it from forest if it exists there . ; Establish Parent - Child Relationship between Start and End ; Constructor ; Driver Code ; Class to represent a tree node ; Adds a child ' n ' to this node ; Constructor ; Recursive method to print indented tree rooted with this node .
using System ; class Tree { public TreeNode root ; public Tree [ ] buildFromLinks ( String [ ] links ) { TreeNode [ ] nodes = new TreeNode [ 26 ] ; Tree [ ] forest = new Tree [ 26 ] ; foreach ( String link in links ) { char [ ] sep = { ' ▁ ' , ' ▁ ' } ; String [ ] ends = link . Split ( sep ) ; int start = ( int ) ( ends [ 0 ] [ 0 ] - ' a ' ) ; int end = ( int ) ( ends [ 1 ] [ 0 ] - ' a ' ) ; if ( nodes [ start ] == null ) { nodes [ start ] = new TreeNode ( ( char ) ( start + ' a ' ) ) ; forest [ start ] = new Tree ( nodes [ start ] ) ; } if ( nodes [ end ] == null ) nodes [ end ] = new TreeNode ( ( char ) ( end + ' a ' ) ) ; else forest [ end ] = null ; nodes [ start ] . addChild ( nodes [ end ] , end ) ; } return forest ; } public Tree ( TreeNode root ) { this . root = root ; } public static void printForest ( String [ ] links ) { Tree t = new Tree ( new TreeNode ( ' \0' ) ) ; foreach ( Tree t1 in t . buildFromLinks ( links ) ) { if ( t1 != null ) { t1 . root . printTreeIdented ( " " ) ; Console . WriteLine ( " " ) ; } } } public static void Main ( String [ ] args ) { String [ ] links1 = { " a ▁ b " , " b ▁ c " , " b ▁ d " , " a ▁ e " } ; Console . WriteLine ( " - - - - - - - - - - - - ▁ Forest ▁ 1 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links1 ) ; String [ ] links2 = { " a ▁ b " , " a ▁ g " , " b ▁ c " , " c ▁ d " , " d ▁ e " , " c ▁ f " , " z ▁ y " , " y ▁ x " , " x ▁ w " } ; Console . WriteLine ( " - - - - - - - - - - - - ▁ Forest ▁ 2 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links2 ) ; } } public class TreeNode { TreeNode [ ] children ; char c ; public void addChild ( TreeNode n , int index ) { this . children [ index ] = n ; } public TreeNode ( char c ) { this . c = c ; this . children = new TreeNode [ 26 ] ; } public void printTreeIdented ( String indent ) { Console . WriteLine ( indent + " - - > " + c ) ; foreach ( TreeNode child in children ) { if ( child != null ) child . printTreeIdented ( indent + " ▁ " ) ; } } }