几何查找与 Kd 树
1d Geometric Search
在学习二叉搜索树与平衡树之后,我们将介绍其在图形化搜索中的重要应用。这一节包括了:
- 1d range search: 基本数据是点,询问区间
[lo,hi]中包含的点。 - 1d interval search: 基本数据是区间,询问区间
[lo,hi]所相交的所有区间。
1d Range Search
Geometric interpretation:
- Keys are point on a line.
- Count/find points in a given 1d interval.
Build a BST out of the given keys (points).
- count [
]. return rank(hi)-rank(lo)+1. - find [
]. - Recursively find all keys in left subtree (if any could fall in range).
- Check key in current node.
- Recursively find all keys in right subtree (if any could fall in range).
1d Interval Search
Create a BST, where each node stores an interval
- Use left endpoint as BST key.
- Store max endpoint in subtree rooted at node.
To search for intervals that intersects query interval
- Check the interval in current node.
- If interval in node intersects query interval:
- Recursively search all intervals in left subtree.
- Recursively search all intervals in right subtree.
- If interval in node doesn't intersects query interval: (# see proof below)
- If left subtree is null, go right.
- Else if max endpoint in left subtree is less than
, go right. - Else go left.
A tricky case: If the current interval doesn't intersect and search goes left, then there is either an intersection in left subtree or no intersections at all.
Pf. (#) We assume the interval with the max endpoint is [c, max]. no intersections in left subtree
The order of growth of running time for
Sweep-Line Algorithm
Sweep-line algorithm coverts:
- Orthogonal line segment intersection problem
1d range search. - Orthogonal rectangle intersection problem
1d interval search.
Orthogonal line segment intersection.
Given
To solve this, we maintain a sweep vertical line from left to right.
- x-coordinates define events. [
] - horizontal segment (left endpoint): insert y-coordinate into BST. [
] - horizontal segment (right endpoint): remove y-coordinate from BST. [
] - vertical segment: range search for interval of y-endpoints. [
]
Note that
Orthogonal rectangle intersection.
Find all intersections among a set of
To solve this, we maintain a sweep vertical line from left to right.
- x-coordinate of left and right endpoints define events. [
] - Maintain set of rectangles that intersect the sweep line in an interval search tree using y-intervals of rectangle. [
] - Left endpoint: interval search for y-interval of rectangle; insert y-interval. [
] - Right endpoint: remove y-interval. [
]
Kd Tree
普通的 BST 以及平衡树已经能够解决许多一维的图形搜索问题;借助扫描线算法的转化,它们也能够解决部分的二维图形搜索问题。那么当我们的基本数据有着大于等于二的维度呢?此时就需要依靠其他的空间划分树 (space partitioning tree) 来进行处理了。这里将会介绍其中一个简单优美的 k 维空间划分树:KD 树。
2d Orthogonal Range Search
Given
- Range search: find all keys that lie in a 2d range.
- Range count: number of keys that lie in a 2d range.
A simple solution is the grid implementation.
- Divide space into M-by-M grid of squares. [rule of thumbs:
] - Create list of points contained in each square.
- Use 2d array to directly index relevant square.
- Insert: add
to list for corresponding square. - Range search: examine only squares that intersect 2d range query.
Grid implementation is a fast, simple algorithm for evenly-distributed points.
- Initialization. [
] - Insert point. [
] - Range search. [
]
However, it becomes inefficient when clustering happens. To solve this, we need data structures that adapts gracefully to data.
2d Tree
2d tree is a space-partitioning tree that recursively divide space into two halfplanes.
Space-partitioning trees. Use a tree to represent a recursive subdivision of 2d space.
Ex. 2d tree, Quadtree, BSP tree.
2d tree is essentially a BST, but alternate using x- and y-coordinates as key.
- Search operation gives the rectangle containing point.
- Insert operation further subdivides the plane.
Here is the inner class Node for a typical 2D tree implementation.
private static class Node {
private Point2D p; // the partitioning point
private RectHV rect; // the partitioning rectangle
private Node lb; // left or bottom
private Node rt; // right or top
}
View my code repository for this course; it contains two applications of 2d tree:
- 2d orthogonal range search.
- Typical case. [
] - Worst case (even if tree is balanced). [
]
- Typical case. [
- Nearest neighbor - find closest point to query point.
- Typical case. [
] - Worst case (even if tree is balanced). [
]
- Typical case. [
From 2d to Kd
Kd tree. Recursively partition k-dimensional space into 2 halfspaces.
Kd tree is the generalization of 2d tree. It is an efficient data structure for processing k-dimensional data. The implementation is similar to 2d tree: we just cycle through dimensions ala 2d trees.





