Class QuadTreeKD<T>

  • Type Parameters:
    T - Value type.
    All Implemented Interfaces:
    Index, PointMap<T>, PointMultimap<T>

    public class QuadTreeKD<T>
    extends Object
    implements PointMap<T>, PointMultimap<T>
    This is a MX-quadtree implementation with configurable maximum depth, maximum nodes size, and (if desired) automatic guessing of root rectangle.

    For navigation during insert/delete/update/queries, it uses hypercube navigation as described by T. Zaeschke and M. Norrie, "Efficient Z-Ordered Traversal of Hypercube Indexes, BTW proceedings, 2017.

    This version of the quadtree stores for each node only the center point and the distance (radius) to the edges. This reduces space requirements but increases problems with numerical precision. Overall it is more space efficient and slightly faster.

    Author:
    ztilmann
    • Field Detail

      • ENABLE_HCI_1

        public static boolean ENABLE_HCI_1
        Enable basic HCI navigation with Algo #1 isInI(), for example for window queries.
      • ENABLE_HCI_2

        public static boolean ENABLE_HCI_2
        Enable basic HCI navigation with Algo #2 inc(), for example for window queries.
    • Method Detail

      • create

        public static <T> QuadTreeKD<T> create​(int dims)
      • create

        public static <T> QuadTreeKD<T> create​(int dims,
                                               int maxNodeSize)
      • create

        public static <T> QuadTreeKD<T> create​(int dims,
                                               int maxNodeSize,
                                               double[] center,
                                               double radius)
      • insert

        public void insert​(double[] key,
                           T value)
        Insert a key-value pair.
        Specified by:
        insert in interface PointMap<T>
        Specified by:
        insert in interface PointMultimap<T>
        Parameters:
        key - the key
        value - the value
      • contains

        public boolean contains​(double[] key)
        Check whether a given key exists.
        Specified by:
        contains in interface PointMap<T>
        Parameters:
        key - the key to check
        Returns:
        true iff the key exists
      • queryExact

        public T queryExact​(double[] key)
        Get the value associates with the key.
        Specified by:
        queryExact in interface PointMap<T>
        Parameters:
        key - the key to look up
        Returns:
        the value for the key or 'null' if the key was not found
      • contains

        public boolean contains​(double[] key,
                                T value)
        Description copied from interface: PointMultimap
        Lookup an entry, using exact match.
        Specified by:
        contains in interface PointMultimap<T>
        Parameters:
        key - the point
        value - the value
        Returns:
        `true` if an entry was found, otherwise `false`.
      • remove

        public T remove​(double[] key)
        Remove a key.
        Specified by:
        remove in interface PointMap<T>
        Parameters:
        key - key to remove
        Returns:
        the value associated with the key or 'null' if the key was not found
      • remove

        public boolean remove​(double[] key,
                              T value)
        Description copied from interface: PointMultimap
        Remove *one* entry with the given value.
        Specified by:
        remove in interface PointMultimap<T>
        Parameters:
        key - the point
        value - only entries with this value are removed
        Returns:
        the value of the entry or null if the entry was not found
      • removeIf

        public boolean removeIf​(double[] key,
                                Predicate<Index.PointEntry<T>> condition)
        Description copied from interface: PointMultimap
        Remove *one* entry with the given condition.
        Specified by:
        removeIf in interface PointMultimap<T>
        Parameters:
        key - the point
        condition - the condition required for removing an entry
        Returns:
        the value of the entry or null if the entry was not found
      • update

        public T update​(double[] oldKey,
                        double[] newKey)
        Reinsert the key.
        Specified by:
        update in interface PointMap<T>
        Parameters:
        oldKey - old key
        newKey - new key
        Returns:
        the value associated with the key or 'null' if the key was not found.
      • update

        public boolean update​(double[] oldKey,
                              double[] newKey,
                              T value)
        Reinsert the key.
        Specified by:
        update in interface PointMultimap<T>
        Parameters:
        oldKey - old key
        newKey - new key
        value - the value of the entry that should be updated.
        Returns:
        the value associated with the key or 'null' if the key was not found.
      • updateIf

        public T updateIf​(double[] oldKey,
                          double[] newKey,
                          Predicate<Index.PointEntry<T>> condition)
        Reinsert the key.
        Parameters:
        oldKey - old key
        newKey - new key
        condition - A predicate that must evaluate to 'true' for an entry to be updated.
        Returns:
        the value associated with the key or 'null' if the key was not found.
      • size

        public int size()
        Get the number of key-value pairs in the tree.
        Specified by:
        size in interface Index
        Returns:
        the size
      • clear

        public void clear()
        Removes all elements from the tree.
        Specified by:
        clear in interface Index
      • query

        public Index.PointIterator<T> query​(double[] min,
                                            double[] max)
        Query the tree, returning all points in the axis-aligned rectangle between 'min' and 'max'.
        Specified by:
        query in interface PointMap<T>
        Specified by:
        query in interface PointMultimap<T>
        Parameters:
        min - lower left corner of query
        max - upper right corner of query
        Returns:
        all entries in the rectangle
      • query1nn

        public Index.PointEntryKnn<T> query1nn​(double[] center)
        Description copied from interface: PointMap
        Finds the nearest neighbor. This uses Euclidean distance. Other distance types can only be specified directly on the index implementations.
        Specified by:
        query1nn in interface PointMap<T>
        Specified by:
        query1nn in interface PointMultimap<T>
        Parameters:
        center - center point
        Returns:
        the nearest neighbor
      • toStringTree

        public String toStringTree()
        Returns a printable list of the tree.
        Specified by:
        toStringTree in interface Index
        Returns:
        the tree as String
      • getStats

        public QuadTreeKD.QStats getStats()
        Specified by:
        getStats in interface Index
        Returns:
        Collect and return some index statistics. Note that indexes are not required to fill all fields. Also, individual indexes may use subclasses with additional fields.
      • getDims

        public int getDims()
        Specified by:
        getDims in interface Index
        Returns:
        the number of dimensions
      • queryKnn

        public Index.PointIteratorKnn<T> queryKnn​(double[] center,
                                                  int k)
        Description copied from interface: PointMap
        Finds the nearest neighbor. This uses Euclidean distance. Other distance types can only be specified directly on the index implementations.
        Specified by:
        queryKnn in interface PointMap<T>
        Specified by:
        queryKnn in interface PointMultimap<T>
        Parameters:
        center - center point
        k - number of neighbors
        Returns:
        list of nearest neighbors
      • getNodeCount

        public int getNodeCount()
        Specified by:
        getNodeCount in interface Index
      • getDepth

        public int getDepth()
        Specified by:
        getDepth in interface Index
      • getRoot

        protected QNode<T> getRoot()