
end if
end if
If Q(v. LeftChild) or Q(v. RightChild), respectively, intersect with R, then rerun the procedure with v.
LeftChild and Q(v. LeftChild) or v. RightChild and Q(v. RightChild), respectively.
For convenience, we discuss time and space complexity for the 2D case. It can be shown that in 2D, the
number of nodes that fulfill R(v) ∩ Q ≠ Ø is restricted to , where h denotes the depth of the
tree and k denotes the number of points in Q, i.e., the size of the answer; see [Klein 05] or [de Berg et al.
00]. Altogether, the efficiency of the kd-tree with respect to range queries depends on the depth of the
tree. A balanced kd-tree can be easily constructed. We sort the points with respect to the x- and y-
coordinates. With this order, we recursively split the set into subsets of equal size in time O(log n). The
construction runs in time O(n log n), and the tree has depth O(log n). Altogether, the following theorem
can be proven.
Theorem 2.9. A balanced kd-tree for n points in the plane can be constructed in O(n log n) and needs
O(n) space. A range query with an axis-parallel rectangle can be answered in time O( + a), where a
denotes the size of the answer.
As mentioned earlier, the 2D kd-tree can be easily generalized to arbitrary dimension d, splitting the
points successively with respect to the given axes in a balanced way. Fortunately, the depth of the
balanced tree still is bounded by O(log n), and the tree can be built up in time O(n log n) and space O(n)
for fixed dimension d. Therefore, the kd-tree is optimal in space. A rectangular range query can be
answered in time .
Theorem 2.10. A balanced d-dimensional kd-tree for n points in can be constructed in O(n log n) and
needs O(n) space. A range query with an axis-parallel box in can be answered in time
, where k denotes the size of the answer.
The main advantage of the d-dimensional kd-tree lies in its small size. In the following section, we will see
that rectangular range queries can be answered more efficiently with the help of range trees. A
WeakDelete operation in the kd-tree can be done in O(log n). We simply traverse the tree up to the
corresponding node and mark the node as deleted.
Lemma 2.11. A WeakDelete operation for a balanced d-dimensional kd-tree for n points in can be
performed in O(log n) time.
[2]
According to [de Berg et al. 00], the term k-d tree was meant as a template to be specialized like 2-d
tree, 3-d tree, etc. Today, it is customary to specialize it like 2D kd-tree, etc.
2.5. Range Trees
Range trees are defined for arbitrary dimension d and support exactly the same windowing query as the
kd-tree. A range tree can answer the corresponding query more efficiently. On the negative side, a range
tree requires more space than the kd-tree.
With the help of a d-dimensional range tree, one can efficiently answer (point/axis-parallel box) windowing
queries as follows:
Input: a set of points S in ;
Query: an axis-parallel d-dimensional box B;
Output: all points p ∈ S with p ∈ B.
The range tree is defined similar to the multi-level segment tree (see Section 2.3). The main difference is
that we do not need to represent segments in the tree. Let us first consider a simple balanced one-
dimensional search tree for a set S of points on the x-axis. As mentioned earlier, each node v represents
a uniquely determined interval v.I of points in S. In turn, for a query interval I, the points of S inside I are
covered by a minimal set of intervals v.I; see Figure 2.7. The construction of the one-dimensional search
tree was already discussed in Section 2.2.