Kd write-dimensional tree (including comparators, Java language description)

About Kd tree ah, many people may never heard of.

Kd tree K-dimension tree abbreviations are data points in the K-dimensional space (two-dimensional (x, y), D (x, y, z), K -dimensional (x, y, z, ... )) in the a way of dividing the high-dimensional index tree data structure . Kd tree tree is a spatial division , plainly, the entire space is divided into specific sections, then the relevant portion of the search operation in the specific space.

Kd tree evolved from BST (Binary search tree), related search operation within a particular space partitioned good part. Kd tree is a balanced binary tree .

Kd tree is mainly used in multi-dimensional space-critical data search , commonly used in large-scale high-dimensional data-intensive than using the scene to find pairs, mainly nearest neighbor lookup (Nearest Neighbor) and approximate nearest neighbor lookup (Approximate Nearest Neighbor). Computer vision (CV) in the main image retrieval and recognition of high-dimensional feature vectors to find and match . (This paragraph reference from following this article)

Interested can look at this explanation

Here we simply implement a two-dimensional division Kd tree , two-dimensional Yo ~.

Two-dimensional code that implements Kd tree

/**
 * Quick illustration of a two-dimensional tree.
 */
public class KdTree<T extends Comparable<? super T>> {

    private static class KdNode<T> {
        T[]     data;
        KdNode<T> left;
        KdNode<T> right;

        @SuppressWarnings("unchecked")
        KdNode(T[] item) {
            data = (T[]) new Comparable[2];
            data[0] = item[0];
            data[1] = item[1];
            left = right = null;
        }
    }

    private KdNode<T> root;

    public KdTree() {
        root = null;
    }

    public void insert(T[] x) {
        root = insert(x, root, 0);
    }

    private KdNode<T> insert(T[] x, KdNode<T> t, int level) {
        if(t == null) {
            t = new KdNode<>(x);
        } else if( x[level].compareTo(t.data[level] ) < 0) {
            t.left = insert(x, t.left, 1 - level);
        } else {
            t.right = insert(x, t.right, 1 - level);
        }
        return t;
    }

    /**
     * Print items satisfying
     * low[0] <= x[0] <= high[0] and
     * low[1] <= x[1] <= high[1].
     */
    public void printRange(T[] low, T[] high) {
        printRange(low, high, root, 0);
    }

    private void printRange(T[] low, T[] high, KdNode<T> t, int level) {
        if(t != null) {
            if(low[0].compareTo(t.data[0]) <= 0 && low[1].compareTo(t.data[1] ) <= 0 &&
                       high[0].compareTo(t.data[0]) >= 0 && high[1].compareTo(t.data[1]) >= 0) {
                System.out.println("(" + t.data[ 0 ] + "," + t.data[ 1 ] + ")");
            }
            if(low[level].compareTo(t.data[level]) <= 0) {
                printRange(low, high, t.left, 1-level);
            }
            if(high[level].compareTo(t.data[level]) >= 0) {
                printRange(low, high, t.right, 1-level);
            }
        }
    }

}

test

public class KdTreeTest {
    public static void main(String [] args) {
        KdTree<Integer> t = new KdTree<>();
        System.out.println("Starting program");
        for(int i = 300; i < 370; i++) {
            Integer [] it = new Integer[2];
            it[0] = i;
            it[1] = 2500-i;
            t.insert(it);
        }
        Integer [] low = {70, 2186};
        Integer [] high = {1200, 2200};
        t.printRange(low, high);
    }
}

Test Results:

Starting program
(300,2200)
(301,2199)
(302,2198)
(303,2197)
(304,2196)
(305,2195)
(306,2194)
(307,2193)
(308,2192)
(309,2191)
(310,2190)
(311,2189)
(312,2188)
(313,2187)
(314,2186)
发布了570 篇原创文章 · 获赞 1179 · 访问量 36万+

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104467560