Skip to content
InternalNode.java 3.42 KiB
Newer Older
import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation.PrecomputedDistances;
import messif.objects.LocalAbstractObject;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
 * Represents internal node of MH-Tree, i.e. a non-leaf node.
 */
public class InternalNode extends Node implements Serializable {
    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 2L;

    /**
     * List of children.
     */
    private final List<Node> children;
    InternalNode(PrecomputedDistances distances, InsertType insertType, ObjectToNodeDistance objectToNodeDistance, List<Node> children) {
        super(distances, insertType, objectToNodeDistance);
        this.children = children;
    /**
     * Returns the list of child nodes.
     *
     * @return the list of child nodes
     */
    List<Node> getChildren() {
    /**
     * Returns the nearest child to the {@code object}.
     *
     * @param object object to which the distance is measured
     * @return the nearest child to the {@code object}
     */
    Node getNearestChild(LocalAbstractObject object) {
        Node nearestChild = children.get(0);
        double minDistance = nearestChild.getDistance(object);
        for (Node child : children) {
            double distance = child.getDistance(object);

            if (distance < minDistance) {
                minDistance = distance;
                nearestChild = child;
            }
        }

        return nearestChild;
    /**
     * Adds {@code object} into this node.
     *
     * @param object object to be added
     */
    void addObject(LocalAbstractObject object) {
        addObjectIntoHull(object);
    /**
     * Returns the list of objects stored in node's descendants.
     *
     * @return the list of objects stored in node's descendants
     */
    List<LocalAbstractObject> getObjects() {
        return children
                .stream()
                .map(Node::getObjects)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
    /**
     * Returns the height of this node.
     *
     * @return the height of this node
     */
    int getHeight() {
        return children
                .stream()
                .mapToInt(Node::getHeight)
                .summaryStatistics()
                .getMax() + 1;
    }

    /**
     * Adds this node and this node's descendants into {@code nodes}.
     *
     * @param nodes list of nodes, where gathered nodes are added
     */
    void gatherNodes(List<Node> nodes) {
        nodes.add(this);
        nodes.addAll(children);
    }
    /**
     * Returns a list of leaf nodes under this node.
     *
     * @return a list of leaf node under this node
     */
    List<LeafNode> getLeafNodes() {
        return children
                .stream()
                .map(Node::getLeafNodes)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

    /**
     * Returns the number of internal nodes in this subtree.
     *
     * @return the number of internal nodes in this subtree
     */
    int getInternalNodesCount() {
        return children
                .stream()
                .filter(Node::isInternal)
                .mapToInt(x -> ((InternalNode) x).getInternalNodesCount())
                .sum() + 1;
    }