1 #ifndef dsa_SplayTree_h__
2 #define dsa_SplayTree_h__
7 #include "../math/utility.h"
36 template<
class Key,
class Value>
47 Node(Key
const& key, Value
const& value):
48 key_(key), keyOffset_(0), value_(value) {
57 keyOffset_ = keyOffset_ + delta;
59 void syncDown()
const {
60 for (
size_t i = 0; i < 2; i++) {
61 if (child_[i] == NULL)
continue;
62 child_[i]->keyOffset(keyOffset_);
64 ((Node*)
this)->keyOffset_ = Key(0);
67 ((Node*)
this)->size_ = 1;
68 for (
size_t i = 0; i < 2; i++) {
69 if (child_[i] == NULL)
continue;
70 ((Node*)
this)->size_ += child_[i]->size_;
78 void connect(Node
const* parent,
size_t left_right, Node
const* child)
const {
79 Node* p = (Node*)parent;
80 Node* c = (Node*)child;
81 if (p != NULL) p->child_[left_right] = c;
82 if (c != NULL) c->parent_ = p;
86 Node
const* splay(Node
const* node)
const {
87 if (node != NULL && node->parent_ != NULL) {
88 for (
const Node *g_grand, *grand, *parent, *child = node; ; ) {
89 g_grand = (grand = parent = child->parent_)->parent_;
90 size_t pc = (parent->child_[0] == child ? 0 : 1);
91 connect(parent, pc, child->child_[!pc]);
92 connect(child , !pc, parent);
93 if (g_grand != NULL) {
94 g_grand = (grand = g_grand)->parent_;
95 size_t gp = (grand->child_[0] == parent ? 0 : 1);
96 Node
const* who = (pc == gp ? parent : child);
97 connect(grand, gp, who->child_[!gp]);
98 connect(who , !gp, grand);
103 if (g_grand == NULL) {
104 connect(NULL, 0, child);
107 connect(g_grand, (g_grand->child_[0] == grand ? 0 : 1), child);
110 return (((
SplayTree*)
this)->root_ = (Node*)node);
113 void clear(Node* node) {
114 if (node == NULL) return ;
115 clear(node->child_[0]);
116 clear(node->child_[1]);
120 Node* dup(Node* node2) {
121 if (node2 == NULL)
return NULL;
123 Node* node =
new Node(node2->key_, node2->value_);
124 connect(node, 0, dup(node2->child_[0]));
125 connect(node, 1, dup(node2->child_[1]));
130 Node
const* findKey(Node
const* node, Key
const& key)
const {
131 Node
const* ret = node;
132 while (node != NULL) {
135 if (!(key < node->key_)) {
136 if (!(node->key_< key))
break;
137 node = node->child_[1];
140 node = node->child_[0];
145 Node
const* findMinMax(Node
const* node,
bool minimum)
const {
146 Node
const* ret = node;
147 for (
int i = minimum ? 0 : 1; node != NULL; node = node->child_[i]) {
153 Node
const* findOrder(Node
const* node,
size_t order)
const {
154 Node
const* ret = node;
155 while (node != NULL) {
158 size_t ord = 1 + (node->child_[0] == NULL ? 0 : node->child_[0]->size_);
159 if (ord == order)
return ret;
160 else if(ord < order){ node = node->child_[1]; order -= ord; }
161 else { node = node->child_[0]; }
166 void split(Node* root, Node** left, Node** right) {
167 if (root == NULL) { *left = NULL; *right = NULL; return ; }
170 *right = root->child_[1];
171 if (*right != NULL) {
172 (*left )->child_[1] = NULL;
173 (*right)->parent_ = NULL;
177 Node* merge(Node* left, Node* right) {
178 if (left == NULL)
return right;
179 if (right == NULL)
return left ;
181 connect(left, 1, right);
193 typedef std::pair<Key const&, Value&> Entry;
197 void reset(Node* node) {
200 entry_ = (node == NULL ? NULL :
new Entry(node->key_, node->value_));
205 Element(Node* node): entry_(NULL), node_(NULL) {
209 reset(element2.node_);
223 return (node_ == e2.node_);
258 root_(dup((Node*)(tree2.root_))) {
271 root_ = dup((Node*)(tree2.root_));
280 tree2->root_ = root_;
290 splay(findKey(root_, key));
291 if (root_ == NULL || !(root_->key_ < key))
return Element(root_);
292 if (root_->child_[1] == NULL)
return Element(NULL);
293 splay(findMinMax(root_->child_[1],
true));
303 splay(findKey(root_, key));
304 if (root_ == NULL || key < root_->key_)
return Element(root_);
305 if (root_->child_[1] == NULL)
return Element(NULL);
306 splay(findMinMax(root_->child_[1],
true));
316 splay(findKey(root_, key));
317 if (root_ == NULL || !(key < root_->key_))
return Element(root_);
318 if (root_->child_[0] == NULL)
return Element(NULL);
319 splay(findMinMax(root_->child_[0],
false));
329 splay(findKey(root_, key));
330 if (root_ == NULL || root_->key_ < key)
return Element(root_);
331 if (root_->child_[0] == NULL)
return Element(NULL);
332 splay(findMinMax(root_->child_[0],
false));
340 splay(findKey(root_, key));
341 if (root_ != NULL && !(key < root_->key_) && !(root_->key_ < key)) {
353 if (root_ == NULL || order >= root_->size_)
return Element(NULL);
354 splay(findOrder(root_, order + 1));
362 splay(findMinMax(root_,
true));
370 splay(findMinMax(root_,
false));
387 return (root_ == NULL ? 0 : root_->size_);
394 return (
size() == 0);
411 bool insert(Key
const& key, Value
const& value) {
413 root_ =
new Node(key, value);
416 Node* parent = (Node*)findKey(root_, key);
417 if (!(parent->key_ < key) && !(key < parent->key_)) {
421 Node* new_node =
new Node(key, value);
422 connect(parent, (parent->key_ < key ? 1 : 0), new_node);
436 if (root_ == NULL)
return false;
437 Node* body = (Node*)findKey(root_, key);
438 if (body->key_ < key || key < body->key_) {
443 if (body->child_[1] == NULL) {
444 ghost = body->child_[0];
445 if (ghost != NULL) ghost->syncDown();
448 ghost = (Node*)findMinMax(body->child_[1],
true);
449 connect(ghost, 0, body->child_[0]);
450 if (ghost != body->child_[1]) {
451 connect(ghost->parent_, 0, ghost->child_[1]);
452 connect(ghost, 1, body->child_[1]);
453 for (Node* a = ghost->parent_; a != ghost; a = a->parent_)
458 Node* parent = body->parent_;
459 connect(parent, parent != NULL && parent->child_[0] == body ? 0 : 1, ghost);
461 splay(ghost != NULL ? ghost : parent);
470 root_->keyOffset(delta);
480 split(root_, &root_, &(right->root_));
483 right->root_ = root_;
495 if (root_ == NULL || tree2->root_ == NULL ||
497 root_ = merge(root_, tree2->root_);
512 if (root_ == NULL || tree2->root_ == NULL ||
514 root_ = merge(root_, tree2->root_);
516 else if(tree2->
last()->first <
first()->first) {
517 root_ = merge(tree2->root_, root_);
534 return root_->value_;
568 template<
class Key,
class Value>
582 Node(Key
const& key, Value
const& value):
583 valueOffset_(0), range_(value),
584 key_(key), keyOffset_(0), value_(value) {
594 keyOffset_ = keyOffset_ + delta;
596 void valueUpdate(Value
const& delta,
bool over) {
598 value_ = delta * size_;
599 valueOffset_ = delta;
600 range_ = delta * size_;
604 value_ = value_ + delta * size_;
605 valueOffset_ = valueOffset_ + delta;
606 range_ = range_ + delta * size_;
609 void syncDown()
const {
610 for (
size_t i = 0; i < 2; i++) {
611 if (child_[i] == NULL)
continue;
612 child_[i]->keyOffset(keyOffset_);
613 child_[i]->valueUpdate(valueOffset_, same_);
615 ((Node*)
this)->keyOffset_ = Key(0);
616 ((Node*)
this)->valueOffset_ = Value(0);
617 ((Node*)
this)->same_ =
false;
619 void syncUp()
const {
620 ((Node*)
this)->size_ = 1;
621 Value* v[3] = {&(((Node*)
this)->value_), NULL, NULL};
623 for (
size_t i = 0; i < 2; i++) {
624 if (child_[i] == NULL)
continue;
625 ((Node*)
this)->size_ += child_[i]->size_;
626 v[vct++] = &(child_[i]->range_);
628 if (vct == 1) ((Node*)
this)->range_ = (*v[0]);
629 else if(vct == 2) ((Node*)
this)->range_ = (*v[0]) | (*v[1]);
630 else ((Node*)
this)->range_ = (*v[0]) | (*v[1]) | (*v[2]);
637 void connect(Node
const* parent,
size_t left_right, Node
const* child)
const {
638 Node* p = (Node*)parent;
639 Node* c = (Node*)child;
640 if (p != NULL) p->child_[left_right] = c;
641 if (c != NULL) c->parent_ = p;
645 Node
const* splay(Node
const* node)
const {
646 if (node != NULL && node->parent_ != NULL) {
647 for (
const Node *g_grand, *grand, *parent, *child = node; ; ) {
648 g_grand = (grand = parent = child->parent_)->parent_;
649 size_t pc = (parent->child_[0] == child ? 0 : 1);
650 connect(parent, pc, child->child_[!pc]);
651 connect(child , !pc, parent);
652 if (g_grand != NULL) {
653 g_grand = (grand = g_grand)->parent_;
654 size_t gp = (grand->child_[0] == parent ? 0 : 1);
655 Node
const* who = (pc == gp ? parent : child);
656 connect(grand, gp, who->child_[!gp]);
657 connect(who , !gp, grand);
662 if (g_grand == NULL) {
663 connect(NULL, 0, child);
666 connect(g_grand, (g_grand->child_[0] == grand ? 0 : 1), child);
672 void clear(Node* node) {
673 if (node == NULL) return ;
674 clear(node->child_[0]);
675 clear(node->child_[1]);
679 Node* dup(Node* node2) {
680 if (node2 == NULL)
return NULL;
682 Node* node =
new Node(node2->key_, node2->value_);
683 connect(node, 0, dup(node2->child_[0]));
684 connect(node, 1, dup(node2->child_[1]));
689 Node
const* findKey(Node
const* node, Key
const& key)
const {
690 Node
const* ret = node;
691 while (node != NULL) {
694 if (!(key < node->key_)) {
695 if (!(node->key_< key))
break;
696 node = node->child_[1];
699 node = node->child_[0];
704 Node
const* findMinMax(Node
const* node,
bool minimum)
const {
705 Node
const* ret = node;
706 for (
int i = minimum ? 0 : 1; node != NULL; node = node->child_[i]) {
712 Node
const* findOrder(Node
const* node,
size_t order)
const {
713 Node
const* ret = node;
714 while (node != NULL) {
717 size_t ord = 1 + (node->child_[0] == NULL ? 0 : node->child_[0]->size_);
718 if (ord == order)
return ret;
719 else if(ord < order){ node = node->child_[1]; order -= ord; }
720 else { node = node->child_[0]; }
725 void split(Node* root, Node** left, Node** right) {
726 if (root == NULL) { *left = NULL; *right = NULL; return ; }
729 *right = root->child_[1];
730 if (*right != NULL) {
731 (*left )->child_[1] = NULL;
732 (*right)->parent_ = NULL;
736 Node* merge(Node* left, Node* right) {
737 if (left == NULL)
return right;
738 if (right == NULL)
return left ;
740 connect(left, 1, right);
752 typedef std::pair<Key const&, Value&> Entry;
756 void reset(Node* node) {
759 entry_ = (node == NULL ? NULL :
new Entry(node->key_, node->value_));
764 Element(Node* node): entry_(NULL), node_(NULL) {
768 reset(element2.node_);
782 return (node_ == e2.node_);
817 root_(dup((Node*)(tree2.root_))) {
830 root_ = dup((Node*)(tree2.root_));
839 tree2->root_ = root_;
849 splay(findKey(root_, key));
850 if (root_ == NULL || !(root_->key_ < key))
return Element(root_);
851 if (root_->child_[1] == NULL)
return Element(NULL);
852 splay(findMinMax(root_->child_[1],
true));
862 splay(findKey(root_, key));
863 if (root_ == NULL || key < root_->key_)
return Element(root_);
864 if (root_->child_[1] == NULL)
return Element(NULL);
865 splay(findMinMax(root_->child_[1],
true));
875 splay(findKey(root_, key));
876 if (root_ == NULL || !(key < root_->key_))
return Element(root_);
877 if (root_->child_[0] == NULL)
return Element(NULL);
878 splay(findMinMax(root_->child_[0],
false));
888 splay(findKey(root_, key));
889 if (root_ == NULL || root_->key_ < key)
return Element(root_);
890 if (root_->child_[0] == NULL)
return Element(NULL);
891 splay(findMinMax(root_->child_[0],
false));
899 splay(findKey(root_, key));
900 if (root_ != NULL && !(key < root_->key_) && !(root_->key_ < key)) {
912 if (root_ == NULL || order >= root_->size_)
return Element(NULL);
913 splay(findOrder(root_, order + 1));
921 splay(findMinMax(root_,
true));
929 splay(findMinMax(root_,
false));
946 return (root_ == NULL ? 0 : root_->size_);
953 return (
size() == 0);
962 if (root_ == NULL)
return Value(0);
963 return root_->range_;
975 self->split(self->root_, &tmp, &(self->root_));
978 if (root_ != NULL && root_->child_[0] != NULL) {
979 ret = root_->child_[0]->range_;
981 self->root_ =
self->merge(tmp, self->root_);
999 bool insert(Key
const& key, Value
const& value) {
1000 if (root_ == NULL) {
1001 root_ =
new Node(key, value);
1004 Node* parent = (Node*)findKey(root_, key);
1005 if (!(parent->key_ < key) && !(key < parent->key_)) {
1009 Node* new_node =
new Node(key, value);
1010 connect(parent, (parent->key_ < key ? 1 : 0), new_node);
1024 if (root_ == NULL)
return false;
1025 Node* body = (Node*)findKey(root_, key);
1026 if (body->key_ < key || key < body->key_) {
1031 if (body->child_[1] == NULL) {
1032 ghost = body->child_[0];
1033 if (ghost != NULL) ghost->syncDown();
1036 ghost = (Node*)findMinMax(body->child_[1],
true);
1037 connect(ghost, 0, body->child_[0]);
1038 if (ghost != body->child_[1]) {
1039 connect(ghost->parent_, 0, ghost->child_[1]);
1040 connect(ghost, 1, body->child_[1]);
1041 for (Node* a = ghost->parent_; a != ghost; a = a->parent_)
1046 Node* parent = body->parent_;
1047 connect(parent, parent != NULL && parent->child_[0] == body ? 0 : 1, ghost);
1049 splay(ghost != NULL ? ghost : parent);
1057 if (root_ != NULL) {
1058 root_->keyOffset(delta);
1066 if (root_ != NULL) {
1067 root_->valueUpdate(delta,
false);
1076 root_->valueUpdate(value,
true);
1086 split(root_, &root_, &(right->root_));
1089 right->root_ = root_;
1101 if (root_ == NULL || tree2->root_ == NULL ||
1103 root_ = merge(root_, tree2->root_);
1104 tree2->root_ = NULL;
1118 if (root_ == NULL || tree2->root_ == NULL ||
1120 root_ = merge(root_, tree2->root_);
1122 else if(tree2->
last()->first <
first()->first) {
1123 root_ = merge(tree2->root_, root_);
1128 tree2->root_ = NULL;
1140 return root_->value_;
1151 #endif // dsa_SplayTree_h__
bool merge(SplayTree *tree2)
合併
bool mergeAfter(SplayTree *tree2)
合併
Element upperBound(Key const &key) const
找出第一個(最小的) Element且 k < 它的 Key, 並且回傳之.
bool operator==(Element const &e2) const
same as same(e2)
Element & operator=(Element const &e2)
same as copyFrom
Element order(size_t order) const
將Elements依照Key由小到大排序, 回傳第 ord 個Element (由0算起).
Element lowerBound(Key const &key) const
找出第一個(最小的) Element且 k <= 它的 Key, 並且回傳之.
Entry & operator*()
重導至std::pair<Key const&,Value&>&
size_t size() const
回傳資料個數
Entry * operator->()
重導至std::pair<Key const&,Value&>*
Element lowerBound(Key const &key) const
找出第一個(最小的) Element且 k <= 它的 Key, 並且回傳之.
bool insert(Key const &key, Value const &value)
插入一組(Key —> Value)
SplayTree & copyFrom(SplayTree const &tree2)
複製資料
Element order(size_t order) const
將Elements依照Key由小到大排序, 回傳第 ord 個Element (由0算起).
bool same(Element const &e2) const
比對兩者是否為指向同一個Entry
Element find(Key const &key) const
找出 Key= k 的Elemenet 並回傳. 找不到的話回傳 this->end()
bool same(Element const &e2) const
比對兩者是否為指向同一個Entry
Value query(Key const &first, Key const &last) const
查找
Element first() const
回傳Key最小的Element, 如果SplayTree為空, 則回傳 this->end()
void splitOut(Key const &upper_bound, SplayTree *right)
將tree2 清空, 再將所有Key > upper_bound 的Element都丟過去
Element(Element const &element2)
Element find(Key const &key) const
找出 Key= k 的Elemenet 並回傳. 找不到的話回傳 this->end()
bool operator!=(Element const &e2) const
same as !same(e2)
bool merge(SplayTree_Range *tree2)
合併
Entry * operator->()
重導至std::pair<Key const&,Value&>*
類似 stl 的 iterator ,不過這邊叫做Element
基本上跟SplayTree一樣, 不過這邊結合線段樹, 多了區間操作 (線段樹相關operator定義請見 SegmentTree )
SplayTree_Range()
constructor
bool empty() const
回傳是否為空
SplayTree_Range & operator=(SplayTree_Range const &tree2)
same as copyFrom(tree2)
Value & operator[](Key const &key)
就像stl::map::operator[]
bool mergeAfter(SplayTree_Range *tree2)
合併
Element upperBound(Key const &key) const
找出第一個(最小的) Element且 k < 它的 Key, 並且回傳之.
Element end() const
回傳一個指向NULL的Element,
void splitOut(Key const &upper_bound, SplayTree_Range *right)
將tree2 清空, 再將所有Key > upper_bound 的Element都丟過去
bool empty() const
回傳是否為空
Element last() const
回傳Key最大的Element, 如果SplayTree為空, 則回傳 this->end()
SplayTree_Range & copyFrom(SplayTree_Range const &tree2)
複製資料
是一種神乎其技的資料結構, 維護一堆 Key->Value . 並且支援 一些 std::map 難以快速實踐的操作, 如 split , merge , keyOffset ...
bool erase(Key const &key)
刪除一組資料
bool operator==(Element const &e2) const
same as same(e2)
Element & operator=(Element const &e2)
same as copyFrom
bool operator!=(Element const &e2) const
same as !same(e2)
void moveTo(SplayTree *tree2)
將資料都丟到 tree2 身上, 並且清空自己
size_t size() const
回傳資料個數
類似 stl 的 iterator ,不過這邊叫做Element
SplayTree_Range(SplayTree_Range const &tree2)
constructor, 複製資料
Entry & operator*()
重導至std::pair<Key const&,Value&>&
void valueOffset(Value const &delta)
將所有Element的Value同加上 delta
SplayTree(SplayTree const &tree2)
constructor, 複製資料
Value & operator[](Key const &key)
就像stl::map::operator[]
void keyOffset(Key const &delta)
將所有Element的Key同加上 delta
void keyOffset(Key const &delta)
將所有Element的Key同加上 delta
Element(Element const &element2)
void valueOverride(Value const &value)
將所有Element的Value全部設定成value
Element first() const
回傳Key最小的Element, 如果SplayTree為空, 則回傳 this->end()
Element end() const
回傳一個指向NULL的Element,
bool insert(Key const &key, Value const &value)
插入一組(Key —> Value)
Element rLowerBound(Key const &key) const
找出第一個(最小的) Element且 k >= 它的 Key, 並且回傳之.
~SplayTree_Range()
destructor
Element last() const
回傳Key最大的Element, 如果SplayTree為空, 則回傳 this->end()
bool erase(Key const &key)
刪除一組資料
Element rUpperBound(Key const &key) const
找出第一個(最小的) Element且 k > 它的 Key, 並且回傳之.
void moveTo(SplayTree_Range *tree2)
將資料都丟到 tree2 身上, 並且清空自己
SplayTree & operator=(SplayTree const &tree2)
same as copyFrom(tree2)
Element rUpperBound(Key const &key) const
找出第一個(最小的) Element且 k > 它的 Key, 並且回傳之.
Element & copyFrom(Element const &e)
複製資料
Element rLowerBound(Key const &key) const
找出第一個(最小的) Element且 k >= 它的 Key, 並且回傳之.
Element & copyFrom(Element const &e)
複製資料