forked from whr-a/Ticket-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
902 lines (897 loc) · 24.9 KB
/
map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/**
* implement a container like std::map
*/
#ifndef SJTU_MAP_HPP
#define SJTU_MAP_HPP
// only for std::less<T>
#include <functional>
#include <cstddef>
#include <memory>
#include <iostream>
#include "utility.hpp"
#include "exceptions.hpp"
namespace sjtu {
enum type {right,wrong};
template<
class Key,
class T,
class Compare = std::less<Key>
> class map {
public:
/**
* the internal type of data.
* it should have a default constructor, a copy constructor.
* You can use sjtu::map as value_type by typedef.
*/
typedef pair<const Key, T> value_type;
struct node
{
value_type value;
int height;
node *left,*right,*father;
node(){left=right=father=nullptr;}
node(value_type value_):value(value_){
left=right=father=nullptr;
}
};
int getheight(node *x){
if(x==nullptr)return 0;
if(x->left==nullptr && x->right==nullptr){
x->height=1;
return 1;
}
if(x->left==nullptr)x->height=x->right->height+1;
else if(x->right==nullptr)x->height=x->left->height+1;
else x->height=std::max(x->left->height , x->right->height) + 1;
return x->height;
}
node* findbegin(node *x) const {
if(x==nullptr)return nullptr;
if(x->left==nullptr)return x;
else return findbegin(x->left);
}
node* findend(node *x) const {
if(x==nullptr)return nullptr;
if(x->right==nullptr)return x;
else return findend(x->right);
}
void rightrotate(node *a){
if(a->father!=nullptr){
if(a->father->left==a)a->father->left=a->left;
else a->father->right=a->left;
}
if(a==root)root=a->left;
node *b=a->left;
b->father=a->father;
a->left=b->right;
if(b->right!=nullptr)b->right->father=a;
b->right=a;
a->father=b;
getheight(a);
getheight(a->father);
}
void leftrotate(node *a){
if(a->father!=nullptr){
if(a->father->left==a)a->father->left=a->right;
else a->father->right=a->right;
}
if(a==root)root=a->right;
node *b=a->right;
b->father=a->father;
a->right=b->left;
if(b->left!=nullptr)b->left->father=a;
b->left=a;
a->father=b;
getheight(a);
getheight(a->father);
}
node* root;
size_t length;
/**
* see BidirectionalIterator at CppReference for help.
*
* if there is anything wrong throw invalid_iterator.
* like it = map.begin(); --it;
* or it = map.end(); ++end();
*/
class const_iterator;
class iterator {
friend class map;
private:
/**
* TODO add data members
* just add whatever you want.
*/
public:
const static type iterator_assignable = right;
node *pointer;
map<Key,T,Compare>* mp;
iterator() {
// TODO
pointer=nullptr;
mp=nullptr;
}
iterator(const iterator &other) {
// TODO
pointer=other.pointer;
mp=other.mp;
}
/**
* TODO iter++
*/
node* findnext(node *x) const{
if(x==nullptr)return nullptr;
if(x->left==nullptr)return x;
else return findnext(x->left);
}
node* findleftroot(node *x) const{
if(x->father==nullptr)return nullptr;
if(x->father->left==x)return x->father;
return findleftroot(x->father);
}
iterator operator++(int) {
if(pointer==nullptr)throw(invalid_iterator());
iterator t(*this);
if(pointer->father==nullptr)pointer=findnext(pointer->right);
else{
if(pointer->father->left==pointer){
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=pointer->father;
}
else{
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=findleftroot(pointer->father);
}
}
return t;
}
/**
* TODO ++iter
*/
iterator & operator++() {
if(pointer==nullptr)throw(invalid_iterator());
if(pointer->father==nullptr)pointer=findnext(pointer->right);
else{
if(pointer->father->left==pointer){
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=pointer->father;
}
else{
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=findleftroot(pointer->father);
}
}
return *this;
}
/**
* TODO iter--
*/
node* findlast(node *x)const {
if(x->right==nullptr)return x;
else return findlast(x->right);
}
node* findrightroot(node *x)const {
if(x->father==nullptr)return nullptr;
if(x->father->right==x)return x->father;
else return findrightroot(x->father);
}
iterator operator--(int) {
if(mp->root==nullptr)throw(invalid_iterator());
iterator t(*this);
if(pointer==nullptr){
pointer=mp->findend(mp->root);
return t;
}
if(pointer->father==nullptr){
if(pointer->left==nullptr)throw(invalid_iterator());
pointer=findlast(pointer->left);
}
else{
if(pointer==pointer->father->left){
if(pointer->left==nullptr){
node *ans=findrightroot(pointer->father);
if(ans==nullptr)throw(invalid_iterator());
pointer=ans;
}
else pointer=findlast(pointer->left);
}
else{
if(pointer->left==nullptr)pointer=pointer->father;
else pointer=findlast(pointer->left);
}
}
return t;
}
/**
* TODO --iter
*/
iterator & operator--() {
if(mp->root==nullptr)throw(invalid_iterator());
if(pointer==nullptr){
pointer=mp->findend(mp->root);
return *this;
}
if(pointer->father==nullptr){
if(pointer->left==nullptr)throw(invalid_iterator());
pointer=findlast(pointer->left);
}
else{
if(pointer==pointer->father->left){
if(pointer->left==nullptr){
node *ans=findrightroot(pointer->father);
if(ans==nullptr)throw(invalid_iterator());
pointer=ans;
ans=nullptr;
}
else pointer=findlast(pointer->left);
}
else{
if(pointer->left==nullptr)pointer=pointer->father;
else pointer=findlast(pointer->left);
}
}
return *this;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
value_type & operator*() const {
return pointer->value;
}
bool operator==(const iterator &rhs) const {
if(mp==rhs.mp && pointer==rhs.pointer)return true;
else return false;
}
bool operator==(const const_iterator &rhs) const {
if(mp==rhs.mp && pointer==rhs.pointer)return true;
else return false;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return !((*this)==rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !((*this)==rhs);
}
/**
* for the support of it->first.
* See <http://kelvinh.github.io/blog/2013/11/20/overloading-of-member-access-operator-dash-greater-than-symbol-in-cpp/> for help.
*/
value_type* operator->() const noexcept {
return &(pointer->value);
}
};
class const_iterator {
friend class map;
// it should has similar member method as iterator.
// and it should be able to construct from an iterator.
private:
// data members.
public:
const static type iterator_assignable = wrong;
node *pointer;
const map<Key,T,Compare>* mp;
const_iterator() {
// TODO
pointer=nullptr;
mp=nullptr;
}
const_iterator(const iterator &other) {
// TODO
pointer=other.pointer;
mp=other.mp;
}
/**
* TODO iter++
*/
node* findnext(node *x) const{
if(x==nullptr)return nullptr;
if(x->left==nullptr)return x;
else return findnext(x->left);
}
node* findleftroot(node *x) const{
if(x->father==nullptr)return nullptr;
if(x->father->left==x)return x->father;
return findleftroot(x->father);
}
const_iterator operator++(int) {
if(pointer==nullptr)throw(invalid_iterator());
const_iterator t(*this);
if(pointer->father==nullptr)pointer=findnext(pointer->right);
else{
if(pointer->father->left==pointer){
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=pointer->father;
}
else{
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=findleftroot(pointer->father);
}
}
return t;
}
/**
* TODO ++iter
*/
const_iterator & operator++() {
if(pointer==nullptr)throw(invalid_iterator());
if(pointer->father==nullptr)pointer=findnext(pointer->right);
else{
if(pointer->father->left==pointer){
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=pointer->father;
}
else{
if(pointer->right!=nullptr)pointer=findnext(pointer->right);
else pointer=findleftroot(pointer->father);
}
}
return *this;
}
/**
* TODO iter--
*/
node* findlast(node *x)const {
if(x->right==nullptr)return x;
else return findlast(x->right);
}
node* findrightroot(node *x)const {
if(x->father==nullptr)return nullptr;
if(x->father->right==x)return x->father;
return findrightroot(x->father);
}
const_iterator operator--(int) {
if(mp->root==nullptr)throw(invalid_iterator());
const_iterator t(*this);
if(pointer==nullptr){
pointer=mp->findend(mp->root);
return t;
}
if(pointer->father==nullptr){
if(pointer->left==nullptr)throw(invalid_iterator());
pointer=findlast(pointer->left);
}
else{
if(pointer==pointer->father->left){
if(pointer->left==nullptr){
node *ans=findrightroot(pointer->father);
if(ans==nullptr)throw(invalid_iterator());
pointer=ans;
}
else pointer=findlast(pointer->left);
}
else{
if(pointer->left==nullptr)pointer=pointer->father;
else pointer=findlast(pointer->left);
}
}
return t;
}
/**
* TODO --iter
*/
const_iterator & operator--() {
if(mp->root==nullptr)throw(invalid_iterator());
if(pointer==nullptr){
pointer=mp->findend(mp->root);
return *this;
}
if(pointer->father==nullptr){
if(pointer->left==nullptr)throw(invalid_iterator());
pointer=findlast(pointer->left);
}
else{
if(pointer==pointer->father->left){
if(pointer->left==nullptr){
node *ans=findrightroot(pointer->father);
if(ans==nullptr)throw(invalid_iterator());
pointer=ans;
ans=nullptr;
}
else pointer=findlast(pointer->left);
}
else{
if(pointer->left==nullptr)pointer=pointer->father;
else pointer=findlast(pointer->left);
}
}
return *this;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
bool operator==(const iterator &rhs) const {
if(mp==rhs.mp && pointer==rhs.pointer)return true;
else return false;
}
bool operator==(const const_iterator &rhs) const {
if(mp==rhs.mp && pointer==rhs.pointer)return true;
else return false;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return !((*this)==rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !((*this)==rhs);
}
value_type* operator->() const noexcept {
return &(pointer->value);
}
value_type & operator*() const {
return pointer->value;
}
};
/**
* TODO two constructors
*/
map() {length=0;root=nullptr;}
void copytree(node *one,node *two){
if(two->left!=nullptr){
one->left=new node(two->left->value);
one->left->height=two->left->height;
one->left->father=one;
copytree(one->left,two->left);
}
else one->left=nullptr;
if(two->right!=nullptr){
one->right=new node(two->right->value);
one->right->height=two->right->height;
one->right->father=one;
copytree(one->right,two->right);
}
else one->right=nullptr;
}
void deletetree(node *one){
if(one->left!=nullptr){
deletetree(one->left);
delete one->left;
one->left=nullptr;
}
if(one->right!=nullptr){
deletetree(one->right);
delete one->right;
one->right=nullptr;
}
}
map(const map &other) {
length=other.length;
if(other.root!=nullptr){
root=new node(other.root->value);
root->height=other.root->height;
copytree(root,other.root);
}
else root=nullptr;
}
/**
* TODO assignment operator
*/
map & operator=(const map &other) {
if(this == &other) return *this;
length=other.length;
if(root!=nullptr){
deletetree(root);
delete root;
root=nullptr;
}
if(other.root!=nullptr){
root=new node(other.root->value);
root->height=other.root->height;
copytree(root,other.root);
}
else root=nullptr;
return *this;
}
/**
* TODO Destructors
*/
~map() {
if(root!=nullptr){
deletetree(root);
delete root;
root=nullptr;
}
}
/**
* TODO
* access specified element with bounds checking
* Returns a reference to the mapped value of the element with key equivalent to key.
* If no such element exists, an exception of type `index_out_of_bound'
*/
T & search(node *one,const Key &key)const{
if(!(Compare()(one->value.first,key)) && !(Compare()(key,one->value.first)))return one->value.second;
if(Compare()(key,one->value.first)){
if(one->left!=nullptr)return search(one->left,key);
else throw(index_out_of_bound());
}
else{
if(one->right!=nullptr)return search(one->right,key);
else throw(index_out_of_bound());
}
}
const T & searchh(node *one,const Key &key) const {
if(!(Compare()(one->value.first,key)) && !(Compare()(key,one->value.first)))return one->value.second;
if(Compare()(key,one->value.first)){
if(one->left!=nullptr)return search(one->left,key);
else throw(index_out_of_bound());
}
else{
if(one->right!=nullptr)return search(one->right,key);
else throw(index_out_of_bound());
}
}
T & at(const Key &key) {
if(root==nullptr)throw(index_out_of_bound());
return search(root,key);
}
const T & at(const Key &key) const {
if(root==nullptr)throw(index_out_of_bound());
return searchh(root,key);
}
/**
* TODO
* access specified element
* Returns a reference to the value that is mapped to a key equivalent to key,
* performing an insertion if such key does not already exist.
*/
pair<node*,bool> finds(node *x,const Key &key){//1==find 0==not find
if(x==nullptr){
value_type temp(key,T());
root=new node(temp);
root->height=1;
return pair<node*,bool>(root,0);
}
if(!(Compare()(x->value.first,key)) && !(Compare()(key,x->value.first)))return pair<node*,bool>(x,1);
if(Compare()(key,x->value.first)){
if(x->left!=nullptr)return finds(x->left,key);
else{
value_type temp(key,T());
x->left=new node(temp);
x->left->height=1;
x->left->father=x;
return pair<node*,bool>(x->left,0);
}
}
else{
if(x->right!=nullptr)return finds(x->right,key);
else{
value_type temp(key,T());
x->right=new node(temp);
x->right->height=1;
x->right->father=x;
return pair<node*,bool>(x->right,0);
}
}
}
void LL(node *x){
rightrotate(x);
}
void RR(node *x){
leftrotate(x);
}
void LR(node *x){
leftrotate(x->left);
rightrotate(x);
}
void RL(node *x){
rightrotate(x->right);
leftrotate(x);
}
void balancetree(node *x){
if(x==nullptr)return;
getheight(x);
if(x->left==nullptr && x->right==nullptr && x->father==nullptr)return;
if(x->left==nullptr && x->right==nullptr){balancetree(x->father);return;}
if(getheight(x->left)-getheight(x->right) == 2){
if(getheight(x->left->left)>=getheight(x->left->right))LL(x);
else LR(x);
balancetree(x->father->father);
return;
}
if(getheight(x->right)-getheight(x->left) == 2){
if(getheight(x->right->right)>=getheight(x->right->left))RR(x);
else RL(x);
balancetree(x->father->father);
return;
}
balancetree(x->father);
return;
}
T & operator[](const Key &key) {
pair<node*,bool> ans=finds(root,key);
if(ans.second==1)return ((ans.first)->value).second;
length++;
balancetree(ans.first);
return (ans.first->value).second;
}
/**
* behave like at() throw index_out_of_bound if such key does not exist.
*/
const T & operator[](const Key &key) const {
if(root==nullptr)throw(index_out_of_bound());
return search(root,key);
}
/**
* return a iterator to the beginning
*/
iterator begin() {
iterator temp;
temp.mp=this;
temp.pointer=findbegin(root);
return temp;
}
const_iterator cbegin() const {
const_iterator temp;
temp.mp = this;
temp.pointer = findbegin(root);
return temp;
}
/**
* return a iterator to the end
* in fact, it returns past-the-end.
*/
iterator end() {
iterator temp;
temp.mp=this;
temp.pointer=nullptr;
return temp;
}
const_iterator cend() const {
const_iterator temp;
temp.mp=this;
temp.pointer=nullptr;
return temp;
}
/**
* checks whether the container is empty
* return true if empty, otherwise false.
*/
bool empty() const {
if(length==0)return true;
else return false;
}
/**
* returns the number of elements.
*/
size_t size() const {
return length;
}
/**
* clears the contents
*/
void clear() {
length=0;
if(root!=nullptr){
deletetree(root);
delete root;
}
root=nullptr;
}
/**
* insert an element.
* return a pair, the first of the pair is
* the iterator to the new element (or the element that prevented the insertion),
* the second one is true if insert successfully, or false.
*/
pair<node*,bool> findss(node *x,const value_type &value_){//1==find 0==not find
if(x==nullptr){
root=new node(value_);
root->height=1;
return pair<node*,bool>(root,0);
}
if(!(Compare()(x->value.first,value_.first)) && !(Compare()(value_.first,x->value.first)))return pair<node*,bool>(x,1);
if(Compare()(value_.first,x->value.first)){
if(x->left!=nullptr)return findss(x->left,value_);
else{
x->left=new node(value_);
x->left->height=1;
x->left->father=x;
return pair<node*,bool>(x->left,0);
}
}
else{
if(x->right!=nullptr)return findss(x->right,value_);
else{
x->right=new node(value_);
x->right->height=1;
x->right->father=x;
return pair<node*,bool>(x->right,0);
}
}
}
pair<iterator, bool> insert(const value_type &value_) {
pair<node*,bool> ans=findss(root,value_);
if(ans.second==1){
iterator temp;
temp.mp=this;
temp.pointer=ans.first;
return pair<iterator, bool>(temp,0);
}
else{
balancetree(ans.first);
length++;
iterator temp;
temp.mp=this;
temp.pointer=ans.first;
return pair<iterator, bool>(temp,1);
}
}
/**
* erase the element at pos.
*
* throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this)
*/
node* findright(node *x)const {
if(x->right==nullptr)return x;
else return findright(x->right);
}
node* findleft(node *x)const {
if(x->left==nullptr)return x;
else return findleft(x->left);
}
void swaps(node *x,node *y){
if(x==root)root=y;
if(x->left==y){
if(y->left!=nullptr)y->left->father=x;
if(y->right!=nullptr)y->right->father=x;
if(x->right!=nullptr)x->right->father=y;
node *temp=x->right;
x->left=y->left;
x->right=y->right;
y->left=x;
y->right=temp;
if(x->father!=nullptr){
if(x->father->left==x)x->father->left=y;
else x->father->right=y;
}
y->father=x->father;
x->father=y;
int tempp=x->height;
x->height=y->height;
y->height=tempp;
return;
}
if(x->right==y){
if(y->left!=nullptr)y->left->father=x;
if(y->right!=nullptr)y->right->father=x;
if(x->left!=nullptr)x->left->father=y;
node *temp=x->left;
x->left=y->left;
x->right=y->right;
y->right=x;
y->left=temp;
if(x->father!=nullptr){
if(x->father->left==x)x->father->left=y;
else x->father->right=y;
}
y->father=x->father;
x->father=y;
int tempp=x->height;
x->height=y->height;
y->height=tempp;
return;
}
if(x->father!=nullptr){
if(x->father->left==x)x->father->left=y;
else x->father->right=y;
}
if(y->father!=nullptr){
if(y->father->left==y)y->father->left=x;
else y->father->right=x;
}
if(x->left!=nullptr)x->left->father=y;
if(x->right!=nullptr)x->right->father=y;
if(y->left!=nullptr)y->left->father=x;
if(y->right!=nullptr)y->right->father=x;
node *l=x->left,*r=x->right,*f=x->father;
x->left=y->left;x->right=y->right;x->father=y->father;
y->left=l;y->right=r;y->father=f;
int tempp=x->height;
x->height=y->height;
y->height=tempp;
return;
}
void erase(iterator pos) {
if(pos.mp!=this || pos.pointer==nullptr)throw(invalid_iterator());
length--;
node *changer;
if(pos.pointer->left==nullptr && pos.pointer->right==nullptr){
if(pos.pointer->father!=nullptr){
changer=pos.pointer->father;
if(pos.pointer->father->left==pos.pointer)pos.pointer->father->left=nullptr;
else pos.pointer->father->right=nullptr;
}
else{
delete root;
root=nullptr;
pos.pointer=nullptr;
return;
}
delete pos.pointer;
getheight(changer);
}
else if(pos.pointer->left==nullptr){
node *x=findleft(pos.pointer->right);
swaps(pos.pointer,x);
changer=pos.pointer->father;
if(pos.pointer->father->left==pos.pointer)pos.pointer->father->left=pos.pointer->right;
else pos.pointer->father->right=pos.pointer->right;
if(pos.pointer->right!=nullptr)pos.pointer->right->father=pos.pointer->father;
delete pos.pointer;
getheight(changer);
}
else {
node *x=findright(pos.pointer->left);
swaps(pos.pointer,x);
changer=pos.pointer->father;
if(pos.pointer->father->left==pos.pointer)pos.pointer->father->left=pos.pointer->left;
else pos.pointer->father->right=pos.pointer->left;
if(pos.pointer->left!=nullptr)pos.pointer->left->father=pos.pointer->father;
delete pos.pointer;
getheight(changer);
}
balancetree(changer);
}
/**
* Returns the number of elements with key
* that compares equivalent to the specified argument,
* which is either 1 or 0
* since this container does not allow duplicates.
* The default method of check the equivalence is !(a < b || b > a)
*/
bool searchn(node *one,const Key &key) const {
if(!(Compare()(one->value.first,key)) && !(Compare()(key,one->value.first)))return true;
if(Compare()(key,one->value.first)){
if(one->left!=nullptr)return searchn(one->left,key);
else return false;
}
else{
if(one->right!=nullptr)return searchn(one->right,key);
else return false;
}
}
size_t count(const Key &key) const {
if(root==nullptr)return 0;
return searchn(root,key);
}
/**
* Finds an element with key equivalent to key.
* key value of the element to search for.
* Iterator to an element with key equivalent to key.
* If no such element is found, past-the-end (see end()) iterator is returned.
*/
node* searchnode(node *one,const Key &key)const{
if(one==nullptr)return nullptr;
if(!(Compare()(one->value.first,key)) && !(Compare()(key,one->value.first)))return one;
if(Compare()(key,one->value.first)){
if(one->left!=nullptr)return searchnode(one->left,key);
else return nullptr;
}
else{
if(one->right!=nullptr)return searchnode(one->right,key);
else return nullptr;
}
}
iterator find(const Key &key) {
iterator ans;
ans.mp=this;
ans.pointer=searchnode(root,key);
return ans;
}
const_iterator find(const Key &key) const {
const_iterator ans;
ans.mp=this;
ans.pointer=searchnode(root,key);
return ans;
}
};
template<typename T>
struct my_type_traits{
const static bool value=T::iterator_assignable==right ? 1 : 0;
};
}
#endif