Custom Tree Problem | Java 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 method to test ; Class to represent a tree node ; Adds a child ' n ' to this node ; Constructor ; Recursive method to print indented tree rooted with this node .
public class Tree { private TreeNode root ; public Tree [ ] buildFromLinks ( String [ ] links ) { TreeNode [ ] nodes = new TreeNode [ 26 ] ; Tree [ ] forest = new Tree [ 26 ] ; for ( String link : links ) { String [ ] ends = link . split ( " ▁ " ) ; int start = ( int ) ( ends [ 0 ] . charAt ( 0 ) - ' a ' ) ; int end = ( int ) ( ends [ 1 ] . charAt ( 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' ) ) ; for ( Tree t1 : t . buildFromLinks ( links ) ) { if ( t1 != null ) { t1 . root . printTreeIdented ( " " ) ; System . out . println ( " " ) ; } } } public static void main ( String [ ] args ) { String [ ] links1 = { " a ▁ b " , " b ▁ c " , " b ▁ d " , " a ▁ e " } ; System . out . println ( " - - - - - - - - - - - - ▁ Forest ▁ 1 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links1 ) ; String [ ] links2 = { " a ▁ b " , " a ▁ g " , " b ▁ c " , " c ▁ d " , " d ▁ e " , " c ▁ f " , " z ▁ y " , " y ▁ x " , " x ▁ w " } ; System . out . println ( " - - - - - - - - - - - - ▁ Forest ▁ 2 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links2 ) ; } } 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 ) { System . out . println ( indent + " - - > " + c ) ; for ( TreeNode child : children ) { if ( child != null ) child . printTreeIdented ( indent + " ▁ " ) ; } } }