#include <iostream>
template<
class __Key
>class lessCmptor
{
using is_transparent = std::true_type;
public:
bool operator()(const __Key &k_fir,const __Key &k_sec)const
{
return k_fir < k_sec;
}
bool operator()(__Key &k_fir,const __Key &k_sec)const
{
return k_fir < k_sec;
}
bool operator()(const __Key &k_fir,__Key &k_sec)const
{
return k_fir < k_sec;
}
bool operator()(__Key &&k_fir,__Key&& k_sec)const
{
return k_fir < k_sec;
}
bool operator()(const __Key &k_fir,__Key&& k_sec)const
{
return k_fir < k_sec;
}
bool operator()(__Key &&k_fir,const __Key& k_sec)const
{
return k_fir < k_sec;
}
};
template<
class Key,
class Compare = lessCmptor<Key>
>class Set
{
struct Node
{
struct Node *_left,
*_right;
Key _k;
Node(Key k,struct Node *left = nullptr,struct Node *right = nullptr)
:_k(k)
,_left(left)
,_right(right)
{}
explicit Node(Node&& other)
:_k(other.k)
,_left(*(other._left))
,_right(*(other._right))
{
other._left = nullptr;
other._right = nullptr;
}
explicit Node(const Node &other)
:_k(other._k)
,_left(!(nullptr == other._left)?new Node(*(other._left))
:static_cast<Node *>(nullptr))
,_right(!(nullptr == other._right)?new Node(*(other._right))
:static_cast<Node *>(nullptr))
{}
};
public:
explicit Set();
explicit Set(const Set& other);
explicit Set(Set&& other);
~Set();
Set& operator=(const Set& other);
Set& operator=(Set &&other) noexcept;
bool empty() const noexcept;
size_t size()const noexcept;
std::pair<Node *,bool> insert(const Key& k);
std::pair<Node *,bool> insert(Key&& k);
bool erase(const Key& k);
bool erase(Node *p);
Node *find(const Key& k)const;
Node *lower_bound(const Key& k)const;
void iterate(void(*visit)(const Key &k));
private:
Node *_root;
size_t _size;
void _iterate(Node *root,void(*visit)(const Key &k));
void dtor(Node *e) noexcept;
Node *&fnd(const Node *&root,const Key& k)const;
Node *ers(Node *root,const Key& k);
Node *succ(const Node *root)const;
void swap(Key *k__x,Key *k__y)noexcept;
};
template<
class Key,
class Compare
> Set<Key,Compare>::Set()
: _root(nullptr)
, _size(0)
{}
template<
class Key,
class Compare
> Set<Key,Compare>::~Set()
{
dtor(_root);_root = nullptr;
}
template<
class Key,
class Compare
> Set<Key,Compare>::Set(const Set<Key,Compare>& other)
:_root(!(nullptr == other._root)?new Node(* (other._root))
:static_cast<Node *>(nullptr))
,_size(other._size)
{}
template<
class Key,
class Compare
> Set<Key,Compare>::Set(Set<Key,Compare>&& other)
:_root(other._root)
,_size(other._size)
{
other._root = nullptr;
other._size = 0;
}
template<
class Key,
class Compare
> Set<Key,Compare>& Set<Key,Compare>::operator=(const Set& other)
{
if(!(nullptr == this->_root))
this->_root = nullptr == other._root?nullptr
:new Node(*(other._root));
this->_size = other._size;
return (*this);
}
template<
class Key,
class Compare
> void Set<Key,Compare>::dtor(Node *e)noexcept
{
if(!(nullptr == e))
{
dtor(e->_left);dtor(e->_right);
delete e;
}
}
template<
class Key,
class Compare
> void Set<Key,Compare>::_iterate(Node *root,void(*visit)(const Key &k))
{
if(!(nullptr == root))
{
_iterate(root->_left,visit);
visit(root->_k);
_iterate(root->_right,visit);
}
}
template<
class Key,
class Compare
> void Set<Key,Compare>::iterate(void(*visit)(const Key &k))
{
_iterate(_root,visit);
}
template<
class Key,
class Compare
> bool Set<Key,Compare>::empty() const noexcept
{
return static_cast<bool>(this->_size() == 0);
}
template<
class Key,
class Compare
> size_t Set<Key,Compare>::size()const noexcept
{
return this->_size;
}
template<
class Key,
class Compare
> struct Set<Key,Compare>::Node *Set<Key,Compare>::find(const Key& k)const
{
return fnd(_root,k);
}
template<
class Key,
class Compare
> struct Set<Key,Compare>::Node *&Set<Key,Compare>::fnd(const Node *&root,const Key& k)const
{
if(nullptr == root)return root;
if(Compare()(k,root->_k))return fnd(root->_left,k);
else if(!Compare()(k,root->_k)
&& !Compare()(root->_k,k))
return const_cast<Node *&>(root);
else
return fnd(root->_right,k);
}
template<
class Key,
class Compare
> std::pair<typename Set<Key,Compare>::Node *,bool> Set<Key,Compare>::insert(const Key& k)
{
Node *&res = fnd(_root,k);
return std::make_pair(!(nullptr == res)?res:
(res = new Node(k))
,(bool)(nullptr == res));
}
template<
class Key,
class Compare
> std::pair<typename Set<Key,Compare>::Node *,bool> Set<Key,Compare>::insert(Key&& k)
{
Node *res = find(k);
return std::make_pair(!(nullptr == res)?res:
new Node(std::move(k))
,(bool)(nullptr == res));
}
template<
class Key,
class Compare
> bool Set<Key,Compare>::erase(const Key& k)
{
return (nullptr == ers(this->_root,k));
}
template<
class Key,
class Compare
> bool Set<Key,Compare>::erase(Node *p)
{
if(nullptr == p)return 0;
return static_cast<size_t>(nullptr == ers(_root,p->_k));
}
template<
class Key,
class Compare
> typename Set<Key,Compare>::Node * Set<Key,Compare>::ers(Node *root,const Key& k)
{
if(nullptr == root)return nullptr;
if(Compare()(k,root->_k))
root->_left = ers(root->_left,k);
else if(!Compare()(root->_k,k)
&&!Compare()(k,root->_k))
{
if(nullptr == root->_left)
{
Node *t = root->_right;
delete root;root = nullptr;
return t;
}
if(nullptr == root->_right)
{
Node *t = root->_left;
delete root;root = nullptr;
return t;
}
Node *t = succ(root);
swap(&(root->_k),&(t->_k));
ers(root,k);
}
else
root->_right = ers(root->_right,k);
}
template<
class Key,
class Compare
> void Set<Key,Compare>::swap(Key *k__x,Key *k__y)noexcept
{
Key k__t = *k__x;
*k__x = *k__y;
*k__y = k__t;
}
template<
class Key,
class Compare
> struct Set<Key,Compare>::Node * Set<Key,Compare>::succ(const Node *root)const
{
if(nullptr == root->_right)return const_cast<Node *>(root);
Node *res = root->_right->_left;
while(!(nullptr == res)
&&!(nullptr == res->_left))
res = res->_left;
return res;
}
template<
class Key,
class Compare
> struct Set<Key,Compare>::Node * Set<Key,Compare>::lower_bound(const Key& k)const
{
Node *res = _root->_right;
while(!(nullptr == res)
&& !(nullptr == res->_left))
res = res->_left;
return res;
}
二叉搜索树 类模板(BST | Template)
最新推荐文章于 2024-09-11 22:48:33 发布