@@ -31,7 +31,7 @@ public List<Node> findEquivalentSubtrees(Node root) {
3131 }
3232
3333 // Fill `vocabulary` field of nodes
34- buildNodeVocabulary (root );
34+ fillNodeVocabulary (root );
3535
3636 // Build Set<Character> -> List<Node> map
3737 Map <Set <Character >, List <Node >> voc2Nodes = new HashMap <>();
@@ -54,15 +54,8 @@ public List<Node> findEquivalentSubtrees(Node root) {
5454 voc2Nodes .get (vocabulary ).add (current );
5555 }
5656
57- // Found equivalent nodes
5857 return voc2Nodes .values ().stream ()
59- .filter (entry -> entry .size () >= 2 )
60- .map (
61- entry -> entry .stream ()
62- .sorted ((o1 , o2 ) -> o2 .vocabulary .size () - o1 .vocabulary .size ())
63- .limit (2 )
64- .toList ()
65- )
58+ .filter (nodesList -> nodesList .size () >= 2 )
6659 .sorted ((o1 , o2 ) -> o2 .stream ().mapToInt (nodeToIntFunction ()).sum () - o1 .stream ().mapToInt (nodeToIntFunction ()).sum ())
6760 .limit (1 )
6861 .findFirst ().orElse (null );
@@ -72,14 +65,14 @@ private static ToIntFunction<Node> nodeToIntFunction() {
7265 return node -> node .vocabulary .size ();
7366 }
7467
75- private Set <Character > buildNodeVocabulary (Node node ) {
68+ private Set <Character > fillNodeVocabulary (Node node ) {
7669 if (node .left != null ) {
7770 node .vocabulary .add (node .left .value );
78- node .vocabulary .addAll (buildNodeVocabulary (node .left ));
71+ node .vocabulary .addAll (fillNodeVocabulary (node .left ));
7972 }
8073 if (node .right != null ) {
8174 node .vocabulary .add (node .right .value );
82- node .vocabulary .addAll (buildNodeVocabulary (node .right ));
75+ node .vocabulary .addAll (fillNodeVocabulary (node .right ));
8376 }
8477 return node .vocabulary ;
8578 }
0 commit comments