11package com .hackerrank .java .advanced ;
22
3+ import java .util .*;
4+
35/**
6+ * Level: Medium
7+ * Problem Link: https://www.hackerrank.com/challenges/java-vistor-pattern/
8+ *
49 * @author rampatra
510 * @since 2019-06-22
611 */
7- public class JavaVisitorPattern {
12+ enum Color {
13+ RED , GREEN
14+ }
15+
16+ abstract class Tree {
17+
18+ private int value ;
19+ private Color color ;
20+ private int depth ;
21+
22+ public Tree (int value , Color color , int depth ) {
23+ this .value = value ;
24+ this .color = color ;
25+ this .depth = depth ;
26+ }
27+
28+ public int getValue () {
29+ return value ;
30+ }
31+
32+ public Color getColor () {
33+ return color ;
34+ }
35+
36+ public int getDepth () {
37+ return depth ;
38+ }
39+
40+ public abstract void accept (TreeVis visitor );
41+
42+
43+ }
44+
45+ class TreeNode extends Tree {
46+
47+ private ArrayList <Tree > children = new ArrayList <>();
48+
49+ public TreeNode (int value , Color color , int depth ) {
50+ super (value , color , depth );
51+ }
52+
53+ public void accept (TreeVis visitor ) {
54+ visitor .visitNode (this );
55+
56+ for (Tree child : children ) {
57+ child .accept (visitor );
58+ }
59+ }
60+
61+ public void addChild (Tree child ) {
62+ children .add (child );
63+ }
64+ }
65+
66+ class TreeLeaf extends Tree {
67+
68+ public TreeLeaf (int value , Color color , int depth ) {
69+ super (value , color , depth );
70+ }
71+
72+ public void accept (TreeVis visitor ) {
73+ visitor .visitLeaf (this );
74+ }
75+ }
76+
77+ abstract class TreeVis {
78+ public abstract int getResult ();
79+
80+ public abstract void visitNode (TreeNode node );
81+
82+ public abstract void visitLeaf (TreeLeaf leaf );
83+
84+ }
85+
86+ class SumInLeavesVisitor extends TreeVis {
87+ int nodeSum = 0 ;
88+ int leafSum = 0 ;
89+
90+ public int getResult () {
91+ //implement this
92+ return leafSum ;
93+ }
94+
95+ public void visitNode (TreeNode node ) {
96+ //implement this
97+ //nodeSum += node.getValue();
98+ }
99+
100+ public void visitLeaf (TreeLeaf leaf ) {
101+ //implement this
102+ leafSum += leaf .getValue ();
103+ }
104+ }
105+
106+ class ProductOfRedNodesVisitor extends TreeVis {
107+ int prodOfRedNodesAndLeaves = 1 ;
108+ private final int M = 1000000007 ;
109+
110+ public int getResult () {
111+ //implement this
112+ return prodOfRedNodesAndLeaves ;
113+ }
114+
115+ public void visitNode (TreeNode node ) {
116+ //implement this
117+ if (node .getColor () == Color .RED ) {
118+ prodOfRedNodesAndLeaves *= (node .getValue () % M );
119+ }
120+ }
121+
122+ public void visitLeaf (TreeLeaf leaf ) {
123+ //implement this
124+ if (leaf .getColor () == Color .RED ) {
125+ prodOfRedNodesAndLeaves *= (leaf .getValue () % M );
126+ }
127+ }
128+ }
129+
130+ class FancyVisitor extends TreeVis {
131+ int sumOfNodesAtEvenDepth = 0 ;
132+ int sumOfGreenLeaves = 0 ;
133+
134+ public int getResult () {
135+ //implement this
136+ return Math .abs (sumOfNodesAtEvenDepth - sumOfGreenLeaves );
137+ }
138+
139+ public void visitNode (TreeNode node ) {
140+ //implement this
141+ if (node .getDepth () % 2 == 0 ) {
142+ sumOfNodesAtEvenDepth += node .getValue ();
143+ }
144+ }
145+
146+ public void visitLeaf (TreeLeaf leaf ) {
147+ //implement this
148+ if (leaf .getColor () == Color .GREEN ) {
149+ sumOfGreenLeaves += leaf .getValue ();
150+ }
151+ }
8152}
153+
154+ public class JavaVisitorPattern {
155+
156+ public static Tree solve () {
157+ //read the tree from STDIN and return its root as a return value of this function
158+ Scanner s = new Scanner (System .in );
159+
160+ int numOfNodes = s .nextInt ();
161+ int [] nodeValues = new int [numOfNodes ];
162+ int [] nodeColors = new int [numOfNodes ];
163+ Map <Integer , Set <Integer >> parentToChildMap = new HashMap <>();
164+ Map <Integer , Integer > childToParentMap = new HashMap <>();
165+
166+ for (int i = 0 ; i < numOfNodes ; i ++) {
167+ nodeValues [i ] = s .nextInt ();
168+ }
169+ for (int i = 0 ; i < numOfNodes ; i ++) {
170+ nodeColors [i ] = s .nextInt ();
171+ }
172+ for (int i = 0 ; i < numOfNodes - 1 ; i ++) {
173+ int parentIndex = s .nextInt ();
174+ int childIndex = s .nextInt ();
175+
176+ Set <Integer > children = parentToChildMap .get (parentIndex - 1 ) != null ? parentToChildMap .get (parentIndex - 1 ) : new HashSet <>();
177+ children .add (childIndex - 1 );
178+ parentToChildMap .put (parentIndex - 1 , children );
179+ childToParentMap .put (childIndex - 1 , parentIndex - 1 );
180+ }
181+
182+ List <Tree > nodes = new ArrayList <>(numOfNodes );
183+ for (int i = 0 ; i < numOfNodes ; i ++) {
184+
185+ int depth = childToParentMap .get (i ) == null ? -1 : nodes .get (childToParentMap .get (i )).getDepth ();
186+
187+ if (parentToChildMap .get (i ) != null ) {
188+ nodes .add (new TreeNode (nodeValues [i ], nodeColors [i ] == 0 ? Color .RED : Color .GREEN , depth + 1 ));
189+ } else {
190+ nodes .add (new TreeLeaf (nodeValues [i ], nodeColors [i ] == 0 ? Color .RED : Color .GREEN , depth + 1 ));
191+ }
192+ }
193+
194+
195+ for (Map .Entry <Integer , Set <Integer >> entry : parentToChildMap .entrySet ()) {
196+
197+ TreeNode parent = (TreeNode ) nodes .get (entry .getKey ());
198+
199+ for (Integer childIndex : entry .getValue ()) {
200+ parent .addChild (nodes .get (childIndex ));
201+ }
202+ }
203+
204+ return nodes .get (0 );
205+ }
206+
207+
208+ public static void main (String [] args ) {
209+ Tree root = solve ();
210+ SumInLeavesVisitor vis1 = new SumInLeavesVisitor ();
211+ ProductOfRedNodesVisitor vis2 = new ProductOfRedNodesVisitor ();
212+ FancyVisitor vis3 = new FancyVisitor ();
213+
214+ root .accept (vis1 );
215+ root .accept (vis2 );
216+ root .accept (vis3 );
217+
218+ int res1 = vis1 .getResult ();
219+ int res2 = vis2 .getResult ();
220+ int res3 = vis3 .getResult ();
221+
222+ System .out .println (res1 );
223+ System .out .println (res2 );
224+ System .out .println (res3 );
225+ }
226+ }
0 commit comments