From 0855b474d720a80a532f0a675949b109f1633888 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Proch=C3=A1zka?= <david@prochazka.dev>
Date: Tue, 9 Feb 2021 15:36:07 +0100
Subject: [PATCH] FIX: getNearestChild cleanup

---
 src/mhtree/BuildTree.java    |  2 +-
 src/mhtree/InternalNode.java | 11 +++++------
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/mhtree/BuildTree.java b/src/mhtree/BuildTree.java
index 379c128..28912b6 100644
--- a/src/mhtree/BuildTree.java
+++ b/src/mhtree/BuildTree.java
@@ -125,7 +125,7 @@ class BuildTree {
                 .reduce(Float.MAX_VALUE, Math::min);
 
         Map<Node, Float> nodeToObjectDistance = Arrays.stream(nodes)
-                .collect(Collectors.toMap(node -> node, getMinHullObjectDistance));
+                .collect(Collectors.toMap(Function.identity(), getMinHullObjectDistance));
 
         // Add object to the node with minimal distance to this object
         Collections.min(nodeToObjectDistance.entrySet(), Map.Entry.comparingByValue()).getKey().addObject(object);
diff --git a/src/mhtree/InternalNode.java b/src/mhtree/InternalNode.java
index 8a6e656..4d418d4 100644
--- a/src/mhtree/InternalNode.java
+++ b/src/mhtree/InternalNode.java
@@ -4,10 +4,8 @@ import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation.Precompu
 import messif.objects.LocalAbstractObject;
 
 import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 public class InternalNode extends Node implements Serializable {
@@ -33,9 +31,10 @@ public class InternalNode extends Node implements Serializable {
     }
 
     public Node getNearestChild(LocalAbstractObject object) {
-        List<Float> distances = children.stream().map(child -> child.getDistance(object)).collect(Collectors.toList());
+        Map<Node, Float> nodeToObjectDistance = children.stream()
+                .collect(Collectors.toMap(Function.identity(), node -> node.getDistance(object)));
 
-        return children.get(Utils.getIndexOfMinElement(distances));
+        return Collections.min(nodeToObjectDistance.entrySet(), Map.Entry.comparingByValue()).getKey();
     }
 
     public void addObject(LocalAbstractObject object) {
-- 
GitLab