KD树

本文深入解析了一维和二维KD树的数据结构与算法实现,通过代码示例详细介绍了如何构建和查询KD树,特别关注于高维空间中点的高效搜索。一维KD树作为理解基础,为二维KD树的复杂实现提供了直观的入门指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一维KD树(貌似用不上,但是理解一维有助于理解二维)

Code

#include <bits/stdc++.h>
#define MAX 500005
#define NIL -1
using namespace std;
#define for(x, y) for(int i=x; i<y; i++)
struct Node1D { int loc, l, r; };
Node1D T[MAX];
int n, np;
int P[MAX];

int make1D(int l, int r)
{
	if (l >= r)
		return NIL;
	int mid = (l + r) / 2;
	int t = np++;
	T[t].loc = mid;
	T[t].l = make1D(l, mid);
	T[t].r = make1D(mid + 1, r);

	return t;
}

void find1D(int p, int sx, int tx)
{
	int x = P[T[p].loc];
	if (x >= sx && x <= tx)
		cout << x << " ";
	if (T[p].l != NIL && x >= sx)
		find1D(T[p].l, sx, tx);
	if (T[p].r != NIL && x <= tx)
		find1D(T[p].r, sx, tx);
}

int main()
{
	cin >> n;
	for (0, n)
		cin >> P[i];
	sort(P, P + n);
	make1D(0, n);
	int q;
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		find1D(0, x, y);
		cout << endl;
	}
	return 0;
}

Data

10
1 2 2 2 3 4 4 5 6 7
5
2 3
2 4
1 5
2 6
4 2

二维KD树(代板子过的,现在理解还很困难)

Aizu - DSL_2_C

#include <bits/stdc++.h>
#define MAX 500005
#define NIL -1
using namespace std;
#define sd(n) scanf("%d", &n)
#define pd(n) printf("%d\n", n)
#define ps(s) printf(s)
#define for(x, y) for(int i=x; i<y; i++)
typedef pair<int, pair<int, int> > Pair;

struct Node2D { int loc, l, r; };
Node2D T[MAX];
int n, np;
Pair P[MAX];
vector<Pair> ans;

bool cmp1(const Pair &p1, const Pair &p2)
{
	return p1.second.first < p2.second.first;
}

bool cmp2(const Pair &p1, const Pair &p2)
{
	return p1.second.second < p2.second.second;
}

bool cmp3(const Pair &p1, const Pair &p2)
{
	return p1.first < p2.first;
}

void init()
{
	np = 0;
}

void print()
{
	for (0, ans.size())
		pd(ans[i].first);
}

int make2D(int l, int r, int d)
{
	if (l >= r)
		return NIL;
	int mid = (l + r) / 2;
	int t = np++;
	if (d % 2 == 0) //x轴为基准
		sort(P + l, P + r, cmp1);
	else  //y轴为基准
		sort(P + l, P + r, cmp2);
	T[t].loc = mid;
	T[t].l = make2D(l, mid, d + 1);
	T[t].r = make2D(mid + 1, r, d + 1);
	return t;
}

void find2D(int p, int sx, int tx, int sy, int ty, int d)
{
	int x = P[T[p].loc].second.first;
	int y = P[T[p].loc].second.second;
	if (sx <= x && x <= tx && sy <= y && y <= ty)
		ans.push_back(P[T[p].loc]);
	if (d % 2 == 0) //x轴为基准
	{
		if (T[p].l != NIL && sx <= x)
			find2D(T[p].l, sx, tx, sy, ty, d + 1);
		if (T[p].r != NIL && x <= tx)
			find2D(T[p].r, sx, tx, sy, ty, d + 1);
	}
	else //y轴为基准
	{
		if (T[p].l != NIL && sy <= y)
			find2D(T[p].l, sx, tx, sy, ty, d + 1);
		if (T[p].r != NIL && y <= ty)
			find2D(T[p].r, sx, tx, sy, ty, d + 1);
	}
}

int main()
{
	sd(n);
	init();
	for (0, n) {
		sd(P[i].second.first);
		sd(P[i].second.second);
		P[i].first = i;
	}
		
	int root = make2D(0, n, 0);
	int q;
	sd(q);
	while (q--) {
		int sx, tx, sy, ty;
		sd(sx); sd(tx); sd(sy); sd(ty);
		ans.clear();
		find2D(root, sx, tx, sy, ty, 0);
		sort(ans.begin(), ans.end(), cmp3);
		print();
		ps("\n");
	}
	return 0;
}

 

### KD的概念 KD(K-d Tree),即K维,是一种用于组织点在k维空间中的状数据结构,主要用于多维空间中的快速搜索操作,如最近邻搜索和范围搜索。每个节点代表一个k维空间中的点,并且每个节点将空间划分为两个子空间,使得的左子中的所有点位于该节点的分割超平面的一侧,而右子中的点位于另一侧。这种分割方式确保了每个节点的子节点所在的区域是父节点区域的一个子集。 ### KD的原理 构建KD的过程是从给定的数据集中选择一个维度,并根据该维度上的中位数来分割数据集。这样做的目的是为了保持的平衡。接着,递归地对分割后的两个子集进行同样的操作,直到所有的数据都被插入到中。选择维度的方式可以是循环选择不同的维度,也可以基于某种启发式方法来决定最优分割维度。 在查询时,KD允许快速定位到接近查询点的数据点。当执行最近邻搜索时,从根节点开始,根据查询点在当前节点维度上的值决定进入左子还是右子。一旦到达叶子节点,就回溯并检查其他分支是否可能包含更近的邻居。这个过程涉及到计算查询点到超平面的距离,并判断是否有必要进入另一个分支进行搜索。 ### KD的实现 实现KD的关键在于如何有效地构建以及如何高效地执行搜索操作。以下是构建KD的基本步骤: 1. 选择一个维度作为分割维度。 2. 找到该维度上的中位数点。 3. 将中位数点作为当前节点,并将剩余的点分为两组,一组位于中位数点的一侧,另一组位于另一侧。 4. 对左右两组分别递归执行上述步骤。 以下是一个简单的Python伪代码示例,展示如何构建一个KD: ```python class Node: def __init__(self, point, left=None, right=None): self.point = point self.left = left self.right = right def build_kd_tree(points, depth=0): if not points: return None # 选择维度 k = len(points[0]) axis = depth % k # 按照当前维度排序并找到中位数 points.sort(key=lambda x: x[axis]) median = len(points) // 2 # 构建节点 return Node( point=points[median], left=build_kd_tree(points[:median], depth + 1), right=build_kd_tree(points[median + 1:], depth + 1) ) ``` 对于搜索算法,特别是最近邻搜索,实现起来更为复杂,因为需要考虑回溯过程以确保找到全局最近的邻居。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值