#include<new>
#include<stdlib.h>
#include<cstddef>
#include<utility>
template <class T1, class T2>
inline void construct(T1* p, const T2& value)
{
new (p)T1(value);
}
template <class T>
inline void destroy(T * pointer)
{
pointer->~T();
}
template <class T>
inline void destroy(T * first, T * last)
{
for (; first < last; ++first)
{
destroy(&*first);
}
}
template <class T>
class simple_alloc
{
private:
static void * allocate_malloc(size_t n)
{
void * result = malloc(n);
return result;
}
static void deallocate(void * p, size_t)
{
free(p);
}
public:
static T * allocate(size_t n)
{
if (n == 0)
return 0;
else
{
return (T*)allocate_malloc(n * sizeof(T));
}
}
static T * allocate(void)
{
return (T*)allocate_malloc(sizeof(T));
}
static void deallocate(T *p, size_t n)
{
if (0 != n)
{
dellocate_free(p, n*sizeof(T));
}
}
static void deallocate(T *p)
{
dellocate_free(p, sizeof(T));
}
};
typedef bool rb_tree_color_type;
const rb_tree_color_type rb_tree_red = false;
const rb_tree_color_type rb_tree_black = true;
//双层节点结构 base上为节点的父指针,左右节点指针和颜色
struct rb_tree_node_base
{
typedef rb_tree_color_type color_type;
typedef rb_tree_node_base * base_ptr;
color_type color;
base_ptr parent;
base_ptr left;
base_ptr right;
static base_ptr minimum(base_ptr x)
{
while (x->left != 0)
x = x->left;
return x;
}
static base_ptr maximum(base_ptr x)
{
while (x->right != 0)
x = x->right;
return x;
}
};
//第二层结构仅为 一个link_type指针和节点值
template <class Value>
struct rb_tree_node :public rb_tree_node_base
{
typedef rb_tree_node<Value> * link_type;
Value value_field;
};
//base迭代器 只有前进和后退操作
struct rb_tree_base_iterator
{
typedef rb_tree_node_base::base_ptr base_ptr;
typedef ptrdiff_t difference_type;
base_ptr node;//让迭代器和容器RB_TREE有一个连接
void increment()//利用二叉搜索树的性质去找到比当前节点值大的节点中最小的那一个
{
if (node->right != 0)
{
node = node->right;
while (node->left != 0)
node = node->left;
}
else
{
base_ptr y = node->parent;
while (node == y->right)
{
node = y;
y = y->parent;
}
if (node->right != y)
node = y;
}
}
void decrement()//利用二叉搜索树的性质去找到比当前节点值小的节点中最大的那一个
{
if (node->color == rb_tree_red && node->parent->parent == node)//这里指的是该节点为当前树的最大节点时
node = node->right;
else if(node->left != 0)
{
base_ptr y = node->left;
while (y->right != 0)
y = y->right;
node = y;
}
else
{
base_ptr y = node->parent;
while (node == y->left)
{
node = y;
y = y->parent;
}
node = y;
}
}
};
//RB_TREE 的迭代器
template<class Value , class Ref , class Ptr>
struct rb_tree_iterator : public rb_tree_base_iterator
{
typedef Value balue_type;
typedef Ref reference;
typedef Ptr pointer;
typedef rb_tree_iterator<Value, Value&, Value*> iterator;
typedef rb_tree_iterator<Value, const Value&, const Value*> const_iterator;
typedef rb_tree_iterator<Value, Value&, Value*> self;
typedef rb_tree_node<Value>* link_type;
rb_tree_iterator(){}
rb_tree_iterator(link_type x)
{
node = x;//这里就是base迭代器中的node
}
rb_tree_iterator(const iterator& it)
{
node = it.node;
}
reference operator*() const
{
return link_type(node)->value_field;
}
pointer operator->() const
{
return &(operator*());
}
self& operator++()
{
increment();
return *this;
}
self operator++(int)
{
self tmp = *this;
increment();
return tmp;
}
/* self& operaotr--()
{
decrement();
return *this;
}*/
self& operator--(int)
{
self tmp = *this;
decrement();
return tmp;
}
};
inline void rb_tree_rotate_left( rb_tree_node_base * x , rb_tree_node_base*& root)
{
rb_tree_node_base * y = x->right;
x->right = y->left;
if(y->left !=0 )
{
y->left->parent = x;
}
y->parent = x->parent;
if(x==root)
{
root = y;
}
else if(x == x->parent->left)
{
x->parent->left = y;
}
else
{
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
inline void rb_tree_rotate_right(rb_tree_node_base * x , rb_tree_node_base*& root)
{
rb_tree_node_base * y = x->left;
x->left = y->right;
if(y->right != 0)
y->right->parent = x;
y->parent = x->parent;
if(x==root)
root = y;
else if(x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
}
inline void rb_tree_rebalance(rb_tree_node_base * x , rb_tree_node_base*& root)//自上而下的保持树的平衡,或改变颜色或进行旋转
{
x->color = rb_tree_red;
while(x != root && x->parent->color == rb_tree_red )
{
if(x->parent == x->parent->parent->left )
{
rb_tree_node_base* y = x->parent->parent->right;
if(y && y->color == rb_tree_red)
{
x->parent->color = rb_tree_black;
y->color = rb_tree_black;
x->parent->parent->color = rb_tree_red;
x = x->parent->parent;
}
else
{
if(x == x->parent->right)
{
x = x->parent;
rb_tree_rotate_left(x,root); //左旋转
}
x->parent->color = rb_tree_black;
x->parent->parent->color = rb_tree_red;
rb_tree_rotate_right(x->parent->parent, root);
}
}
else
{
if(x== x->parent->left)
{
x = x->parent;
rb_tree_rotate_right(x,root);
}
x->parent->color = rb_tree_black;
x->parent->parent->color = rb_tree_red;
rb_tree_rotate_left(x->parent->parent,root);
}
}
root->color = rb_tree_black;
}
template<class Key , class Value , class KeyOfValue , class Compare>
class rb_tree
{
protected:
typedef void* void_pointer;
typedef rb_tree_node_base* base_ptr;
typedef rb_tree_node<Value> rb_tree_node;
typedef simple_alloc<rb_tree_node> rb_tree_node_allocator;
typedef rb_tree_color_type color_type;
public:
typedef Key key_type;
typedef Value value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef rb_tree_node* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:
link_type get_node()
{
return rb_tree_node_allocator::allocate();
}
void put_node(link_type p)
{
rb_tree_node_allocator::deallocate(p);
}
link_type create_node(const value_type& x)
{
link_type tmp = get_node();
construct(&tmp->value_field, x);
return tmp;
}
link_type clone_node(link_type x)
{
link_type tmp = create_node(x->value_field);
tmp->color = x->color;
tmp->left = 0;
tmp->right = 0;
return tmp;
}
void destroy_node(link_type p)
{
destroy(&p->value_field);
put_node(p);
}
protected:
size_type node_count_;
link_type header_;//类似于头指针 header_ 和root 互为父节点
Compare key_compare_;
link_type& root() const
{
return (link_type&)header_->parent;
}
link_type& leftmost() const
{
return (link_type&)header_->left;
}
link_type& rightmost() const
{
return (link_type&)header_->right;
}
static link_type& left(link_type x)
{
return (link_type&)(x->left);
}
static link_type& right(link_type x)
{
return (link_type&)(x->right);
}
static link_type& parent(link_type x)
{
return (link_type&)(x->parent);
}
static reference value(link_type x)
{
return x->value_field;
}
static Key& key(link_type x)
{
return KeyOfValue()(value(x));
}
static color_type& color(link_type x)
{
return (color_type&)(x->color);
}
static link_type& left(base_ptr x)
{
return (link_type&)(x->left);
}
static link_type& right(base_ptr x)
{
return (link_type&)(x->right);
}
static link_type& parent(base_ptr x)
{
return (link_type&)(x->parent);
}
static reference value(base_ptr x)
{
return ((link_type)x)->value_field;
}
static color_type& color(base_ptr x)
{
return (color_type&)(x->color);
}
static link_type minimum(link_type x)
{
return (link_type)rb_tree_node_base::minimum(x);
}
static link_type maximum(link_type x)
{
return (link_type)rb_tree_node_base::maximum(x);
}
public:
typedef rb_tree_iterator<value_type, reference, pointer> iterator;
private:
void init()
{
header_ = get_node();
color(header_) = rb_tree_red;
root() = 0;
leftmost() = header_;
rightmost() = header_;
}
public:
rb_tree(const Compare& comp = Compare()) :node_count_(0), key_compare_(comp)
{
init();
}
~rb_tree()
{
clear();
put_node(header_);
}
// rb_tree<Key, Value, KeyOfValue, Compare >& operaotr = ( rb_tree<Key, Value, KeyOfValue, Compare>& x);
public:
iterator begin()
{
return leftmost;//起点为最小值
}
iterator end()
{
return header_;//终点为头节点指向的根
}
bool empty() const
{
return node_count_ == 0;
}
size_type size() const
{
return node_count_;
}
size_type max_size() const
{
return size_type(-1);
}
public:
void __erase(link_type x)
{
while (x != 0)
{
__erase(right(x));
link_type y = left(x);
destroy_node(x);
x = y;
}
}
void erase(iterator position);
size_type erase(const key_type& x);
void erase(iterator first, iterator last);
void erase(const key_type* first, const key_type* last);
void clear() {
if (node_count_ != 0)
{
__erase(root());
leftmost() = header_;
root() = 0;
rightmost() = header_;
node_count_ = 0;
}
}
typename rb_tree<Key, Value, KeyOfValue, Compare>::iterator insert(base_ptr x_, base_ptr y_, const Value& v)
{
link_type x = (link_type)x_;
link_type y = (link_type)y_;
link_type z;
if (y == header_ || x!=0 ||key_comp(KeyOfValue()(v),key(y)))
{
z = create_node(v);
left(y) = z;
if (y == header_)
{
root() = z;
rightmost() = z;
}
else if (y == leftmost())
{
leftmost() = z;
}
}
else
{
z = create_node(v);
right(y) = z;
if (y == rightmost())
rightmost() = z;
}
parent(z) = y;
left(z) = 0;
right(z) = 0;
rb_tree_rebalance(z, header_->parent);
++node_count_;
return iterator(z);
}
typename rb_tree<Key, Value, KeyOfValue, Compare>::iterator insert_equal(const Value& v)
{
link_type y = header_;
link_type x = root();
while (x != 0)
{
y = x;
if (key_compare_(KeyOfValue()(v), key(x)))//这里要求用户设定keyofcompare
{
x = left(x);
}
else
{
x = right(x);
}
}
return insert(x, y, v);
}
pair<typename rb_tree<Key, Value, KeyOfValue, Compare>::iterator, bool>
rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_unique(const Value& v)
{
link_type y = header_;
link_type x = root();
bool comp = true;
while (x != 0)
{
y = x;
comp = key_compare_(KeyOfValue()(v), key(x));
if (comp)
x = left(x);
else
x = right(x);
}
iterator j = iterator(y);
if (comp)
if (j == begin())
return pair<iteraotr, bool>(insert(x, y, v), true);
else
--j;
if (key_comp(key(j.node), KeyOfValue()(v)))
return pair<iterator, bool>(insert(x, y, v), true);
return pair<iterator,bool>;
}
};
rb_tree
最新推荐文章于 2023-07-24 21:55:33 发布