-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMap.h
More file actions
307 lines (273 loc) · 5.07 KB
/
TreeMap.h
File metadata and controls
307 lines (273 loc) · 5.07 KB
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
#ifndef __TREEMAP_H
#define __TREEMAP_H
#include "ElementNotExist.h"
template<class K,class V>
class TreeMap
{
public:
class Entry;
private:
struct node;
node *_root,*_null;
void _update(node *_X)
{
if(_X==_null) _X->_size=0;
else _X->_size=_X->_L->_size+_X->_R->_size+1;
}
void _left_rotate(node *&_X)
{
node *__F=_X->_F;
node *_Y=_X->_R;
if(__F!=_null)
{
if(__F->_L==_X) __F->_L=_Y;
else __F->_R=_Y;
}
_Y->_F=__F;
_X->_R=_Y->_L;
if(_X->_R!=_null) _X->_R->_F=_X;
_Y->_L=_X;
_X->_F=_Y;
_update(_X);
_update(_Y);
_X=_Y;
}
void _right_rotate(node *&_X)
{
node *__F=_X->_F;
node *_Y=_X->_L;
if(__F!=_null)
{
if(__F->_L==_X) __F->_L=_Y;
else __F->_R=_Y;
}
_Y->_F=__F;
_X->_L=_Y->_R;
if(_X->_L!=_null) _X->_L->_F=_X;
_Y->_R=_X;
_X->_F=_Y;
_update(_X);
_update(_Y);
_X=_Y;
}
node *insert(node *_X,Entry &_data)
{
if(_X==_null)
{
_X=new node(_data);
_X->_L=_X->_R=_X->_F=_null;
_X->_size=1;
}
else if(_data.key<_X->_data.key)
{
_X->_L=insert(_X->_L,_data);
_X->_L->_F=_X;
if(_X->_L->_L->_size>_X->_R->_size)
_right_rotate(_X);
}
else if(_data.key>_X->_data.key)
{
_X->_R=insert(_X->_R,_data);
_X->_R->_F=_X;
if(_X->_R->_R->_size>_X->_L->_size)
_left_rotate(_X);
}
else if(_data.key==_X->_data.key)
{
_X->_data.value=_data.value;
}
_update(_X);
return _X;
}
node *erase(node *_X,const K &key)
{
if(_X==_null) return _null;
if(key<_X->_data.key)
_X->_L=erase(_X->_L,key);
else if(key>_X->_data.key)
_X->_R=erase(_X->_R,key);
else if(_X->_L==_null && _X->_R==_null)
{
delete _X;
_X=_null;
}
else
{
if(_X->_L->_size<_X->_R->_size) _left_rotate(_X);
else _right_rotate(_X);
_X=erase(_X,key);
}
_update(_X);
return _X;
}
node *eraseall(node *_X)
{
if(_X==_null) return _null;
_X->_L=eraseall(_X->_L);
_X->_R=eraseall(_X->_R);
delete _X;
return _null;
}
node *findkey(const K &key) const
{
node *_X=_root;
while(_X!=_null)
{
if(_X->_data.key==key) return _X;
else if(key<_X->_data.key) _X=_X->_L;
else _X=_X->_R;
}
return _X;
}
bool findvalue(node *_X,const V &value) const
{
if(_X==_null) return false;
if(_X->_data.value==value) return true;
if(findvalue(_X->_L,value)) return true;
if(findvalue(_X->_R,value)) return true;
return false;
}
public:
class Iterator;
TreeMap()
{
_null=new node();
_null->_F=_null->_L=_null->_R=_null;
_root=_null;
}
TreeMap(const TreeMap &_X)
{
_null=new node();
_null->_F=_null->_L=_null->_R=_null;
_root=_null;
typename TreeMap<K,V>::Iterator it=_X.iterator();
while(it.hasNext())
{
Entry d=it.next();
put(d.key,d.value);
}
}
~TreeMap()
{
clear();
delete _null;
}
TreeMap& operator=(const TreeMap &_X)
{
if(this==&_X) return *this;
clear();
typename TreeMap<K,V>::Iterator it=_X.iterator();
while(it.hasNext())
{
Entry d=it.next();
put(d.key,d.value);
}
return *this;
}
Iterator iterator() const
{
node *_pos=_root;
while(_pos->_L!=_null) _pos=_pos->_L;
return Iterator(_pos,_null);
}
void clear()
{
_root=eraseall(_root);
}
bool containsKey(const K& key) const
{
return findkey(key)!=_null;
}
bool containsValue(const V& value) const
{
return findvalue(_root,value);
}
const V& get(const K& key) const
{
node *_X=findkey(key);
if(_X==_null) throw ElementNotExist();
return _X->_data.value;
}
void put(const K& key,const V& value)
{
Entry _pos(key,value);
_root=insert(_root,_pos);
}
void remove(const K& key)
{
int pre=_root->_size;
_root=erase(_root,key);
if(pre==_root->_size) throw ElementNotExist();
}
int size() const
{
return _root->_size;
}
bool isEmpty() const
{
return _root==_null;
}
};
template<class K,class V>
class TreeMap<K,V>::Entry
{
public:
K key;
V value;
Entry(K k,V v):key(k),value(v) {}
K getKey() const { return key; }
V getValue() const { return value; }
};
template<class K,class V>
struct TreeMap<K,V>::node
{
node *_L,*_R,*_F;
Entry _data;
int _size;
node ():_data(K(),V()),_size(0) {}
node(const Entry &dat):_data(dat),_size(0) {}
};
template<class K,class V>
class TreeMap<K,V>::Iterator
{
private:
node *_pos,*_null;
bool _flag;
public:
bool hasNext()
{
if(_pos==_null||_pos==NULL) return false;
if(_pos->_R!=_null||!_flag) return true;
node *_Q=_pos,*_X=_Q->_F;
while(_X!=_null)
{
if(_X->_L==_Q) return true;
else _Q=_X,_X=_Q->_F;
}
return false;
}
const Entry& next()
{
if(_pos==NULL||_pos==_null) throw ElementNotExist();
if(!_flag)
{
_flag=true;
return _pos->_data;
}
if(_pos->_R!=_null)
{
_pos=_pos->_R;
while(_pos->_L!=_null) _pos=_pos->_L;
return _pos->_data;
}
node *_Q=_pos,*_X=_Q->_F;
while(_X!=_null)
{
if(_X->_L==_Q) return (_pos=_X)->_data;
else _Q=_X,_X=_Q->_F;
}
throw ElementNotExist();
}
Iterator(node *_pos=NULL,node *_null=NULL):_pos(_pos),_null(_null),_flag(false) {}
};
#endif