浅谈替罪羊树与K-D Tree
Ruiqun2009
·
·
个人记录
K-D Tree(K-Dimensional Tree),是一种高效处理 k 维数据的数据结构。
替罪羊树
其实替罪羊树是 K-D Tree 在 k=1 时的特殊情况。因为 K-D Tree 不支持旋转,也不能像 FHQ-Treap 那样使用堆权来保持平衡,所以只能使用暴力重构的方式。
步骤:
- 将需要重构的 K-D Tree 变成一个序列(使用中序遍历)
- 建一棵完全平衡的 K-D Tree(将中位数变到中间,对两边递归)
然后没了。
K-D Tree
其实 K-D Tree 的各种操作与替罪羊树较为相似。在 OI 中一般 k=2。
这里先放一下前置声明:
template <typename _Tp, uint32_t k, typename _Comp = std::less<_Tp> >
class kd_tree {
struct node;
_Comp _M_comp;
template <uint32_t d>
inline bool cmp(node* lhs, node* rhs);
public:
node* root;
void build(_Tp** arr, size_t l, size_t r);
inline void insert(size_t siz, _Tp* dat, const _Tp& v, const _Tp& sm) { root->insert(new node(siz, dat, v, sm, nullptr, nullptr); }
inline void empty() { delete root; }
}
template <typename _Tp, uint32_t k, typename _Comp>
class kd_tree<_Tp, k, _Comp>::node {
public:
size_t _M_size;
_Tp _M_data[k];
_Tp _M_value, _M_sum;
node *_M_left, *_M_right;
inline node() = default;
inline node(size_t siz, _Tp* dat, const _Tp& val, const _Tp& sm, node* l, node* r) : _M_size(siz), _M_value(val), _M_sum(sm), _M_left(l), _M_right(r) { memcpy(_M_data, dat, k * sizeof(_Tp)); }
template <typename _Func>
inline void pushup(_Func func) {
_M_size = 1;
_M_sum = _M_value;
if (left) {
_M_size += left->_M_size;
_M_sum += left->_M_sum;
for (uint32_t i = 0; i < k; i++) _M_data[i] = func(_M_data[i], left->_M_data[i]);
}
if (right) {
_M_size += right->_M_size;
_M_sum += right->_M_sum;
for (uint32_t i = 0; i < k; i++) _M_data[i] = func(_M_data[i], right->_M_data[i]);
}
}
private:
inline void _M_insert(node* x);
inline void _M_maintain();
};
template <typename _Tp, uint32_t k, typename _Comp>
inline bool kd_tree<_Tp, k, _Comp>::cmp(node* lhs, node* rhs) { return _M_comp(lhs->_M_data[d], rhs->_M_data[d]); }
build 函数的运作方式是:
- 计算当前有多少个元素,如果 \leq 1 则返回;
- 按照当前的维度的元素作为比较标准,得到中位数 ^{\lbrack 1\rbrack};
- 递归建左右子树。
```cpp
namespace std {
template<typename _RandomAccessIterator, typename _Compare>
inline void
nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
_RandomAccessIterator __last, _Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
typename iterator_traits<_RandomAccessIterator>::value_type,
typename iterator_traits<_RandomAccessIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __nth);
__glibcxx_requires_valid_range(__nth, __last);
__glibcxx_requires_irreflexive_pred(__first, __last, __comp);
if (__first == __last || __nth == __last) return;
std::__introselect(__first, __nth, __last,
std::__lg(__last - __first) * 2,
__gnu_cxx::__ops::__iter_comp_iter(__comp));
}
template<typename _RandomAccessIterator, typename _Size, typename _Compare>
void
__introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
_RandomAccessIterator __last, _Size __depth_limit,
_Compare __comp)
{
while (__last - __first > 3) {
if (__depth_limit == 0) {
std::__heap_select(__first, __nth + 1, __last, __comp);
// Place the nth largest element in its final position.
std::iter_swap(__first, __nth);
return;
}
--__depth_limit;
_RandomAccessIterator __cut = std::__unguarded_partition_pivot(__first, __last, __comp);
if (__cut <= __nth) __first = __cut;
else __last = __cut;
}
std::__insertion_sort(__first, __last, __comp);
}
template<typename _RandomAccessIterator, typename _Compare>
_RandomAccessIterator
__unguarded_partition(_RandomAccessIterator __first,
_RandomAccessIterator __last,
_RandomAccessIterator __pivot, _Compare __comp)
{
while (true) {
while (__comp(__first, __pivot)) ++__first;
--__last;
while (__comp(__pivot, __last)) --__last;
if (!(__first < __last)) return __first;
std::iter_swap(__first, __last);
++__first;
}
}
template<typename _Iterator, typename _Compare>
void
__move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b,
_Iterator __c, _Compare __comp)
{
if (__comp(__a, __b)) {
if (__comp(__b, __c)) std::iter_swap(__result, __b);
else if (__comp(__a, __c)) std::iter_swap(__result, __c);
else std::iter_swap(__result, __a);
}
else if (__comp(__a, __c)) std::iter_swap(__result, __a);
else if (__comp(__b, __c)) std::iter_swap(__result, __c);
else std::iter_swap(__result, __b);
}
template<typename _RandomAccessIterator, typename _Compare>
inline _RandomAccessIterator
__unguarded_partition_pivot(_RandomAccessIterator __first,
_RandomAccessIterator __last, _Compare __comp)
{
_RandomAccessIterator __mid = __first + (__last - __first) / 2;
std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, __comp);
return std::__unguarded_partition(__first + 1, __last, __first, __comp);
}
template<typename _RandomAccessIterator, typename _Compare>
void
__heap_select(_RandomAccessIterator __first,
_RandomAccessIterator __middle,
_RandomAccessIterator __last, _Compare __comp)
{
std::__make_heap(__first, __middle, __comp);
for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) if (__comp(__i, __first)) std::__pop_heap(__first, __middle, __i, __comp);
}
template<typename _RandomAccessIterator, typename _Compare>
void
__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp)
{
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
if (__last - __first < 2) return;
const _DistanceType __len = __last - __first;
_DistanceType __parent = (__len - 2) / 2;
while (true) {
_ValueType __value = std::move(*(__first + __parent));
std::__adjust_heap(__first, __parent, __len, std::move(__value), __comp);
if (__parent == 0) return;
__parent--;
}
}
template<typename _RandomAccessIterator, typename _Distance, typename _Tp, typename _Compare>
void __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
_Distance __len, _Tp __value, _Compare __comp)
{
const _Distance __topIndex = __holeIndex;
_Distance __secondChild = __holeIndex;
while (__secondChild < (__len - 1) / 2) {
__secondChild = 2 * (__secondChild + 1);
if (__comp(__first + __secondChild, __first + (__secondChild - 1))) __secondChild--;
*(__first + __holeIndex) = std::move(*(__first + __secondChild));
__holeIndex = __secondChild;
}
if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2) {
__secondChild = 2 * (__secondChild + 1);
*(__first + __holeIndex) = std::move(*(__first + (__secondChild - 1)));
__holeIndex = __secondChild - 1;
}
__decltype(__gnu_cxx::__ops::__iter_comp_val(std::move(__comp))) __cmp(std::move(__comp));
std::__push_heap(__first, __holeIndex, __topIndex, std::move(__value), __cmp);
}
template<typename _RandomAccessIterator, typename _Compare>
inline void
__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
_RandomAccessIterator __result, _Compare& __comp)
{
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
_ValueType __value = std::move(*__result);
*__result = std::move(*__first);
std::__adjust_heap(__first, _DistanceType(0), _DistanceType(__last - __first), std::move(__value), __comp);
}
}
```
其在 `__depth_limit == 0` 的时候会转变为堆排序,从而时间复杂度是 $\Theta(n\log n)$ 的。
这是示意图:

代码先不放了。
上述代码中将 $k=1$ 即得替罪羊树(`value` 可以随意)。